test(scripts): share temp dir helpers

This commit is contained in:
Vincent Koc 2026-04-06 05:35:00 +01:00
parent b2cc5ab636
commit d77dbd699c
5 changed files with 51 additions and 64 deletions

View File

@ -1,27 +1,15 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import {
collectFilesSync,
isCodeFile,
relativeToCwd,
toPosixPath,
} from "../../scripts/check-file-utils.js";
import { createScriptTestHarness } from "./test-helpers.js";
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
function makeTempDir(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-check-file-utils-"));
tempDirs.push(dir);
return dir;
}
const { createTempDir } = createScriptTestHarness();
describe("scripts/check-file-utils isCodeFile", () => {
it("accepts source files and skips declarations", () => {
@ -33,7 +21,7 @@ describe("scripts/check-file-utils isCodeFile", () => {
describe("scripts/check-file-utils collectFilesSync", () => {
it("collects matching files while skipping common generated dirs", () => {
const rootDir = makeTempDir();
const rootDir = createTempDir("openclaw-check-file-utils-");
fs.mkdirSync(path.join(rootDir, "src", "nested"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "dist"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "docs", ".generated"), { recursive: true });
@ -53,7 +41,7 @@ describe("scripts/check-file-utils collectFilesSync", () => {
});
it("supports custom skipped directories", () => {
const rootDir = makeTempDir();
const rootDir = createTempDir("openclaw-check-file-utils-");
fs.mkdirSync(path.join(rootDir, "fixtures"), { recursive: true });
fs.mkdirSync(path.join(rootDir, "src"), { recursive: true });
fs.writeFileSync(path.join(rootDir, "fixtures", "skip.ts"), "");

View File

@ -1,27 +1,15 @@
import { execFileSync } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import {
findConflictMarkerLines,
findConflictMarkersInFiles,
listTrackedFiles,
} from "../../scripts/check-no-conflict-markers.mjs";
import { createScriptTestHarness } from "./test-helpers.js";
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
function makeTempDir(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-conflict-markers-"));
tempDirs.push(dir);
return dir;
}
const { createTempDir } = createScriptTestHarness();
function git(cwd: string, ...args: string[]): string {
return execFileSync("git", args, {
@ -55,7 +43,7 @@ describe("check-no-conflict-markers", () => {
});
it("scans text files and skips binary files", () => {
const rootDir = makeTempDir();
const rootDir = createTempDir("openclaw-conflict-markers-");
const textFile = path.join(rootDir, "CHANGELOG.md");
const binaryFile = path.join(rootDir, "image.png");
fs.writeFileSync(textFile, "<<<<<<< HEAD\nconflict\n>>>>>>> main\n");
@ -72,7 +60,7 @@ describe("check-no-conflict-markers", () => {
});
it("finds conflict markers in tracked script files", () => {
const rootDir = makeTempDir();
const rootDir = createTempDir("openclaw-conflict-markers-");
git(rootDir, "init", "-q");
git(rootDir, "config", "user.email", "test@example.com");
git(rootDir, "config", "user.name", "Test User");

View File

@ -1,26 +1,14 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import {
acquireLocalHeavyCheckLockSync,
applyLocalOxlintPolicy,
applyLocalTsgoPolicy,
} from "../../scripts/lib/local-heavy-check-runtime.mjs";
import { createScriptTestHarness } from "./test-helpers.js";
const tempDirs: string[] = [];
afterEach(() => {
for (const dir of tempDirs.splice(0)) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
function makeTempDir() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-local-heavy-check-"));
tempDirs.push(dir);
return dir;
}
const { createTempDir } = createScriptTestHarness();
function makeEnv(overrides: Record<string, string | undefined> = {}) {
return {
@ -61,7 +49,7 @@ describe("local-heavy-check-runtime", () => {
});
it("reclaims stale local heavy-check locks from dead pids", () => {
const cwd = makeTempDir();
const cwd = createTempDir("openclaw-local-heavy-check-");
const commonDir = path.join(cwd, ".git");
const lockDir = path.join(commonDir, "openclaw-local-checks", "heavy-check.lock");
fs.mkdirSync(lockDir, { recursive: true });

View File

@ -1,28 +1,18 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import {
buildVitestProfileCommand,
parseArgs,
resolveVitestProfileDir,
} from "../../scripts/run-vitest-profile.mjs";
import { createScriptTestHarness } from "./test-helpers.js";
describe("scripts/run-vitest-profile", () => {
const tempDirs: string[] = [];
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (dir) {
fs.rmSync(dir, { recursive: true, force: true });
}
}
});
const { trackTempDir } = createScriptTestHarness();
it("defaults profile output outside the repo", () => {
const outputDir = resolveVitestProfileDir({ mode: "main", outputDir: "" });
tempDirs.push(outputDir);
const outputDir = trackTempDir(resolveVitestProfileDir({ mode: "main", outputDir: "" }));
expect(outputDir.startsWith(os.tmpdir())).toBe(true);
expect(outputDir.startsWith(process.cwd())).toBe(false);

View File

@ -0,0 +1,33 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach } from "vitest";
export function createScriptTestHarness() {
const tempDirs: string[] = [];
afterEach(() => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (dir) {
fs.rmSync(dir, { recursive: true, force: true });
}
}
});
function createTempDir(prefix: string): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
tempDirs.push(dir);
return dir;
}
function trackTempDir(dir: string): string {
tempDirs.push(dir);
return dir;
}
return {
createTempDir,
trackTempDir,
};
}