Skip to content

Commit f88ce40

Browse files
vincentkoctzy-17
andcommitted
fix(test): bound shared git inventory lookup
Co-authored-by: 唐梓夷0668001293 <[email protected]>
1 parent ffcca75 commit f88ce40

6 files changed

Lines changed: 84 additions & 15 deletions

File tree

‎src/plugins/compat/registry.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ function expectNonEmptyStringList(values: readonly string[], label: string) {
5757
}
5858

5959
function listTrackedSourceFiles(): string[] {
60-
return (listGitTrackedFiles({ pathspecs: sourceRootsForDeprecatedCallGuard }) ?? []).filter(
61-
(file) => /\.(?:ts|tsx|mts|cts)$/u.test(file),
62-
);
60+
const files = listGitTrackedFiles({ pathspecs: sourceRootsForDeprecatedCallGuard });
61+
if (!files) {
62+
throw new Error("unable to list tracked source files for the deprecated-call guard");
63+
}
64+
return files.filter((file) => /\.(?:ts|tsx|mts|cts)$/u.test(file));
6365
}
6466

6567
describe("plugin compatibility registry", () => {

‎src/plugins/contracts/config-footprint-guardrails.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ describe("config footprint guardrails", () => {
257257
]);
258258
const facadeImportPattern =
259259
/\bfrom\s*["'][^"']*(?:channel-config-primitives|bundled-channel-config-schema)(?:\.js)?["']/u;
260-
const files = listGitTrackedFiles({ repoRoot: REPO_ROOT, pathspecs: "src" }) ?? [];
260+
const files = listGitTrackedFiles({ repoRoot: REPO_ROOT, pathspecs: "src" });
261+
if (!files) {
262+
throw new Error("unable to list tracked source files for the config facade guard");
263+
}
261264

262265
const offenders = files.filter((file) => {
263266
if (!(file.endsWith(".ts") || file.endsWith(".tsx")) || allowedShellImporters.has(file)) {

‎src/plugins/contracts/model-catalog-core-imports.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ const LEGACY_MODEL_CATALOG_BRIDGES = new Map([
3434
]);
3535

3636
function listSourceFiles(): string[] {
37-
return (
38-
listGitTrackedFiles({
39-
repoRoot: REPO_ROOT,
40-
pathspecs: ["src", "extensions", "packages", "test"],
41-
}) ?? []
42-
)
37+
const files = listGitTrackedFiles({
38+
repoRoot: REPO_ROOT,
39+
pathspecs: ["src", "extensions", "packages", "test"],
40+
});
41+
if (!files) {
42+
throw new Error("unable to list tracked source files for the model-catalog import guard");
43+
}
44+
return files
4345
.filter((file) => /\.(?:[cm]?ts|tsx|mts|cts)$/u.test(file))
4446
.filter((file) => fs.existsSync(path.join(REPO_ROOT, file)));
4547
}

‎src/plugins/contracts/plugin-sdk-package-contract-guardrails.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,13 @@ function collectNewDeprecatedMemoryEmbeddingProviderApiFiles(): string[] {
465465

466466
function collectNewDeprecatedMemoryEmbeddingProviderManifestFiles(): string[] {
467467
const files: string[] = [];
468-
const manifestFiles =
469-
listGitTrackedFiles({
470-
repoRoot: REPO_ROOT,
471-
pathspecs: "extensions/**/openclaw.plugin.json",
472-
}) ?? [];
468+
const manifestFiles = listGitTrackedFiles({
469+
repoRoot: REPO_ROOT,
470+
pathspecs: "extensions/**/openclaw.plugin.json",
471+
});
472+
if (!manifestFiles) {
473+
throw new Error("unable to list plugin manifests for the deprecated manifest guard");
474+
}
473475
for (const repoRelativePath of manifestFiles) {
474476
const source = fs.readFileSync(resolve(REPO_ROOT, repoRelativePath), "utf8");
475477
if (

‎src/test-utils/repo-files.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
4+
import { withTempDir } from "./temp-dir.js";
5+
6+
const spawnSyncMock = vi.hoisted(() => vi.fn());
7+
8+
vi.mock("node:child_process", () => ({
9+
spawnSync: spawnSyncMock,
10+
}));
11+
12+
import { listGitTrackedFiles } from "./repo-files.js";
13+
14+
describe("listGitTrackedFiles", () => {
15+
beforeEach(() => {
16+
spawnSyncMock.mockReset();
17+
});
18+
19+
it("bounds Git inventory lookup and returns sorted existing files", async () => {
20+
await withTempDir("openclaw-repo-files-", async (repoRoot) => {
21+
await fs.writeFile(path.join(repoRoot, "z.ts"), "");
22+
await fs.writeFile(path.join(repoRoot, "a.ts"), "");
23+
spawnSyncMock.mockReturnValue({
24+
status: 0,
25+
stdout: "z.ts\nmissing.ts\na.ts\n",
26+
});
27+
28+
expect(listGitTrackedFiles({ repoRoot, pathspecs: "src" })).toEqual(["a.ts", "z.ts"]);
29+
expect(spawnSyncMock).toHaveBeenCalledWith(
30+
"git",
31+
["ls-files", "--", "src"],
32+
expect.objectContaining({
33+
cwd: repoRoot,
34+
killSignal: "SIGKILL",
35+
timeout: 5_000,
36+
}),
37+
);
38+
});
39+
});
40+
41+
it("caches a timed-out inventory lookup as unavailable", async () => {
42+
await withTempDir("openclaw-repo-files-timeout-", async (repoRoot) => {
43+
spawnSyncMock.mockReturnValue({
44+
error: Object.assign(new Error("spawnSync git ETIMEDOUT"), { code: "ETIMEDOUT" }),
45+
signal: "SIGKILL",
46+
status: null,
47+
stdout: "",
48+
});
49+
const params = { repoRoot, pathspecs: ["src", "extensions"] };
50+
51+
expect(listGitTrackedFiles(params)).toBeNull();
52+
expect(listGitTrackedFiles(params)).toBeNull();
53+
expect(spawnSyncMock).toHaveBeenCalledOnce();
54+
});
55+
});
56+
});

‎src/test-utils/repo-files.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { spawnSync } from "node:child_process";
33
import fs from "node:fs";
44
import path from "node:path";
55

6+
const GIT_LS_FILES_TIMEOUT_MS = 5_000;
67
const gitTrackedFilesCache = new Map<string, string[] | null>();
78

89
function filterExistingRepoFiles(repoRoot: string, files: readonly string[]): string[] {
@@ -36,8 +37,11 @@ export function listGitTrackedFiles(params: {
3637
const result = spawnSync("git", ["ls-files", "--", ...pathspecs], {
3738
cwd: repoRoot,
3839
encoding: "utf8",
40+
// Bound repository scans; SIGKILL avoids waiting on a hung Git process after timeout.
41+
killSignal: "SIGKILL",
3942
maxBuffer: 16 * 1024 * 1024,
4043
stdio: ["ignore", "pipe", "ignore"],
44+
timeout: GIT_LS_FILES_TIMEOUT_MS,
4145
});
4246
if (result.status !== 0) {
4347
gitTrackedFilesCache.set(cacheKey, null);

0 commit comments

Comments
 (0)