refactor(test): simplify config io env snapshot

This commit is contained in:
Peter Steinberger
2026-02-15 23:58:06 +00:00
parent a90e007d50
commit 7bb0b7d1fc

View File

@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { createConfigIO } from "./io.js";
describe("config io write", () => {
@@ -10,37 +11,6 @@ describe("config io write", () => {
error: () => {},
};
type HomeEnvSnapshot = {
home: string | undefined;
userProfile: string | undefined;
homeDrive: string | undefined;
homePath: string | undefined;
stateDir: string | undefined;
};
const snapshotHomeEnv = (): HomeEnvSnapshot => ({
home: process.env.HOME,
userProfile: process.env.USERPROFILE,
homeDrive: process.env.HOMEDRIVE,
homePath: process.env.HOMEPATH,
stateDir: process.env.OPENCLAW_STATE_DIR,
});
const restoreHomeEnv = (snapshot: HomeEnvSnapshot) => {
const restoreKey = (key: string, value: string | undefined) => {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
};
restoreKey("HOME", snapshot.home);
restoreKey("USERPROFILE", snapshot.userProfile);
restoreKey("HOMEDRIVE", snapshot.homeDrive);
restoreKey("HOMEPATH", snapshot.homePath);
restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir);
};
let fixtureRoot = "";
let caseId = 0;
@@ -49,7 +19,13 @@ describe("config io write", () => {
const home = path.join(fixtureRoot, `${safePrefix}${caseId++}`);
await fs.mkdir(path.join(home, ".openclaw"), { recursive: true });
const snapshot = snapshotHomeEnv();
const snapshot = captureEnv([
"HOME",
"USERPROFILE",
"HOMEDRIVE",
"HOMEPATH",
"OPENCLAW_STATE_DIR",
]);
process.env.HOME = home;
process.env.USERPROFILE = home;
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
@@ -65,7 +41,7 @@ describe("config io write", () => {
try {
await fn(home);
} finally {
restoreHomeEnv(snapshot);
snapshot.restore();
}
}