From d77dbd699c69953e427c8d1fc5126780f6eb19ba Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 6 Apr 2026 05:35:00 +0100 Subject: [PATCH] test(scripts): share temp dir helpers --- test/scripts/check-file-utils.test.ts | 22 +++---------- .../scripts/check-no-conflict-markers.test.ts | 22 +++---------- .../scripts/local-heavy-check-runtime.test.ts | 20 +++-------- test/scripts/run-vitest-profile.test.ts | 18 +++------- test/scripts/test-helpers.ts | 33 +++++++++++++++++++ 5 files changed, 51 insertions(+), 64 deletions(-) create mode 100644 test/scripts/test-helpers.ts diff --git a/test/scripts/check-file-utils.test.ts b/test/scripts/check-file-utils.test.ts index 1ce47b6abb8..377e183f876 100644 --- a/test/scripts/check-file-utils.test.ts +++ b/test/scripts/check-file-utils.test.ts @@ -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"), ""); diff --git a/test/scripts/check-no-conflict-markers.test.ts b/test/scripts/check-no-conflict-markers.test.ts index 9bb656eb1c3..922ef17ecab 100644 --- a/test/scripts/check-no-conflict-markers.test.ts +++ b/test/scripts/check-no-conflict-markers.test.ts @@ -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"); diff --git a/test/scripts/local-heavy-check-runtime.test.ts b/test/scripts/local-heavy-check-runtime.test.ts index 5f599b803fb..456d3be1636 100644 --- a/test/scripts/local-heavy-check-runtime.test.ts +++ b/test/scripts/local-heavy-check-runtime.test.ts @@ -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 = {}) { 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 }); diff --git a/test/scripts/run-vitest-profile.test.ts b/test/scripts/run-vitest-profile.test.ts index 8d9b31ea71c..8eefd0d2817 100644 --- a/test/scripts/run-vitest-profile.test.ts +++ b/test/scripts/run-vitest-profile.test.ts @@ -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); diff --git a/test/scripts/test-helpers.ts b/test/scripts/test-helpers.ts new file mode 100644 index 00000000000..07e8ea608f3 --- /dev/null +++ b/test/scripts/test-helpers.ts @@ -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, + }; +}