forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-hooks-pre-commit.test.ts
More file actions
165 lines (137 loc) 路 5.85 KB
/
Copy pathgit-hooks-pre-commit.test.ts
File metadata and controls
165 lines (137 loc) 路 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Git hook tests validate pre-commit hook behavior and scripts.
import { execFileSync } from "node:child_process";
import { mkdirSync, symlinkSync, writeFileSync } from "node:fs";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { cleanupTempDirs, makeTempRepoRoot } from "./helpers/temp-repo.js";
const baseGitEnv = {
GIT_CONFIG_NOSYSTEM: "1",
GIT_TERMINAL_PROMPT: "0",
};
const baseRunEnv: NodeJS.ProcessEnv = { ...process.env, ...baseGitEnv };
const tempDirs: string[] = [];
const run = (cwd: string, cmd: string, args: string[] = [], env?: NodeJS.ProcessEnv) => {
return execFileSync(cmd, args, {
cwd,
encoding: "utf8",
env: env ? { ...baseRunEnv, ...env } : baseRunEnv,
}).trim();
};
function writeExecutable(dir: string, name: string, contents: string): void {
writeFileSync(path.join(dir, name), contents, {
encoding: "utf8",
mode: 0o755,
});
}
function installPreCommitFixture(dir: string): string {
mkdirSync(path.join(dir, "git-hooks"), { recursive: true });
mkdirSync(path.join(dir, "scripts", "pre-commit"), { recursive: true });
symlinkSync(
path.join(process.cwd(), "git-hooks", "pre-commit"),
path.join(dir, "git-hooks", "pre-commit"),
);
writeFileSync(
path.join(dir, "scripts", "pre-commit", "run-node-tool.sh"),
"#!/usr/bin/env bash\nexit 0\n",
{
encoding: "utf8",
mode: 0o755,
},
);
writeFileSync(
path.join(dir, "scripts", "pre-commit", "filter-staged-files.mjs"),
"process.exit(0);\n",
"utf8",
);
const fakeBinDir = path.join(dir, "bin");
mkdirSync(fakeBinDir, { recursive: true });
writeExecutable(fakeBinDir, "node", "#!/usr/bin/env bash\nexit 0\n");
return fakeBinDir;
}
function splitNonEmptyLines(output: string): string[] {
const lines: string[] = [];
for (const line of output.split("\n")) {
if (line) {
lines.push(line);
}
}
return lines;
}
afterEach(() => {
cleanupTempDirs(tempDirs);
});
describe("git-hooks/pre-commit (integration)", () => {
it("does not treat staged filenames as git-add flags (e.g. --all)", () => {
const dir = makeTempRepoRoot(tempDirs, "openclaw-pre-commit-");
run(dir, "git", ["init", "-q", "--initial-branch=main"]);
// Use the real hook script and lightweight helper stubs.
const fakeBinDir = installPreCommitFixture(dir);
// Create an untracked file that should NOT be staged by the hook.
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`.
writeFileSync(path.join(dir, "--all"), "flag\n", "utf8");
run(dir, "git", ["add", "--", "--all"]);
// Run the hook directly (same logic as when installed via core.hooksPath).
run(dir, "bash", ["git-hooks/pre-commit"], {
PATH: `${fakeBinDir}:${process.env.PATH ?? ""}`,
});
const staged = splitNonEmptyLines(run(dir, "git", ["diff", "--cached", "--name-only"]));
expect(staged).toEqual(["--all"]);
});
it("does not run the changed-scope check for non-doc staged changes", () => {
const dir = makeTempRepoRoot(tempDirs, "openclaw-pre-commit-no-check-changed-");
run(dir, "git", ["init", "-q", "--initial-branch=main"]);
const fakeBinDir = installPreCommitFixture(dir);
writeFileSync(path.join(dir, "package.json"), '{"name":"tmp"}\n', "utf8");
writeFileSync(path.join(dir, "pnpm-lock.yaml"), "lockfileVersion: '9.0'\n", "utf8");
writeExecutable(
fakeBinDir,
"pnpm",
"#!/usr/bin/env bash\necho 'pnpm should not run from pre-commit' >&2\nexit 99\n",
);
writeFileSync(path.join(dir, "tracked.txt"), "hello\n", "utf8");
run(dir, "git", ["add", "--", "tracked.txt"]);
run(dir, "bash", ["git-hooks/pre-commit"], {
PATH: `${fakeBinDir}:${process.env.PATH ?? ""}`,
});
expect(run(dir, "git", ["diff", "--cached", "--name-only"])).toBe("tracked.txt");
});
it("does not re-add staged paths that are ignored by the current .gitignore", () => {
const dir = makeTempRepoRoot(tempDirs, "openclaw-pre-commit-ignored-staged-");
run(dir, "git", ["init", "-q", "--initial-branch=main"]);
const fakeBinDir = installPreCommitFixture(dir);
mkdirSync(path.join(dir, ".agents", "skills", "discord-clawd"), { recursive: true });
writeFileSync(path.join(dir, ".gitignore"), ".agents/skills/discord-clawd/\n", "utf8");
writeFileSync(
path.join(dir, ".agents", "skills", "discord-clawd", "SKILL.md"),
"# Discord Clawd\n",
"utf8",
);
run(dir, "git", ["add", "--", ".gitignore"]);
run(dir, "git", ["add", "-f", "--", ".agents/skills/discord-clawd/SKILL.md"]);
run(dir, "bash", ["git-hooks/pre-commit"], {
PATH: `${fakeBinDir}:${process.env.PATH ?? ""}`,
});
const staged = splitNonEmptyLines(run(dir, "git", ["diff", "--cached", "--name-only"]));
expect(staged).toEqual([".agents/skills/discord-clawd/SKILL.md", ".gitignore"]);
});
it("ignores FAST_COMMIT because the hook is already formatting-only", () => {
const dir = makeTempRepoRoot(tempDirs, "openclaw-pre-commit-fast-");
run(dir, "git", ["init", "-q", "--initial-branch=main"]);
const fakeBinDir = installPreCommitFixture(dir);
writeFileSync(path.join(dir, "package.json"), '{"name":"tmp"}\n', "utf8");
writeFileSync(path.join(dir, "pnpm-lock.yaml"), "lockfileVersion: '9.0'\n", "utf8");
writeExecutable(
fakeBinDir,
"pnpm",
"#!/usr/bin/env bash\necho 'pnpm should not run when FAST_COMMIT is enabled' >&2\nexit 99\n",
);
writeFileSync(path.join(dir, "tracked.txt"), "hello\n", "utf8");
run(dir, "git", ["add", "--", "tracked.txt"]);
run(dir, "bash", ["git-hooks/pre-commit"], {
PATH: `${fakeBinDir}:${process.env.PATH ?? ""}`,
FAST_COMMIT: "1",
});
expect(run(dir, "git", ["diff", "--cached", "--name-only"])).toBe("tracked.txt");
});
});