mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-03 14:10:24 +00:00
fix(test): isolate shared vitest home setup
This commit is contained in:
13
test/setup-home-isolation.test.ts
Normal file
13
test/setup-home-isolation.test.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import path from "node:path";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { createConfigIO } from "../src/config/config.js";
|
||||||
|
|
||||||
|
describe("shared test setup home isolation", () => {
|
||||||
|
it("routes default config IO through the per-worker temp home", () => {
|
||||||
|
const testHome = process.env.OPENCLAW_TEST_HOME;
|
||||||
|
expect(testHome).toBeTruthy();
|
||||||
|
expect(process.env.HOME).toBe(testHome);
|
||||||
|
expect(process.env.USERPROFILE).toBe(testHome);
|
||||||
|
expect(createConfigIO().configPath).toBe(path.join(testHome!, ".openclaw", "openclaw.json"));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -51,13 +51,41 @@ type SharedTestSetupOptions = {
|
|||||||
loadProfileEnv?: boolean;
|
loadProfileEnv?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const SHARED_TEST_SETUP = Symbol.for("openclaw.sharedTestSetup");
|
||||||
|
|
||||||
|
type SharedTestSetupHandle = {
|
||||||
|
cleanup: () => void;
|
||||||
|
tempHome: string;
|
||||||
|
};
|
||||||
|
|
||||||
export function installSharedTestSetup(options?: SharedTestSetupOptions): {
|
export function installSharedTestSetup(options?: SharedTestSetupOptions): {
|
||||||
cleanup: () => void;
|
cleanup: () => void;
|
||||||
tempHome: string;
|
tempHome: string;
|
||||||
} {
|
} {
|
||||||
|
const globalState = globalThis as typeof globalThis & {
|
||||||
|
[SHARED_TEST_SETUP]?: SharedTestSetupHandle;
|
||||||
|
};
|
||||||
|
const existing = globalState[SHARED_TEST_SETUP];
|
||||||
|
if (existing) {
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
const testEnv = withIsolatedTestHome({
|
const testEnv = withIsolatedTestHome({
|
||||||
loadProfileEnv: options?.loadProfileEnv,
|
loadProfileEnv: options?.loadProfileEnv,
|
||||||
});
|
});
|
||||||
installProcessWarningFilter();
|
installProcessWarningFilter();
|
||||||
return testEnv;
|
|
||||||
|
let cleaned = false;
|
||||||
|
const handle: SharedTestSetupHandle = {
|
||||||
|
tempHome: testEnv.tempHome,
|
||||||
|
cleanup: () => {
|
||||||
|
if (cleaned) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cleaned = true;
|
||||||
|
testEnv.cleanup();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
globalState[SHARED_TEST_SETUP] = handle;
|
||||||
|
return handle;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,8 @@
|
|||||||
import "./setup.shared.js";
|
import { afterAll } from "vitest";
|
||||||
|
import { installSharedTestSetup } from "./setup.shared.js";
|
||||||
|
|
||||||
|
const testEnv = installSharedTestSetup();
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
testEnv.cleanup();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user