test: centralize session file env restore · openclaw/openclaw@a1b7f35
shakkernerd
·
2026-06-15
·
via Recent Commits to openclaw:main
File tree
packages/memory-host-sdk/src/host
| Original file line number | Diff line number | Diff line change |
|---|
@@ -10,9 +10,22 @@ import {
|
10 | 10 | type SessionFileEntry, |
11 | 11 | } from "./session-files.js"; |
12 | 12 | |
| 13 | +function captureStateDirEnv() { |
| 14 | +const value = process.env.OPENCLAW_STATE_DIR; |
| 15 | +return { |
| 16 | +restore() { |
| 17 | +if (value === undefined) { |
| 18 | +Reflect.deleteProperty(process.env, "OPENCLAW_STATE_DIR"); |
| 19 | +} else { |
| 20 | +Reflect.set(process.env, "OPENCLAW_STATE_DIR", value); |
| 21 | +} |
| 22 | +}, |
| 23 | +}; |
| 24 | +} |
| 25 | + |
13 | 26 | let fixtureRoot: string; |
14 | 27 | let tmpDir: string; |
15 | | -let originalStateDir: string | undefined; |
| 28 | +let envSnapshot: ReturnType<typeof captureStateDirEnv> | undefined; |
16 | 29 | let fixtureId = 0; |
17 | 30 | |
18 | 31 | beforeAll(() => { |
@@ -26,16 +39,13 @@ afterAll(() => {
|
26 | 39 | beforeEach(() => { |
27 | 40 | tmpDir = path.join(fixtureRoot, `case-${fixtureId++}`); |
28 | 41 | fsSync.mkdirSync(tmpDir, { recursive: true }); |
29 | | -originalStateDir = process.env.OPENCLAW_STATE_DIR; |
30 | | -process.env.OPENCLAW_STATE_DIR = tmpDir; |
| 42 | +envSnapshot = captureStateDirEnv(); |
| 43 | +Reflect.set(process.env, "OPENCLAW_STATE_DIR", tmpDir); |
31 | 44 | }); |
32 | 45 | |
33 | 46 | afterEach(() => { |
34 | | -if (originalStateDir === undefined) { |
35 | | -delete process.env.OPENCLAW_STATE_DIR; |
36 | | -} else { |
37 | | -process.env.OPENCLAW_STATE_DIR = originalStateDir; |
38 | | -} |
| 47 | +envSnapshot?.restore(); |
| 48 | +envSnapshot = undefined; |
39 | 49 | }); |
40 | 50 | |
41 | 51 | function requireSessionEntry(entry: SessionFileEntry | null): SessionFileEntry { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。