From 3980c315d1306c45cdcd9cfc2aa030477c340f48 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 2 Mar 2026 12:41:51 +0000 Subject: [PATCH] test(perf): avoid real node startup in pre-commit hook integration --- test/git-hooks-pre-commit.test.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/git-hooks-pre-commit.test.ts b/test/git-hooks-pre-commit.test.ts index de61a1abd7c..6e74aaa4d8a 100644 --- a/test/git-hooks-pre-commit.test.ts +++ b/test/git-hooks-pre-commit.test.ts @@ -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"]);