test(perf): avoid real node startup in pre-commit hook integration

This commit is contained in:
Peter Steinberger
2026-03-02 12:41:51 +00:00
parent 7b38e8231e
commit 3980c315d1

View File

@@ -4,8 +4,12 @@ import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
const run = (cwd: string, cmd: string, args: string[] = []) => {
return execFileSync(cmd, args, { cwd, encoding: "utf8" }).trim();
const run = (cwd: string, cmd: string, args: string[] = [], env?: NodeJS.ProcessEnv) => {
return execFileSync(cmd, args, {
cwd,
encoding: "utf8",
env: env ? { ...process.env, ...env } : process.env,
}).trim();
};
describe("git-hooks/pre-commit (integration)", () => {
@@ -33,6 +37,12 @@ describe("git-hooks/pre-commit (integration)", () => {
"process.exit(0);\n",
"utf8",
);
const fakeBinDir = path.join(dir, "bin");
mkdirSync(fakeBinDir, { recursive: true });
writeFileSync(path.join(fakeBinDir, "node"), "#!/usr/bin/env bash\nexit 0\n", {
encoding: "utf8",
mode: 0o755,
});
// Create an untracked file that should NOT be staged by the hook.
writeFileSync(path.join(dir, "secret.txt"), "do-not-stage\n", "utf8");
@@ -42,7 +52,9 @@ describe("git-hooks/pre-commit (integration)", () => {
run(dir, "git", ["add", "--", "--all"]);
// Run the hook directly (same logic as when installed via core.hooksPath).
run(dir, "bash", ["git-hooks/pre-commit"]);
run(dir, "bash", ["git-hooks/pre-commit"], {
PATH: `${fakeBinDir}:${process.env.PATH ?? ""}`,
});
const staged = run(dir, "git", ["diff", "--cached", "--name-only"]).split("\n").filter(Boolean);
expect(staged).toEqual(["--all"]);