Skip to content

Commit 9c4972e

Browse files
test(config): run the hook install test against its own clone (#1800)
1 parent f3fc975 commit 9c4972e

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

scripts/enable-hooks.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const REPO_ROOT = resolve(import.meta.dirname, "..");
1111
const SCRIPT = join(REPO_ROOT, "scripts", "enable-hooks.ts");
1212
const repositories: string[] = [];
1313

14+
/**
15+
* Git exports `GIT_DIR` and its siblings to every hook process, and they outrank a child's `cwd`.
16+
* Inherited, they would point both the script under test and the assertions at the repository the
17+
* hook is running in rather than at the clone made below.
18+
*/
19+
const ENV = Object.fromEntries(
20+
Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_")),
21+
);
22+
1423
after(() => {
1524
for (const repository of repositories) rmSync(repository, { recursive: true, force: true });
1625
});
@@ -24,9 +33,7 @@ function clone(): { repository: string; git: (...args: string[]) => string } {
2433
cwd: repository,
2534
encoding: "utf8",
2635
maxBuffer: CAPTURE_LIMIT_BYTES,
27-
env: Object.fromEntries(
28-
Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_")),
29-
),
36+
env: ENV,
3037
}).trim();
3138
git("init", "--quiet");
3239
git("config", "core.hooksPath", ".husky/_");
@@ -40,10 +47,11 @@ void test("an install moves Git from an earlier hooks directory to the dispatche
4047
cwd: repository,
4148
encoding: "utf8",
4249
maxBuffer: CAPTURE_LIMIT_BYTES,
50+
env: ENV,
4351
});
4452
assert.equal(result.status, 0, `${result.stdout}${result.stderr}`);
4553
assert.equal(git("config", "core.hooksPath"), ".vite-hooks/_");
46-
const again = spawnSync("node", [SCRIPT], { cwd: repository, encoding: "utf8" });
54+
const again = spawnSync("node", [SCRIPT], { cwd: repository, encoding: "utf8", env: ENV });
4755
assert.equal(again.status, 0, `${again.stdout}${again.stderr}`);
4856
assert.equal(git("config", "core.hooksPath"), ".vite-hooks/_");
4957
});

0 commit comments

Comments
 (0)