mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 23:30:21 +00:00
test: reuse plugin sdk temp fixtures
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user