mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 04:47:03 -06:00
test: read staged temp helper source from index
This commit is contained in:
@@ -143,6 +143,33 @@ function readDiff(args, cwd = process.cwd()) {
|
||||
});
|
||||
}
|
||||
|
||||
function readWorktreeSource(filePath, cwd) {
|
||||
try {
|
||||
return fs.readFileSync(path.join(cwd, filePath), "utf8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function readStagedSource(filePath, cwd) {
|
||||
try {
|
||||
return execFileSync("git", ["show", `:${filePath}`], {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
maxBuffer: 64 * 1024 * 1024,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function readSourceForDiff(filePath, args, cwd) {
|
||||
// Staged checks must parse the index blob. Reading the worktree mixes in
|
||||
// unstaged edits and can warn on code that will not be committed.
|
||||
return args.staged ? readStagedSource(filePath, cwd) : readWorktreeSource(filePath, cwd);
|
||||
}
|
||||
|
||||
function stripKnownExtension(filePath) {
|
||||
return filePath.replace(/\.(?:c|m)?[jt]sx?$/u, "");
|
||||
}
|
||||
@@ -419,13 +446,10 @@ export async function main(argv, io) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const findings = collectTempCreationFindingsFromDiff(readDiff(args), {
|
||||
const cwd = process.cwd();
|
||||
const findings = collectTempCreationFindingsFromDiff(readDiff(args, cwd), {
|
||||
readFile(filePath) {
|
||||
try {
|
||||
return fs.readFileSync(path.join(process.cwd(), filePath), "utf8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
return readSourceForDiff(filePath, args, cwd);
|
||||
},
|
||||
});
|
||||
if (args.json) {
|
||||
|
||||
@@ -116,11 +116,11 @@ describe("report-test-temp-creations", () => {
|
||||
|
||||
it("reports repository-observed mkdtemp call forms", () => {
|
||||
const sources = [
|
||||
'const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), "case-"));',
|
||||
'const root = await fs.mkdtemp(path.join(os.tmpdir(), "case-"));',
|
||||
'const root = await fsPromises.mkdtemp("/tmp/openclaw-case-");',
|
||||
'const root = await mkdtemp(path.join(tmpdir(), "case-"));',
|
||||
'const root = mkdtempSync(join(tmpdir(), "case-"));',
|
||||
["const root = await fs.promises.", "mkdtemp", '(path.join(os.tmpdir(), "case-"));'].join(""),
|
||||
["const root = await fs.", "mkdtemp", '(path.join(os.tmpdir(), "case-"));'].join(""),
|
||||
["const root = await fsPromises.", "mkdtemp", '("/tmp/openclaw-case-");'].join(""),
|
||||
["const root = await ", "mkdtemp", '(path.join(tmpdir(), "case-"));'].join(""),
|
||||
["const root = ", "mkdtemp", 'Sync(join(tmpdir(), "case-"));'].join(""),
|
||||
];
|
||||
const diff = [
|
||||
"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", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("reads staged source for manual helper scans", () => {
|
||||
const root = tempDirs.make("openclaw-temp-report-staged-source-");
|
||||
const env = createNestedGitEnv();
|
||||
execFileSync("git", ["init", "-q", "--initial-branch=main"], { cwd: root, env });
|
||||
execFileSync(
|
||||
"git",
|
||||
[
|
||||
"-c",
|
||||
"user.email=test@example.com",
|
||||
"-c",
|
||||
"user.name=Test User",
|
||||
"commit",
|
||||
"--allow-empty",
|
||||
"-q",
|
||||
"-m",
|
||||
"initial",
|
||||
],
|
||||
{ cwd: root, env },
|
||||
);
|
||||
|
||||
fs.mkdirSync(path.join(root, "test", "scripts"), { recursive: true });
|
||||
const stagedManualFile = path.join(root, "test", "scripts", "staged-manual.test.ts");
|
||||
const stagedAutoFile = path.join(root, "test", "scripts", "staged-auto.test.ts");
|
||||
const manualSource = [
|
||||
'import { makeTempDir } from "../helpers/temp-dir.js";',
|
||||
"const tempDirs = new Set<string>();",
|
||||
'const workspace = makeTempDir(tempDirs, "case-");',
|
||||
].join("\n");
|
||||
const autoSource = [
|
||||
'import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";',
|
||||
"const tempDirs = useAutoCleanupTempDirTracker();",
|
||||
'const workspace = tempDirs.make("case-");',
|
||||
].join("\n");
|
||||
fs.writeFileSync(stagedManualFile, `${manualSource}\n`, "utf8");
|
||||
fs.writeFileSync(stagedAutoFile, `${autoSource}\n`, "utf8");
|
||||
execFileSync("git", ["add", "test/scripts"], { cwd: root, env });
|
||||
fs.writeFileSync(stagedManualFile, `${autoSource}\n`, "utf8");
|
||||
fs.writeFileSync(stagedAutoFile, `${manualSource}\n`, "utf8");
|
||||
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[path.join(repoRoot, "scripts", "report-test-temp-creations.mjs"), "--staged", "--json"],
|
||||
{
|
||||
cwd: root,
|
||||
encoding: "utf8",
|
||||
env,
|
||||
},
|
||||
);
|
||||
|
||||
expect(result.status).toBe(0);
|
||||
expect(JSON.parse(result.stdout)).toEqual([
|
||||
{
|
||||
file: "test/scripts/staged-manual.test.ts",
|
||||
line: 1,
|
||||
reason: "new manual temp-dir helper import",
|
||||
source: 'import { makeTempDir } from "../helpers/temp-dir.js";',
|
||||
},
|
||||
{
|
||||
file: "test/scripts/staged-manual.test.ts",
|
||||
line: 3,
|
||||
reason: "new manual temp-dir helper usage",
|
||||
source: 'const workspace = makeTempDir(tempDirs, "case-");',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("exits non-zero for staged findings when requested", () => {
|
||||
const root = tempDirs.make("openclaw-temp-report-");
|
||||
const env = createNestedGitEnv();
|
||||
|
||||
Reference in New Issue
Block a user