diff --git a/src/agents/agent-paths.e2e.test.ts b/src/agents/agent-paths.e2e.test.ts index f455f8286..f0df2cbbd 100644 --- a/src/agents/agent-paths.e2e.test.ts +++ b/src/agents/agent-paths.e2e.test.ts @@ -2,12 +2,11 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; +import { captureEnv } from "../test-utils/env.js"; import { resolveOpenClawAgentDir } from "./agent-paths.js"; describe("resolveOpenClawAgentDir", () => { - const previousStateDir = process.env.OPENCLAW_STATE_DIR; - const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; - const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR; + const env = captureEnv(["OPENCLAW_STATE_DIR", "OPENCLAW_AGENT_DIR", "PI_CODING_AGENT_DIR"]); let tempStateDir: string | null = null; afterEach(async () => { @@ -15,21 +14,7 @@ describe("resolveOpenClawAgentDir", () => { await fs.rm(tempStateDir, { recursive: true, force: true }); tempStateDir = null; } - if (previousStateDir === undefined) { - delete process.env.OPENCLAW_STATE_DIR; - } else { - process.env.OPENCLAW_STATE_DIR = previousStateDir; - } - if (previousAgentDir === undefined) { - delete process.env.OPENCLAW_AGENT_DIR; - } else { - process.env.OPENCLAW_AGENT_DIR = previousAgentDir; - } - if (previousPiAgentDir === undefined) { - delete process.env.PI_CODING_AGENT_DIR; - } else { - process.env.PI_CODING_AGENT_DIR = previousPiAgentDir; - } + env.restore(); }); it("defaults to the multi-agent path when no overrides are set", async () => { diff --git a/src/test-utils/env.ts b/src/test-utils/env.ts new file mode 100644 index 000000000..0976987c2 --- /dev/null +++ b/src/test-utils/env.ts @@ -0,0 +1,18 @@ +export function captureEnv(keys: string[]) { + const snapshot = new Map(); + for (const key of keys) { + snapshot.set(key, process.env[key]); + } + + return { + restore() { + for (const [key, value] of snapshot) { + if (value === undefined) { + delete process.env[key]; + } else { + process.env[key] = value; + } + } + }, + }; +}