























@@ -13,6 +13,7 @@ import {
1313} from "../state/openclaw-agent-db.js";
1414import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
1515import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
16+import { withEnvAsync } from "../test-utils/env.js";
1617import { resolveAgentDir } from "./agent-scope.js";
1718import { loadPersistedAuthProfileStore } from "./auth-profiles/persisted.js";
1819import { resolveAuthProfileDatabasePath } from "./auth-profiles/sqlite.js";
@@ -58,27 +59,19 @@ function apiKeyStore(key: string): AuthProfileStore {
58595960async function withAgentDirEnv(prefix: string, run: (agentDir: string) => void | Promise<void>) {
6061const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
61-const previousStateDir = process.env.OPENCLAW_STATE_DIR;
62-const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
6362const agentDir = path.join(root, "agents", "main", "agent");
6463try {
65-process.env.OPENCLAW_STATE_DIR = root;
66-process.env.OPENCLAW_AGENT_DIR = agentDir;
6764fs.mkdirSync(agentDir, { recursive: true });
68-await run(agentDir);
65+await withEnvAsync(
66+{
67+OPENCLAW_STATE_DIR: root,
68+OPENCLAW_AGENT_DIR: agentDir,
69+},
70+async () => await run(agentDir),
71+);
6972} finally {
7073closeOpenClawAgentDatabasesForTest();
7174closeOpenClawStateDatabaseForTest();
72-if (previousStateDir === undefined) {
73-delete process.env.OPENCLAW_STATE_DIR;
74-} else {
75-process.env.OPENCLAW_STATE_DIR = previousStateDir;
76-}
77-if (previousAgentDir === undefined) {
78-delete process.env.OPENCLAW_AGENT_DIR;
79-} else {
80-process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
81-}
8275fs.rmSync(root, { recursive: true, force: true });
8376}
8477}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。