mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 22:24:32 +00:00
refactor(test): centralize temporary state-dir env setup
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
|
||||
export function snapshotStateDirEnv() {
|
||||
@@ -12,3 +15,20 @@ export function setStateDirEnv(stateDir: string): void {
|
||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
||||
delete process.env.CLAWDBOT_STATE_DIR;
|
||||
}
|
||||
|
||||
export async function withStateDirEnv<T>(
|
||||
prefix: string,
|
||||
fn: (ctx: { tempRoot: string; stateDir: string }) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const snapshot = snapshotStateDirEnv();
|
||||
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
const stateDir = path.join(tempRoot, "state");
|
||||
await fs.mkdir(stateDir, { recursive: true });
|
||||
setStateDirEnv(stateDir);
|
||||
try {
|
||||
return await fn({ tempRoot, stateDir });
|
||||
} finally {
|
||||
restoreStateDirEnv(snapshot);
|
||||
await fs.rm(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user