test: reuse plugin sdk temp fixtures

This commit is contained in:
Peter Steinberger
2026-04-07 05:58:31 +01:00
parent 8cde0167c5
commit ac9464441c

View File

@@ -1,35 +1,41 @@
import { mkdtempSync, type RmOptions } from "node:fs";
import { mkdtemp, rm } from "node:fs/promises";
import { mkdirSync, type RmOptions } from "node:fs";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach } from "vitest";
import { afterAll, beforeAll } from "vitest";
export function createPluginSdkTestHarness(options?: { cleanup?: RmOptions }) {
const tempDirs: string[] = [];
let fixtureRoot = "";
let caseId = 0;
afterEach(async () => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (!dir) {
continue;
}
await rm(dir, {
recursive: true,
force: true,
...options?.cleanup,
});
}
beforeAll(async () => {
fixtureRoot = await mkdtemp(path.join(tmpdir(), "openclaw-plugin-sdk-fixtures-"));
});
afterAll(async () => {
if (!fixtureRoot) {
return;
}
await rm(fixtureRoot, {
recursive: true,
force: true,
...options?.cleanup,
});
});
function nextTempDir(prefix: string): string {
return path.join(fixtureRoot, `${prefix}${caseId++}`);
}
async function createTempDir(prefix: string): Promise<string> {
const dir = await mkdtemp(path.join(tmpdir(), prefix));
tempDirs.push(dir);
const dir = nextTempDir(prefix);
await mkdir(dir, { recursive: true });
return dir;
}
function createTempDirSync(prefix: string): string {
const dir = mkdtempSync(path.join(tmpdir(), prefix));
tempDirs.push(dir);
const dir = nextTempDir(prefix);
mkdirSync(dir, { recursive: true });
return dir;
}