test(perf): speed up pre-commit integration setup

This commit is contained in:
Peter Steinberger
2026-03-02 11:36:16 +00:00
parent 43bffe7bdc
commit 316875582a

View File

@@ -1,6 +1,5 @@
import { execFileSync } from "node:child_process"; import { execFileSync } from "node:child_process";
import { chmodSync, copyFileSync } from "node:fs"; import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from "node:fs";
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
@@ -10,33 +9,31 @@ const run = (cwd: string, cmd: string, args: string[] = []) => {
}; };
describe("git-hooks/pre-commit (integration)", () => { describe("git-hooks/pre-commit (integration)", () => {
it("does not treat staged filenames as git-add flags (e.g. --all)", async () => { it("does not treat staged filenames as git-add flags (e.g. --all)", () => {
const dir = await mkdtemp(path.join(os.tmpdir(), "openclaw-pre-commit-")); const dir = mkdtempSync(path.join(os.tmpdir(), "openclaw-pre-commit-"));
run(dir, "git", ["init", "-q"]); run(dir, "git", ["init", "-q"]);
// Copy the hook + helpers so the test exercises real on-disk wiring. // Copy the hook + helpers so the test exercises real on-disk wiring.
await mkdir(path.join(dir, "git-hooks"), { recursive: true }); mkdirSync(path.join(dir, "git-hooks"), { recursive: true });
await mkdir(path.join(dir, "scripts", "pre-commit"), { recursive: true }); mkdirSync(path.join(dir, "scripts", "pre-commit"), { recursive: true });
copyFileSync( symlinkSync(
path.join(process.cwd(), "git-hooks", "pre-commit"), path.join(process.cwd(), "git-hooks", "pre-commit"),
path.join(dir, "git-hooks", "pre-commit"), path.join(dir, "git-hooks", "pre-commit"),
); );
copyFileSync( symlinkSync(
path.join(process.cwd(), "scripts", "pre-commit", "run-node-tool.sh"), path.join(process.cwd(), "scripts", "pre-commit", "run-node-tool.sh"),
path.join(dir, "scripts", "pre-commit", "run-node-tool.sh"), path.join(dir, "scripts", "pre-commit", "run-node-tool.sh"),
); );
copyFileSync( symlinkSync(
path.join(process.cwd(), "scripts", "pre-commit", "filter-staged-files.mjs"), path.join(process.cwd(), "scripts", "pre-commit", "filter-staged-files.mjs"),
path.join(dir, "scripts", "pre-commit", "filter-staged-files.mjs"), path.join(dir, "scripts", "pre-commit", "filter-staged-files.mjs"),
); );
chmodSync(path.join(dir, "git-hooks", "pre-commit"), 0o755);
chmodSync(path.join(dir, "scripts", "pre-commit", "run-node-tool.sh"), 0o755);
// Create an untracked file that should NOT be staged by the hook. // Create an untracked file that should NOT be staged by the hook.
await writeFile(path.join(dir, "secret.txt"), "do-not-stage\n"); writeFileSync(path.join(dir, "secret.txt"), "do-not-stage\n", "utf8");
// Stage a maliciously-named file. Older hooks using `xargs git add` could run `git add --all`. // Stage a maliciously-named file. Older hooks using `xargs git add` could run `git add --all`.
await writeFile(path.join(dir, "--all"), "flag\n"); writeFileSync(path.join(dir, "--all"), "flag\n", "utf8");
run(dir, "git", ["add", "--", "--all"]); run(dir, "git", ["add", "--", "--all"]);
// Run the hook directly (same logic as when installed via core.hooksPath). // Run the hook directly (same logic as when installed via core.hooksPath).