test(root): share temp dir helper across root tests

This commit is contained in:
Vincent Koc 2026-04-06 05:42:40 +01:00
parent d4c443bc1e
commit ce50b97c86
3 changed files with 44 additions and 21 deletions

23
test/helpers/temp-dir.ts Normal file
View File

@ -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<string>, 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<string>): 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();
}
}

View File

@ -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<string, { ok: boolean; stdout: string; stderr: string }>();
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", () => {

View File

@ -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", () => {