Skip to content

Commit 1fdd7d2

Browse files
committed
test: read staged temp helper source from index
1 parent 117b2bc commit 1fdd7d2

2 files changed

Lines changed: 101 additions & 11 deletions

File tree

scripts/report-test-temp-creations.mjs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,33 @@ function readDiff(args, cwd = process.cwd()) {
143143
});
144144
}
145145

146+
function readWorktreeSource(filePath, cwd) {
147+
try {
148+
return fs.readFileSync(path.join(cwd, filePath), "utf8");
149+
} catch {
150+
return "";
151+
}
152+
}
153+
154+
function readStagedSource(filePath, cwd) {
155+
try {
156+
return execFileSync("git", ["show", `:${filePath}`], {
157+
cwd,
158+
encoding: "utf8",
159+
maxBuffer: 64 * 1024 * 1024,
160+
stdio: ["ignore", "pipe", "pipe"],
161+
});
162+
} catch {
163+
return "";
164+
}
165+
}
166+
167+
function readSourceForDiff(filePath, args, cwd) {
168+
// Staged checks must parse the index blob. Reading the worktree mixes in
169+
// unstaged edits and can warn on code that will not be committed.
170+
return args.staged ? readStagedSource(filePath, cwd) : readWorktreeSource(filePath, cwd);
171+
}
172+
146173
function stripKnownExtension(filePath) {
147174
return filePath.replace(/\.(?:c|m)?[jt]sx?$/u, "");
148175
}
@@ -419,13 +446,10 @@ export async function main(argv, io) {
419446
return 0;
420447
}
421448

422-
const findings = collectTempCreationFindingsFromDiff(readDiff(args), {
449+
const cwd = process.cwd();
450+
const findings = collectTempCreationFindingsFromDiff(readDiff(args, cwd), {
423451
readFile(filePath) {
424-
try {
425-
return fs.readFileSync(path.join(process.cwd(), filePath), "utf8");
426-
} catch {
427-
return "";
428-
}
452+
return readSourceForDiff(filePath, args, cwd);
429453
},
430454
});
431455
if (args.json) {

test/scripts/report-test-temp-creations.test.ts

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ describe("report-test-temp-creations", () => {
116116

117117
it("reports repository-observed mkdtemp call forms", () => {
118118
const sources = [
119-
'const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "case-"));',
120-
'const root = await fs.mkdtemp(path.join(os.tmpdir(), "case-"));',
121-
'const root = await fsPromises.mkdtemp("/tmp/openclaw-case-");',
122-
'const root = await mkdtemp(path.join(tmpdir(), "case-"));',
123-
'const root = mkdtempSync(join(tmpdir(), "case-"));',
119+
["const root = await fs.promises.", "mkdtemp", '(path.join(os.tmpdir(), "case-"));'].join(""),
120+
["const root = await fs.", "mkdtemp", '(path.join(os.tmpdir(), "case-"));'].join(""),
121+
["const root = await fsPromises.", "mkdtemp", '("/tmp/openclaw-case-");'].join(""),
122+
["const root = await ", "mkdtemp", '(path.join(tmpdir(), "case-"));'].join(""),
123+
["const root = ", "mkdtemp", 'Sync(join(tmpdir(), "case-"));'].join(""),
124124
];
125125
const diff = [
126126
"diff --git a/test/scripts/temp-patterns.test.ts b/test/scripts/temp-patterns.test.ts",
@@ -394,6 +394,72 @@ describe("report-test-temp-creations", () => {
394394
);
395395
});
396396

397+
it("reads staged source for manual helper scans", () => {
398+
const root = tempDirs.make("openclaw-temp-report-staged-source-");
399+
const env = createNestedGitEnv();
400+
execFileSync("git", ["init", "-q", "--initial-branch=main"], { cwd: root, env });
401+
execFileSync(
402+
"git",
403+
[
404+
"-c",
405+
406+
"-c",
407+
"user.name=Test User",
408+
"commit",
409+
"--allow-empty",
410+
"-q",
411+
"-m",
412+
"initial",
413+
],
414+
{ cwd: root, env },
415+
);
416+
417+
fs.mkdirSync(path.join(root, "test", "scripts"), { recursive: true });
418+
const stagedManualFile = path.join(root, "test", "scripts", "staged-manual.test.ts");
419+
const stagedAutoFile = path.join(root, "test", "scripts", "staged-auto.test.ts");
420+
const manualSource = [
421+
'import { makeTempDir } from "../helpers/temp-dir.js";',
422+
"const tempDirs = new Set<string>();",
423+
'const workspace = makeTempDir(tempDirs, "case-");',
424+
].join("\n");
425+
const autoSource = [
426+
'import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";',
427+
"const tempDirs = useAutoCleanupTempDirTracker();",
428+
'const workspace = tempDirs.make("case-");',
429+
].join("\n");
430+
fs.writeFileSync(stagedManualFile, `${manualSource}\n`, "utf8");
431+
fs.writeFileSync(stagedAutoFile, `${autoSource}\n`, "utf8");
432+
execFileSync("git", ["add", "test/scripts"], { cwd: root, env });
433+
fs.writeFileSync(stagedManualFile, `${autoSource}\n`, "utf8");
434+
fs.writeFileSync(stagedAutoFile, `${manualSource}\n`, "utf8");
435+
436+
const result = spawnSync(
437+
process.execPath,
438+
[path.join(repoRoot, "scripts", "report-test-temp-creations.mjs"), "--staged", "--json"],
439+
{
440+
cwd: root,
441+
encoding: "utf8",
442+
env,
443+
},
444+
);
445+
446+
expect(result.status).toBe(0);
447+
expect(JSON.parse(result.stdout)).toEqual([
448+
{
449+
file: "test/scripts/staged-manual.test.ts",
450+
line: 1,
451+
reason: "new manual temp-dir helper import",
452+
source: 'import { makeTempDir } from "../helpers/temp-dir.js";',
453+
},
454+
{
455+
file: "test/scripts/staged-manual.test.ts",
456+
line: 3,
457+
reason: "new manual temp-dir helper usage",
458+
source: 'const workspace = makeTempDir(tempDirs, "case-");',
459+
},
460+
]);
461+
});
462+
397463
it("exits non-zero for staged findings when requested", () => {
398464
const root = tempDirs.make("openclaw-temp-report-");
399465
const env = createNestedGitEnv();

0 commit comments

Comments
 (0)