From ce50b97c866154608c6ffa2f50b39ad7dc235803 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 6 Apr 2026 05:42:40 +0100 Subject: [PATCH] test(root): share temp dir helper across root tests --- test/helpers/temp-dir.ts | 23 +++++++++++++++++++++++ test/scripts/ios-team-id.test.ts | 31 ++++++++++++++++++------------- test/test-env.test.ts | 11 +++-------- 3 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 test/helpers/temp-dir.ts diff --git a/test/helpers/temp-dir.ts b/test/helpers/temp-dir.ts new file mode 100644 index 00000000000..578321c2219 --- /dev/null +++ b/test/helpers/temp-dir.ts @@ -0,0 +1,23 @@ +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +export function makeTempDir(tempDirs: string[] | Set, prefix: string): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + if (Array.isArray(tempDirs)) { + tempDirs.push(dir); + } else { + tempDirs.add(dir); + } + return dir; +} + +export function cleanupTempDirs(tempDirs: string[] | Set): void { + const dirs = Array.isArray(tempDirs) ? tempDirs.splice(0, tempDirs.length) : [...tempDirs]; + for (const dir of dirs) { + fs.rmSync(dir, { recursive: true, force: true }); + } + if (!Array.isArray(tempDirs)) { + tempDirs.clear(); + } +} diff --git a/test/scripts/ios-team-id.test.ts b/test/scripts/ios-team-id.test.ts index 2496073951c..0cd2b97997e 100644 --- a/test/scripts/ios-team-id.test.ts +++ b/test/scripts/ios-team-id.test.ts @@ -1,9 +1,9 @@ import { execFileSync } from "node:child_process"; import { chmodSync } from "node:fs"; -import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; -import os from "node:os"; +import { mkdir, writeFile } from "node:fs/promises"; import path from "node:path"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { cleanupTempDirs, makeTempDir } from "../helpers/temp-dir.js"; const SCRIPT = path.join(process.cwd(), "scripts", "ios-team-id.sh"); const BASH_BIN = process.platform === "win32" ? "bash" : "/bin/bash"; @@ -15,6 +15,7 @@ let sharedBinDir = ""; let sharedHomeDir = ""; let sharedHomeBinDir = ""; let sharedFakePythonPath = ""; +const tempDirs: string[] = []; const runScriptCache = new Map(); type TeamCandidate = { teamId: string; @@ -110,12 +111,19 @@ function runScript( runScriptCache.set(cacheKey, result); return result; } catch (error) { - const e = error as { - stdout?: string | Buffer; - stderr?: string | Buffer; - }; - const stdout = typeof e.stdout === "string" ? e.stdout : (e.stdout?.toString("utf8") ?? ""); - const stderr = typeof e.stderr === "string" ? e.stderr : (e.stderr?.toString("utf8") ?? ""); + const e = error as { stdout?: unknown; stderr?: unknown }; + const stdout = + typeof e.stdout === "string" + ? e.stdout + : Buffer.isBuffer(e.stdout) + ? e.stdout.toString("utf8") + : ""; + const stderr = + typeof e.stderr === "string" + ? e.stderr + : Buffer.isBuffer(e.stderr) + ? e.stderr.toString("utf8") + : ""; const result = { ok: false, stdout: stdout.trim(), stderr: stderr.trim() }; runScriptCache.set(cacheKey, result); return result; @@ -124,7 +132,7 @@ function runScript( describe("scripts/ios-team-id.sh", () => { beforeAll(async () => { - fixtureRoot = await mkdtemp(path.join(os.tmpdir(), "openclaw-ios-team-id-")); + fixtureRoot = makeTempDir(tempDirs, "openclaw-ios-team-id-"); sharedBinDir = path.join(fixtureRoot, "shared-bin"); await mkdir(sharedBinDir, { recursive: true }); sharedHomeDir = path.join(fixtureRoot, "home"); @@ -182,10 +190,7 @@ printf 'BBBBB22222\\t0\\tBeta Team\\r\\n'`, }); afterAll(async () => { - if (!fixtureRoot) { - return; - } - await rm(fixtureRoot, { recursive: true, force: true }); + cleanupTempDirs(tempDirs); }); it("parses team listings and prioritizes preferred IDs without shelling out", () => { diff --git a/test/test-env.test.ts b/test/test-env.test.ts index e76bc539c6b..d22e11befc2 100644 --- a/test/test-env.test.ts +++ b/test/test-env.test.ts @@ -1,8 +1,8 @@ import fs from "node:fs"; -import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; import { importFreshModule } from "./helpers/import-fresh.js"; +import { cleanupTempDirs, makeTempDir } from "./helpers/temp-dir.js"; import { installTestEnv } from "./test-env.js"; const ORIGINAL_ENV = { ...process.env }; @@ -31,9 +31,7 @@ function writeFile(targetPath: string, content: string): void { } function createTempHome(): string { - const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-test-env-real-home-")); - tempDirs.add(tempDir); - return tempDir; + return makeTempDir(tempDirs, "openclaw-test-env-real-home-"); } afterEach(() => { @@ -41,10 +39,7 @@ afterEach(() => { cleanupFns.pop()?.(); } restoreProcessEnv(); - for (const tempDir of tempDirs) { - fs.rmSync(tempDir, { recursive: true, force: true }); - } - tempDirs.clear(); + cleanupTempDirs(tempDirs); }); describe("installTestEnv", () => {