@simbo/git-fixture 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE.md +20 -0
- package/README.md +138 -2
- package/dist/git-fixture.d.ts +27 -0
- package/dist/git-fixture.js +172 -0
- package/dist/git-fixture.js.map +1 -0
- package/dist/git-fixture.types.d.ts +131 -0
- package/dist/git-fixture.types.js +2 -0
- package/dist/git-fixture.types.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @simbo/git-fixture
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- a68ec52: Add `@simbo/git-fixture` to create temporary Git repositories with a
|
|
8
|
+
neutral configuration for tests.
|
|
9
|
+
|
|
10
|
+
- `createGitFixture()` creates a repository in a new temporary directory,
|
|
11
|
+
optionally with an initial commit, and `cloneGitFixture()` clones one.
|
|
12
|
+
- The fixture runs Git, writes files, commits, and removes itself with
|
|
13
|
+
`cleanup()` or `await using`.
|
|
14
|
+
- Global and system configurations are ignored, Git does not search above the
|
|
15
|
+
temporary directory, signing is off, and author, committer, and dates are
|
|
16
|
+
fixed, so commit hashes are reproducible.
|
|
17
|
+
- `env` holds the variables of that configuration for the code under test.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Simon Lepel <simbo@simbo.de> (https://simbo.de)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,139 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Git Fixture
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
📦 [**`@simbo/git-fixture`**](https://npmjs.com/package/@simbo/git-fixture)
|
|
4
|
+
|
|
5
|
+
Create temporary Git repositories with a neutral configuration for tests.
|
|
6
|
+
|
|
7
|
+
Built on [`@simbo/git-exec`](https://npmjs.com/package/@simbo/git-exec), for
|
|
8
|
+
tests that run real Git instead of mocking it, with any test runner.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- `createGitFixture()` creates a repository in a new temporary directory,
|
|
13
|
+
optionally with an initial commit
|
|
14
|
+
|
|
15
|
+
- `cloneGitFixture()` clones a repository into a new temporary directory
|
|
16
|
+
|
|
17
|
+
- Helpers to run Git, write files, and commit in the repository
|
|
18
|
+
|
|
19
|
+
- A neutral configuration, so that results do not depend on the host: global and
|
|
20
|
+
system configurations are ignored, Git does not search above the temporary
|
|
21
|
+
directory, signing is off, and author, committer, and dates are fixed, which
|
|
22
|
+
makes commit hashes reproducible
|
|
23
|
+
|
|
24
|
+
- Paths with symbolic links resolved, such as `/private/var` instead of `/var`
|
|
25
|
+
on macOS
|
|
26
|
+
|
|
27
|
+
- Cleanup with `cleanup()` or `await using`
|
|
28
|
+
|
|
29
|
+
## Requirements
|
|
30
|
+
|
|
31
|
+
Requires Node.js `^24.15.0 || >=26.0.0` and Git 2.28 or newer on the `PATH`. The
|
|
32
|
+
tests run on macOS and Linux.
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Install `@simbo/git-fixture` from the npm registry as a development dependency:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm i -D @simbo/git-fixture
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
For a complete API reference, see the
|
|
45
|
+
[documentation](https://simbo.de/packages/modules/_simbo_git-fixture/).
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { cloneGitFixture, createGitFixture } from '@simbo/git-fixture';
|
|
49
|
+
|
|
50
|
+
it('reads the latest commit', async () => {
|
|
51
|
+
await using repo = await createGitFixture({
|
|
52
|
+
files: { 'README.md': '# Test\n', 'src/index.ts': 'export {};\n' },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
await repo.write('src/index.ts', 'export const answer = 42;\n');
|
|
56
|
+
const hash = await repo.commit('add answer', { 'CHANGELOG.md': '# 1.0.0\n' });
|
|
57
|
+
|
|
58
|
+
await using clone = await cloneGitFixture(repo.dir, {
|
|
59
|
+
args: ['--depth', '1'],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(await clone.git(['rev-parse', 'HEAD'])).toBe(hash);
|
|
63
|
+
}); // both directories are removed here
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Without `await using`, remove the repository with `cleanup()`, for example in
|
|
67
|
+
`afterEach()`.
|
|
68
|
+
|
|
69
|
+
### The Code Under Test
|
|
70
|
+
|
|
71
|
+
The fixture runs its own Git commands with the neutral configuration. Code under
|
|
72
|
+
test that runs Git itself reads the environment of the process instead. Set the
|
|
73
|
+
variables of `env` for it, for example with Vitest:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
for (const [name, value] of Object.entries(repo.env)) {
|
|
77
|
+
vi.stubEnv(name, value);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`env` holds `GIT_CONFIG_GLOBAL` (the null device), `GIT_CONFIG_NOSYSTEM`,
|
|
82
|
+
`GIT_CEILING_DIRECTORIES` (the parent of the repository), `GIT_TERMINAL_PROMPT`,
|
|
83
|
+
and name, email, and date of author and committer. Signing is turned off with
|
|
84
|
+
`-c` options for the commands of the fixture only.
|
|
85
|
+
|
|
86
|
+
### Options
|
|
87
|
+
|
|
88
|
+
Both functions accept these options:
|
|
89
|
+
|
|
90
|
+
| Option | Description | Default |
|
|
91
|
+
| -------- | ------------------------------------------------------------------------- | ------------------------ |
|
|
92
|
+
| `prefix` | The prefix of the temporary directory name. | `'git-fixture-'` |
|
|
93
|
+
| `date` | The author and committer date of all commits, as `Date` or `string`. | `'2000-01-01T00:00:00Z'` |
|
|
94
|
+
| `debug` | A callback that receives every Git command of the fixture before it runs. | none |
|
|
95
|
+
|
|
96
|
+
`createGitFixture()` also accepts:
|
|
97
|
+
|
|
98
|
+
| Option | Description | Default |
|
|
99
|
+
| --------------- | ------------------------------------------------------------- | ----------- |
|
|
100
|
+
| `initialBranch` | The name of the initial branch. | `'main'` |
|
|
101
|
+
| `files` | Files of an initial commit; without them, there is no commit. | none |
|
|
102
|
+
| `message` | The message of the initial commit. | `'initial'` |
|
|
103
|
+
|
|
104
|
+
`cloneGitFixture(source, options)` also accepts `args`, additional arguments for
|
|
105
|
+
`git clone` that are passed before the source.
|
|
106
|
+
|
|
107
|
+
### The Fixture
|
|
108
|
+
|
|
109
|
+
| Member | Description |
|
|
110
|
+
| ------------------------ | ---------------------------------------------------------------------------------------------------------- |
|
|
111
|
+
| `dir` | The absolute path of the repository. |
|
|
112
|
+
| `env` | The environment variables of the neutral configuration. |
|
|
113
|
+
| `git(args)` | Runs Git in the repository and returns the trimmed standard output. |
|
|
114
|
+
| `gitSucceeds(args)` | Runs Git in the repository and reports whether it exited with code 0. |
|
|
115
|
+
| `path(...segments)` | Resolves a path in the repository. |
|
|
116
|
+
| `write(path, contents)` | Writes a file without staging it; paths outside the repository, also through symbolic links, are rejected. |
|
|
117
|
+
| `writeFiles(files)` | Writes several files without staging them. |
|
|
118
|
+
| `commit(message, files)` | Writes the files, stages all changes, commits, and returns the commit hash. |
|
|
119
|
+
| `cleanup()` | Removes the repository directory. |
|
|
120
|
+
|
|
121
|
+
Failed Git commands reject with a `GitError` of `@simbo/git-exec`. If
|
|
122
|
+
`createGitFixture()` or `cloneGitFixture()` fails, the temporary directory is
|
|
123
|
+
removed.
|
|
124
|
+
|
|
125
|
+
## Security
|
|
126
|
+
|
|
127
|
+
Git runs through `@simbo/git-exec` without a shell. Git itself, however, runs
|
|
128
|
+
commands that some options name, such as `--upload-pack` for `clone`.
|
|
129
|
+
`@simbo/git-fixture` passes the arguments of `git()`, `gitSucceeds()`, and
|
|
130
|
+
`cloneGitFixture()` to Git as given and does not filter them.
|
|
131
|
+
|
|
132
|
+
The package is meant for tests with values from the test itself. Never pass
|
|
133
|
+
untrusted values as options; place them after `--end-of-options` or `--`, as
|
|
134
|
+
described in the
|
|
135
|
+
[security notes of `@simbo/git-exec`](https://github.com/simbo/packages/tree/main/packages/git-exec#security).
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
[MIT © Simon Lepel](https://simbo.mit-license.org/2025/)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CloneGitFixtureOptions, CreateGitFixtureOptions, GitFixture } from './git-fixture.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a temporary Git repository, optionally with an initial commit.
|
|
4
|
+
*
|
|
5
|
+
* The repository and all of its Git commands use a neutral configuration:
|
|
6
|
+
* global and system configurations are ignored, Git does not search for
|
|
7
|
+
* repositories above the temporary directory, and author, committer, and
|
|
8
|
+
* dates are fixed.
|
|
9
|
+
*
|
|
10
|
+
* @param options - Options for the repository.
|
|
11
|
+
* @returns The fixture.
|
|
12
|
+
* @throws {GitError} if Git fails; the directory is removed then.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createGitFixture(options?: CreateGitFixtureOptions): Promise<GitFixture>;
|
|
15
|
+
/**
|
|
16
|
+
* Clones a repository into a temporary directory with the same neutral
|
|
17
|
+
* configuration as `createGitFixture()`.
|
|
18
|
+
*
|
|
19
|
+
* The source is passed to Git as given. Pass untrusted values neither as
|
|
20
|
+
* source nor in `args`: options such as `--upload-pack` make Git run commands.
|
|
21
|
+
*
|
|
22
|
+
* @param source - The path or URL of the repository to clone.
|
|
23
|
+
* @param options - Options for the clone.
|
|
24
|
+
* @returns The fixture of the clone.
|
|
25
|
+
* @throws {GitError} if Git fails; the directory is removed then.
|
|
26
|
+
*/
|
|
27
|
+
export declare function cloneGitFixture(source: string, options?: CloneGitFixtureOptions): Promise<GitFixture>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { lstat, mkdir, mkdtemp, realpath, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { devNull, tmpdir } from 'node:os';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import { git, gitSucceeds } from '@simbo/git-exec';
|
|
5
|
+
/**
|
|
6
|
+
* The default author and committer date.
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_DATE = '2000-01-01T00:00:00Z';
|
|
9
|
+
/**
|
|
10
|
+
* The author and committer name.
|
|
11
|
+
*/
|
|
12
|
+
const NAME = 'Test';
|
|
13
|
+
/**
|
|
14
|
+
* The author and committer email.
|
|
15
|
+
*/
|
|
16
|
+
const EMAIL = 'test@example.com';
|
|
17
|
+
/**
|
|
18
|
+
* Configuration passed with `-c` to every Git command of a fixture, so that
|
|
19
|
+
* a repository configuration cannot turn signing on.
|
|
20
|
+
*/
|
|
21
|
+
const CONFIG_ARGS = ['-c', 'commit.gpgsign=false', '-c', 'tag.gpgsign=false'];
|
|
22
|
+
/**
|
|
23
|
+
* Creates a temporary Git repository, optionally with an initial commit.
|
|
24
|
+
*
|
|
25
|
+
* The repository and all of its Git commands use a neutral configuration:
|
|
26
|
+
* global and system configurations are ignored, Git does not search for
|
|
27
|
+
* repositories above the temporary directory, and author, committer, and
|
|
28
|
+
* dates are fixed.
|
|
29
|
+
*
|
|
30
|
+
* @param options - Options for the repository.
|
|
31
|
+
* @returns The fixture.
|
|
32
|
+
* @throws {GitError} if Git fails; the directory is removed then.
|
|
33
|
+
*/
|
|
34
|
+
export async function createGitFixture(options = {}) {
|
|
35
|
+
const { initialBranch = 'main', files, message = 'initial' } = options;
|
|
36
|
+
const fixture = await createFixture(options);
|
|
37
|
+
try {
|
|
38
|
+
await fixture.git(['init', '--quiet', `--initial-branch=${initialBranch}`]);
|
|
39
|
+
if (files) {
|
|
40
|
+
await fixture.commit(message, files);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
await fixture.cleanup();
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
return fixture;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Clones a repository into a temporary directory with the same neutral
|
|
51
|
+
* configuration as `createGitFixture()`.
|
|
52
|
+
*
|
|
53
|
+
* The source is passed to Git as given. Pass untrusted values neither as
|
|
54
|
+
* source nor in `args`: options such as `--upload-pack` make Git run commands.
|
|
55
|
+
*
|
|
56
|
+
* @param source - The path or URL of the repository to clone.
|
|
57
|
+
* @param options - Options for the clone.
|
|
58
|
+
* @returns The fixture of the clone.
|
|
59
|
+
* @throws {GitError} if Git fails; the directory is removed then.
|
|
60
|
+
*/
|
|
61
|
+
export async function cloneGitFixture(source, options = {}) {
|
|
62
|
+
const { args = [] } = options;
|
|
63
|
+
const fixture = await createFixture(options);
|
|
64
|
+
try {
|
|
65
|
+
await fixture.git(['clone', '--quiet', ...args, '--', source, '.']);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
await fixture.cleanup();
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
return fixture;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a temporary directory and the fixture helpers for it.
|
|
75
|
+
*
|
|
76
|
+
* @param options - Options for the fixture.
|
|
77
|
+
* @returns The fixture.
|
|
78
|
+
*/
|
|
79
|
+
async function createFixture(options) {
|
|
80
|
+
const { prefix = 'git-fixture-', date = DEFAULT_DATE, debug } = options;
|
|
81
|
+
const tempDir = await mkdtemp(join(tmpdir(), prefix));
|
|
82
|
+
const dir = await realpath(tempDir);
|
|
83
|
+
const gitDate = typeof date === 'string' ? date : date.toISOString();
|
|
84
|
+
const env = Object.freeze({
|
|
85
|
+
GIT_CONFIG_GLOBAL: devNull,
|
|
86
|
+
GIT_CONFIG_NOSYSTEM: '1',
|
|
87
|
+
GIT_CEILING_DIRECTORIES: dirname(dir),
|
|
88
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
89
|
+
GIT_AUTHOR_NAME: NAME,
|
|
90
|
+
GIT_AUTHOR_EMAIL: EMAIL,
|
|
91
|
+
GIT_AUTHOR_DATE: gitDate,
|
|
92
|
+
GIT_COMMITTER_NAME: NAME,
|
|
93
|
+
GIT_COMMITTER_EMAIL: EMAIL,
|
|
94
|
+
GIT_COMMITTER_DATE: gitDate,
|
|
95
|
+
});
|
|
96
|
+
const run = async (args) => git([...CONFIG_ARGS, ...args], { cwd: dir, env, debug });
|
|
97
|
+
const succeeds = async (args) => gitSucceeds([...CONFIG_ARGS, ...args], { cwd: dir, env, debug });
|
|
98
|
+
const write = async (path, contents) => {
|
|
99
|
+
const file = await resolveInside(dir, path);
|
|
100
|
+
await mkdir(dirname(file), { recursive: true });
|
|
101
|
+
await writeFile(file, contents);
|
|
102
|
+
};
|
|
103
|
+
const writeFiles = async (files) => {
|
|
104
|
+
for (const [path, contents] of Object.entries(files)) {
|
|
105
|
+
await write(path, contents);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const cleanup = async () => rm(dir, { recursive: true, force: true });
|
|
109
|
+
return {
|
|
110
|
+
dir,
|
|
111
|
+
env,
|
|
112
|
+
git: run,
|
|
113
|
+
gitSucceeds: succeeds,
|
|
114
|
+
path: (...segments) => join(dir, ...segments),
|
|
115
|
+
write,
|
|
116
|
+
writeFiles,
|
|
117
|
+
commit: async (message, files = {}) => {
|
|
118
|
+
await writeFiles(files);
|
|
119
|
+
await run(['add', '--all']);
|
|
120
|
+
await run(['commit', '--quiet', '--allow-empty', '--message', message]);
|
|
121
|
+
return run(['rev-parse', 'HEAD']);
|
|
122
|
+
},
|
|
123
|
+
cleanup,
|
|
124
|
+
[Symbol.asyncDispose]: cleanup,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Resolves a path relative to a directory and ensures that it stays inside,
|
|
129
|
+
* also through symbolic links.
|
|
130
|
+
*
|
|
131
|
+
* @param dir - The directory, with symbolic links resolved.
|
|
132
|
+
* @param path - The relative path.
|
|
133
|
+
* @returns The absolute path.
|
|
134
|
+
* @throws {Error} if the path or the target of a symbolic link is outside the directory.
|
|
135
|
+
*/
|
|
136
|
+
async function resolveInside(dir, path) {
|
|
137
|
+
const file = resolve(dir, path);
|
|
138
|
+
const outside = new Error(`The path "${path}" is not a file path inside the repository "${dir}".`);
|
|
139
|
+
if (!isInside(dir, file)) {
|
|
140
|
+
throw outside;
|
|
141
|
+
}
|
|
142
|
+
// The deepest existing ancestor and an existing file may be symbolic links.
|
|
143
|
+
let existing = file;
|
|
144
|
+
while (!(await lstat(existing)
|
|
145
|
+
.then(() => true)
|
|
146
|
+
.catch(() => false))) {
|
|
147
|
+
existing = dirname(existing);
|
|
148
|
+
}
|
|
149
|
+
let target;
|
|
150
|
+
try {
|
|
151
|
+
target = await realpath(existing);
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
throw outside;
|
|
155
|
+
}
|
|
156
|
+
if (target !== dir && !isInside(dir, target)) {
|
|
157
|
+
throw outside;
|
|
158
|
+
}
|
|
159
|
+
return file;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Reports whether a path is inside a directory, not the directory itself.
|
|
163
|
+
*
|
|
164
|
+
* @param dir - The directory.
|
|
165
|
+
* @param path - The absolute path.
|
|
166
|
+
* @returns `true` if the path is inside the directory.
|
|
167
|
+
*/
|
|
168
|
+
function isInside(dir, path) {
|
|
169
|
+
const relativePath = relative(dir, path);
|
|
170
|
+
return (relativePath !== '' && relativePath !== '..' && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath));
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=git-fixture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-fixture.js","sourceRoot":"","sources":["../src/git-fixture.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAE9E,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAUnD;;GAEG;AACH,MAAM,YAAY,GAAG,sBAAsB,CAAC;AAE5C;;GAEG;AACH,MAAM,IAAI,GAAG,MAAM,CAAC;AAEpB;;GAEG;AACH,MAAM,KAAK,GAAG,kBAAkB,CAAC;AA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AU,CAAC,YAAY,CAAC,CAClH,CAAC;AACJ,CAAC","sourcesContent":["import { lstat, mkdir, mkdtemp, realpath, rm, writeFile } from 'node:fs/promises';\nimport { devNull, tmpdir } from 'node:os';\nimport { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';\n\nimport { git, gitSucceeds } from '@simbo/git-exec';\n\nimport type {\n CloneGitFixtureOptions,\n CreateGitFixtureOptions,\n GitFixture,\n GitFixtureFiles,\n GitFixtureOptions,\n} from './git-fixture.types.js';\n\n/**\n * The default author and committer date.\n */\nconst DEFAULT_DATE = '2000-01-01T00:00:00Z';\n\n/**\n * The author and committer name.\n */\nconst NAME = 'Test';\n\n/**\n * The author and committer email.\n */\nconst EMAIL = 'test@example.com';\n\n/**\n * Configuration passed with `-c` to every Git command of a fixture, so that\n * a repository configuration cannot turn signing on.\n */\nconst CONFIG_ARGS = ['-c', 'commit.gpgsign=false', '-c', 'tag.gpgsign=false'] as const;\n\n/**\n * Creates a temporary Git repository, optionally with an initial commit.\n *\n * The repository and all of its Git commands use a neutral configuration:\n * global and system configurations are ignored, Git does not search for\n * repositories above the temporary directory, and author, committer, and\n * dates are fixed.\n *\n * @param options - Options for the repository.\n * @returns The fixture.\n * @throws {GitError} if Git fails; the directory is removed then.\n */\nexport async function createGitFixture(options: CreateGitFixtureOptions = {}): Promise<GitFixture> {\n const { initialBranch = 'main', files, message = 'initial' } = options;\n const fixture = await createFixture(options);\n\n try {\n await fixture.git(['init', '--quiet', `--initial-branch=${initialBranch}`]);\n if (files) {\n await fixture.commit(message, files);\n }\n } catch (error) {\n await fixture.cleanup();\n throw error;\n }\n\n return fixture;\n}\n\n/**\n * Clones a repository into a temporary directory with the same neutral\n * configuration as `createGitFixture()`.\n *\n * The source is passed to Git as given. Pass untrusted values neither as\n * source nor in `args`: options such as `--upload-pack` make Git run commands.\n *\n * @param source - The path or URL of the repository to clone.\n * @param options - Options for the clone.\n * @returns The fixture of the clone.\n * @throws {GitError} if Git fails; the directory is removed then.\n */\nexport async function cloneGitFixture(source: string, options: CloneGitFixtureOptions = {}): Promise<GitFixture> {\n const { args = [] } = options;\n const fixture = await createFixture(options);\n\n try {\n await fixture.git(['clone', '--quiet', ...args, '--', source, '.']);\n } catch (error) {\n await fixture.cleanup();\n throw error;\n }\n\n return fixture;\n}\n\n/**\n * Creates a temporary directory and the fixture helpers for it.\n *\n * @param options - Options for the fixture.\n * @returns The fixture.\n */\nasync function createFixture(options: GitFixtureOptions): Promise<GitFixture> {\n const { prefix = 'git-fixture-', date = DEFAULT_DATE, debug } = options;\n\n const tempDir = await mkdtemp(join(tmpdir(), prefix));\n const dir = await realpath(tempDir);\n const gitDate = typeof date === 'string' ? date : date.toISOString();\n\n const env: Readonly<Record<string, string>> = Object.freeze({\n GIT_CONFIG_GLOBAL: devNull,\n GIT_CONFIG_NOSYSTEM: '1',\n GIT_CEILING_DIRECTORIES: dirname(dir),\n GIT_TERMINAL_PROMPT: '0',\n GIT_AUTHOR_NAME: NAME,\n GIT_AUTHOR_EMAIL: EMAIL,\n GIT_AUTHOR_DATE: gitDate,\n GIT_COMMITTER_NAME: NAME,\n GIT_COMMITTER_EMAIL: EMAIL,\n GIT_COMMITTER_DATE: gitDate,\n });\n\n const run = async (args: readonly string[]): Promise<string> =>\n git([...CONFIG_ARGS, ...args], { cwd: dir, env, debug });\n\n const succeeds = async (args: readonly string[]): Promise<boolean> =>\n gitSucceeds([...CONFIG_ARGS, ...args], { cwd: dir, env, debug });\n\n const write = async (path: string, contents: string | Uint8Array): Promise<void> => {\n const file = await resolveInside(dir, path);\n await mkdir(dirname(file), { recursive: true });\n await writeFile(file, contents);\n };\n\n const writeFiles = async (files: GitFixtureFiles): Promise<void> => {\n for (const [path, contents] of Object.entries(files)) {\n await write(path, contents);\n }\n };\n\n const cleanup = async (): Promise<void> => rm(dir, { recursive: true, force: true });\n\n return {\n dir,\n env,\n git: run,\n gitSucceeds: succeeds,\n path: (...segments: string[]): string => join(dir, ...segments),\n write,\n writeFiles,\n commit: async (message: string, files: GitFixtureFiles = {}): Promise<string> => {\n await writeFiles(files);\n await run(['add', '--all']);\n await run(['commit', '--quiet', '--allow-empty', '--message', message]);\n return run(['rev-parse', 'HEAD']);\n },\n cleanup,\n [Symbol.asyncDispose]: cleanup,\n };\n}\n\n/**\n * Resolves a path relative to a directory and ensures that it stays inside,\n * also through symbolic links.\n *\n * @param dir - The directory, with symbolic links resolved.\n * @param path - The relative path.\n * @returns The absolute path.\n * @throws {Error} if the path or the target of a symbolic link is outside the directory.\n */\nasync function resolveInside(dir: string, path: string): Promise<string> {\n const file = resolve(dir, path);\n const outside = new Error(`The path \"${path}\" is not a file path inside the repository \"${dir}\".`);\n\n if (!isInside(dir, file)) {\n throw outside;\n }\n\n // The deepest existing ancestor and an existing file may be symbolic links.\n let existing = file;\n while (\n !(await lstat(existing)\n .then(() => true)\n .catch(() => false))\n ) {\n existing = dirname(existing);\n }\n let target: string;\n try {\n target = await realpath(existing);\n } catch {\n throw outside;\n }\n\n if (target !== dir && !isInside(dir, target)) {\n throw outside;\n }\n\n return file;\n}\n\n/**\n * Reports whether a path is inside a directory, not the directory itself.\n *\n * @param dir - The directory.\n * @param path - The absolute path.\n * @returns `true` if the path is inside the directory.\n */\nfunction isInside(dir: string, path: string): boolean {\n const relativePath = relative(dir, path);\n return (\n relativePath !== '' && relativePath !== '..' && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath)\n );\n}\n"]}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Files to write, as paths relative to the repository root mapped to their
|
|
3
|
+
* contents. Parent directories are created as needed.
|
|
4
|
+
*/
|
|
5
|
+
export type GitFixtureFiles = Readonly<Record<string, string | Uint8Array>>;
|
|
6
|
+
/**
|
|
7
|
+
* Options for every fixture.
|
|
8
|
+
*/
|
|
9
|
+
export interface GitFixtureOptions {
|
|
10
|
+
/**
|
|
11
|
+
* The prefix of the temporary directory name.
|
|
12
|
+
*
|
|
13
|
+
* @default 'git-fixture-'
|
|
14
|
+
*/
|
|
15
|
+
prefix?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The author and committer date of all commits, as `Date` or in a format
|
|
18
|
+
* that Git accepts, such as ISO 8601. A fixed date makes commit hashes
|
|
19
|
+
* reproducible.
|
|
20
|
+
*
|
|
21
|
+
* @default '2000-01-01T00:00:00Z'
|
|
22
|
+
*/
|
|
23
|
+
date?: Date | string;
|
|
24
|
+
/**
|
|
25
|
+
* A callback for debug messages. It receives every Git command of the
|
|
26
|
+
* fixture before it runs.
|
|
27
|
+
*/
|
|
28
|
+
debug?: (message: string) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Options for creating a new repository.
|
|
32
|
+
*/
|
|
33
|
+
export interface CreateGitFixtureOptions extends GitFixtureOptions {
|
|
34
|
+
/**
|
|
35
|
+
* The name of the initial branch.
|
|
36
|
+
*
|
|
37
|
+
* @default 'main'
|
|
38
|
+
*/
|
|
39
|
+
initialBranch?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Files of an initial commit. Without files, the repository has no commits.
|
|
42
|
+
*/
|
|
43
|
+
files?: GitFixtureFiles;
|
|
44
|
+
/**
|
|
45
|
+
* The message of the initial commit.
|
|
46
|
+
*
|
|
47
|
+
* @default 'initial'
|
|
48
|
+
*/
|
|
49
|
+
message?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Options for cloning a repository.
|
|
53
|
+
*/
|
|
54
|
+
export interface CloneGitFixtureOptions extends GitFixtureOptions {
|
|
55
|
+
/**
|
|
56
|
+
* Additional arguments for `git clone`, such as `['--branch', 'feature']`.
|
|
57
|
+
* They are passed before the source.
|
|
58
|
+
*/
|
|
59
|
+
args?: readonly string[];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A temporary Git repository for tests.
|
|
63
|
+
*
|
|
64
|
+
* Remove it with `cleanup()`, or declare it with `await using` to remove it
|
|
65
|
+
* at the end of the scope.
|
|
66
|
+
*/
|
|
67
|
+
export interface GitFixture extends AsyncDisposable {
|
|
68
|
+
/**
|
|
69
|
+
* The absolute path of the repository, with symbolic links resolved.
|
|
70
|
+
*/
|
|
71
|
+
readonly dir: string;
|
|
72
|
+
/**
|
|
73
|
+
* The environment variables of the neutral configuration. Set them for the
|
|
74
|
+
* code under test, so that it runs Git like the fixture.
|
|
75
|
+
*/
|
|
76
|
+
readonly env: Readonly<Record<string, string>>;
|
|
77
|
+
/**
|
|
78
|
+
* Runs a Git command in the repository with the neutral configuration.
|
|
79
|
+
*
|
|
80
|
+
* @param args - The Git arguments.
|
|
81
|
+
* @returns The trimmed standard output.
|
|
82
|
+
* @throws {GitError} if the command fails.
|
|
83
|
+
*/
|
|
84
|
+
git: (args: readonly string[]) => Promise<string>;
|
|
85
|
+
/**
|
|
86
|
+
* Runs a Git command in the repository with the neutral configuration and
|
|
87
|
+
* reports whether it exited with code 0.
|
|
88
|
+
*
|
|
89
|
+
* @param args - The Git arguments.
|
|
90
|
+
* @returns `true` if the command exited with code 0, otherwise `false`.
|
|
91
|
+
* @throws {GitError} if Git cannot run.
|
|
92
|
+
*/
|
|
93
|
+
gitSucceeds: (args: readonly string[]) => Promise<boolean>;
|
|
94
|
+
/**
|
|
95
|
+
* Resolves a path in the repository.
|
|
96
|
+
*
|
|
97
|
+
* @param segments - Path segments relative to the repository root.
|
|
98
|
+
* @returns The absolute path.
|
|
99
|
+
*/
|
|
100
|
+
path: (...segments: string[]) => string;
|
|
101
|
+
/**
|
|
102
|
+
* Writes a file in the repository without staging it, creating parent
|
|
103
|
+
* directories as needed.
|
|
104
|
+
*
|
|
105
|
+
* @param path - The file path relative to the repository root.
|
|
106
|
+
* @param contents - The file contents.
|
|
107
|
+
* @throws {Error} if the path is outside the repository, also through symbolic links.
|
|
108
|
+
*/
|
|
109
|
+
write: (path: string, contents: string | Uint8Array) => Promise<void>;
|
|
110
|
+
/**
|
|
111
|
+
* Writes several files in the repository without staging them.
|
|
112
|
+
*
|
|
113
|
+
* @param files - The files to write.
|
|
114
|
+
* @throws {Error} if a path is outside the repository, also through symbolic links.
|
|
115
|
+
*/
|
|
116
|
+
writeFiles: (files: GitFixtureFiles) => Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Writes the files, stages all changes, and commits them. Without changes,
|
|
119
|
+
* the commit is empty.
|
|
120
|
+
*
|
|
121
|
+
* @param message - The commit message.
|
|
122
|
+
* @param files - Files to write before committing.
|
|
123
|
+
* @returns The hash of the new commit.
|
|
124
|
+
* @throws {GitError} if the commit fails.
|
|
125
|
+
*/
|
|
126
|
+
commit: (message: string, files?: GitFixtureFiles) => Promise<string>;
|
|
127
|
+
/**
|
|
128
|
+
* Removes the repository directory.
|
|
129
|
+
*/
|
|
130
|
+
cleanup: () => Promise<void>;
|
|
131
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git-fixture.types.js","sourceRoot":"","sources":["../src/git-fixture.types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Files to write, as paths relative to the repository root mapped to their\n * contents. Parent directories are created as needed.\n */\nexport type GitFixtureFiles = Readonly<Record<string, string | Uint8Array>>;\n\n/**\n * Options for every fixture.\n */\nexport interface GitFixtureOptions {\n /**\n * The prefix of the temporary directory name.\n *\n * @default 'git-fixture-'\n */\n prefix?: string;\n\n /**\n * The author and committer date of all commits, as `Date` or in a format\n * that Git accepts, such as ISO 8601. A fixed date makes commit hashes\n * reproducible.\n *\n * @default '2000-01-01T00:00:00Z'\n */\n date?: Date | string;\n\n /**\n * A callback for debug messages. It receives every Git command of the\n * fixture before it runs.\n */\n debug?: (message: string) => void;\n}\n\n/**\n * Options for creating a new repository.\n */\nexport interface CreateGitFixtureOptions extends GitFixtureOptions {\n /**\n * The name of the initial branch.\n *\n * @default 'main'\n */\n initialBranch?: string;\n\n /**\n * Files of an initial commit. Without files, the repository has no commits.\n */\n files?: GitFixtureFiles;\n\n /**\n * The message of the initial commit.\n *\n * @default 'initial'\n */\n message?: string;\n}\n\n/**\n * Options for cloning a repository.\n */\nexport interface CloneGitFixtureOptions extends GitFixtureOptions {\n /**\n * Additional arguments for `git clone`, such as `['--branch', 'feature']`.\n * They are passed before the source.\n */\n args?: readonly string[];\n}\n\n/**\n * A temporary Git repository for tests.\n *\n * Remove it with `cleanup()`, or declare it with `await using` to remove it\n * at the end of the scope.\n */\nexport interface GitFixture extends AsyncDisposable {\n /**\n * The absolute path of the repository, with symbolic links resolved.\n */\n readonly dir: string;\n\n /**\n * The environment variables of the neutral configuration. Set them for the\n * code under test, so that it runs Git like the fixture.\n */\n readonly env: Readonly<Record<string, string>>;\n\n /**\n * Runs a Git command in the repository with the neutral configuration.\n *\n * @param args - The Git arguments.\n * @returns The trimmed standard output.\n * @throws {GitError} if the command fails.\n */\n git: (args: readonly string[]) => Promise<string>;\n\n /**\n * Runs a Git command in the repository with the neutral configuration and\n * reports whether it exited with code 0.\n *\n * @param args - The Git arguments.\n * @returns `true` if the command exited with code 0, otherwise `false`.\n * @throws {GitError} if Git cannot run.\n */\n gitSucceeds: (args: readonly string[]) => Promise<boolean>;\n\n /**\n * Resolves a path in the repository.\n *\n * @param segments - Path segments relative to the repository root.\n * @returns The absolute path.\n */\n path: (...segments: string[]) => string;\n\n /**\n * Writes a file in the repository without staging it, creating parent\n * directories as needed.\n *\n * @param path - The file path relative to the repository root.\n * @param contents - The file contents.\n * @throws {Error} if the path is outside the repository, also through symbolic links.\n */\n write: (path: string, contents: string | Uint8Array) => Promise<void>;\n\n /**\n * Writes several files in the repository without staging them.\n *\n * @param files - The files to write.\n * @throws {Error} if a path is outside the repository, also through symbolic links.\n */\n writeFiles: (files: GitFixtureFiles) => Promise<void>;\n\n /**\n * Writes the files, stages all changes, and commits them. Without changes,\n * the commit is empty.\n *\n * @param message - The commit message.\n * @param files - Files to write before committing.\n * @returns The hash of the new commit.\n * @throws {GitError} if the commit fails.\n */\n commit: (message: string, files?: GitFixtureFiles) => Promise<string>;\n\n /**\n * Removes the repository directory.\n */\n cleanup: () => Promise<void>;\n}\n"]}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC","sourcesContent":["export * from './git-fixture.js';\nexport type * from './git-fixture.types.js';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simbo/git-fixture",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Create temporary Git repositories with a neutral configuration for tests.",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"git",
|
|
7
|
+
"fixture",
|
|
8
|
+
"test",
|
|
9
|
+
"testing",
|
|
10
|
+
"temporary",
|
|
11
|
+
"repository",
|
|
12
|
+
"clone",
|
|
13
|
+
"integration",
|
|
14
|
+
"utility",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"author": "Simon Lepel <simbo@simbo.de> (https://simbo.de/)",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"homepage": "https://github.com/simbo/packages/tree/main/packages/git-fixture",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/simbo/packages.git",
|
|
23
|
+
"directory": "packages/git-fixture"
|
|
24
|
+
},
|
|
25
|
+
"bugs": "https://github.com/simbo/packages/issues",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": "^24.15.0 || >=26.0.0"
|
|
28
|
+
},
|
|
29
|
+
"type": "module",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"CHANGELOG.md",
|
|
35
|
+
"dist",
|
|
36
|
+
"LICENSE.md",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@simbo/git-exec": "1.0.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@simbo/eslint-config": "5.0.0",
|
|
44
|
+
"@simbo/tsconfig": "3.0.1",
|
|
45
|
+
"@types/node": "^24.19.2",
|
|
46
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
47
|
+
"@vitest/ui": "^5.1.1",
|
|
48
|
+
"eslint": "^10.15.0",
|
|
49
|
+
"jiti": "^2.7.0",
|
|
50
|
+
"typescript": "^6.0.3",
|
|
51
|
+
"vitest": "^5.1.1"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc --project tsconfig.build.json",
|
|
55
|
+
"check": "pnpm run check:eslint && pnpm run check:types",
|
|
56
|
+
"check:eslint": "eslint --config ./eslint.config.ts --max-warnings 0",
|
|
57
|
+
"check:types": "tsc --noEmit",
|
|
58
|
+
"clean": "pnpm run clean:dist && pnpm run clean:coverage && pnpm run clean:turbo && pnpm run clean:node_modules",
|
|
59
|
+
"clean:dist": "rm -rf dist",
|
|
60
|
+
"clean:node_modules": "rm -rf node_modules",
|
|
61
|
+
"clean:turbo": "rm -rf .turbo",
|
|
62
|
+
"clean:coverage": "rm -rf coverage",
|
|
63
|
+
"fix:eslint": "pnpm run check:eslint --fix",
|
|
64
|
+
"test": "vitest run --coverage",
|
|
65
|
+
"test:ui": "pnpm run test:watch --ui",
|
|
66
|
+
"test:watch": "vitest dev --coverage"
|
|
67
|
+
}
|
|
6
68
|
}
|