test(infra): share suite temp root tracker in infra tests

This commit is contained in:
Vincent Koc
2026-04-06 06:13:32 +01:00
parent 138e85c88e
commit 7c629d3e8b
3 changed files with 43 additions and 14 deletions

View File

@@ -1,22 +1,19 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import JSZip from "jszip";
import * as tar from "tar";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js";
import { withRealpathSymlinkRebindRace } from "../test-utils/symlink-rebind-race.js";
import type { ArchiveSecurityError } from "./archive.js";
import { extractArchive, resolvePackedRootDir } from "./archive.js";
let fixtureRoot = "";
let fixtureCount = 0;
const fixtureRootTracker = createSuiteTempRootTracker({ prefix: "openclaw-archive-" });
const directorySymlinkType = process.platform === "win32" ? "junction" : undefined;
const ARCHIVE_EXTRACT_TIMEOUT_MS = 15_000;
async function makeTempDir(prefix = "case") {
const dir = path.join(fixtureRoot, `${prefix}-${fixtureCount++}`);
await fs.mkdir(dir, { recursive: true });
return dir;
return await fixtureRootTracker.make(prefix);
}
async function withArchiveCase(
@@ -75,11 +72,11 @@ async function expectExtractedSizeBudgetExceeded(params: {
}
beforeAll(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-archive-"));
await fixtureRootTracker.setup();
});
afterAll(async () => {
await fs.rm(fixtureRoot, { recursive: true, force: true });
await fixtureRootTracker.cleanup();
});
describe("archive utils", () => {

View File

@@ -3,15 +3,15 @@ import { EventEmitter } from "node:events";
import fsSync from "node:fs";
import fs from "node:fs/promises";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { setTimeout as nativeSleep } from "node:timers/promises";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { resolveConfigPath, resolveStateDir } from "../config/paths.js";
import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js";
import { acquireGatewayLock, GatewayLockError, type GatewayLockOptions } from "./gateway-lock.js";
const fixtureRootTracker = createSuiteTempRootTracker({ prefix: "openclaw-gateway-lock-" });
let fixtureRoot = "";
let fixtureCount = 0;
const realNow = Date.now.bind(Date);
function resolveTestLockDir() {
@@ -19,8 +19,7 @@ function resolveTestLockDir() {
}
async function makeEnv() {
const dir = path.join(fixtureRoot, `case-${fixtureCount++}`);
await fs.mkdir(dir, { recursive: true });
const dir = await fixtureRootTracker.make("case");
const configPath = path.join(dir, "openclaw.json");
await fs.writeFile(configPath, "{}", "utf8");
return {
@@ -149,7 +148,7 @@ async function writeRecentLockFile(env: NodeJS.ProcessEnv, startTime = 111) {
describe("gateway lock", () => {
beforeAll(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-gateway-lock-"));
fixtureRoot = await fixtureRootTracker.setup();
});
beforeEach(() => {
@@ -161,7 +160,8 @@ describe("gateway lock", () => {
});
afterAll(async () => {
await fs.rm(fixtureRoot, { recursive: true, force: true });
await fixtureRootTracker.cleanup();
fixtureRoot = "";
});
afterEach(() => {

View File

@@ -28,6 +28,38 @@ export async function withTempDir<T>(
}
}
export function createSuiteTempRootTracker(options: { prefix: string; parentDir?: string }) {
let root = "";
let nextIndex = 0;
return {
async setup(): Promise<string> {
root = await fs.mkdtemp(path.join(options.parentDir ?? os.tmpdir(), options.prefix));
nextIndex = 0;
return root;
},
async make(prefix = "case"): Promise<string> {
const dir = path.join(root, `${prefix}-${nextIndex++}`);
await fs.mkdir(dir, { recursive: true });
return dir;
},
async cleanup(): Promise<void> {
if (!root) {
return;
}
const currentRoot = root;
root = "";
nextIndex = 0;
await fs.rm(currentRoot, {
recursive: true,
force: true,
maxRetries: 20,
retryDelay: 25,
});
},
};
}
export function withTempDirSync<T>(
options: {
prefix: string;