refactor(test): reuse shared env snapshots
This commit is contained in:
@@ -3,6 +3,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { captureFullEnv } from "../test-utils/env.js";
|
||||
import { resolveSandboxContext } from "./sandbox.js";
|
||||
|
||||
vi.mock("./sandbox/docker.js", () => ({
|
||||
@@ -27,30 +28,15 @@ async function writeSkill(params: { dir: string; name: string; description: stri
|
||||
);
|
||||
}
|
||||
|
||||
function restoreEnv(snapshot: Record<string, string | undefined>) {
|
||||
for (const key of Object.keys(process.env)) {
|
||||
if (!(key in snapshot)) {
|
||||
delete process.env[key];
|
||||
}
|
||||
}
|
||||
for (const [key, value] of Object.entries(snapshot)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe("sandbox skill mirroring", () => {
|
||||
let envSnapshot: Record<string, string | undefined>;
|
||||
let envSnapshot: ReturnType<typeof captureFullEnv>;
|
||||
|
||||
beforeEach(() => {
|
||||
envSnapshot = { ...process.env };
|
||||
envSnapshot = captureFullEnv();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
restoreEnv(envSnapshot);
|
||||
envSnapshot.restore();
|
||||
});
|
||||
|
||||
const runContext = async (workspaceAccess: "none" | "ro") => {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { captureFullEnv } from "../test-utils/env.js";
|
||||
|
||||
const spawnMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
@@ -8,27 +9,12 @@ vi.mock("node:child_process", () => ({
|
||||
|
||||
import { restartGatewayProcessWithFreshPid } from "./process-respawn.js";
|
||||
|
||||
const originalEnv = { ...process.env };
|
||||
const originalArgv = [...process.argv];
|
||||
const originalExecArgv = [...process.execArgv];
|
||||
|
||||
function restoreEnv() {
|
||||
for (const key of Object.keys(process.env)) {
|
||||
if (!(key in originalEnv)) {
|
||||
delete process.env[key];
|
||||
}
|
||||
}
|
||||
for (const [key, value] of Object.entries(originalEnv)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
const envSnapshot = captureFullEnv();
|
||||
|
||||
afterEach(() => {
|
||||
restoreEnv();
|
||||
envSnapshot.restore();
|
||||
process.argv = [...originalArgv];
|
||||
process.execArgv = [...originalExecArgv];
|
||||
spawnMock.mockReset();
|
||||
|
||||
@@ -5,30 +5,21 @@ import path from "node:path";
|
||||
import sharp from "sharp";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { isPathWithinBase } from "../../test/helpers/paths.js";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
|
||||
describe("media store", () => {
|
||||
let store: typeof import("./store.js");
|
||||
let home = "";
|
||||
const envSnapshot: Record<string, string | undefined> = {};
|
||||
|
||||
const snapshotEnv = () => {
|
||||
for (const key of ["HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH", "OPENCLAW_STATE_DIR"]) {
|
||||
envSnapshot[key] = process.env[key];
|
||||
}
|
||||
};
|
||||
|
||||
const restoreEnv = () => {
|
||||
for (const [key, value] of Object.entries(envSnapshot)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
};
|
||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||
|
||||
beforeAll(async () => {
|
||||
snapshotEnv();
|
||||
envSnapshot = captureEnv([
|
||||
"HOME",
|
||||
"USERPROFILE",
|
||||
"HOMEDRIVE",
|
||||
"HOMEPATH",
|
||||
"OPENCLAW_STATE_DIR",
|
||||
]);
|
||||
home = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-test-home-"));
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
@@ -45,7 +36,7 @@ describe("media store", () => {
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
restoreEnv();
|
||||
envSnapshot.restore();
|
||||
try {
|
||||
await fs.rm(home, { recursive: true, force: true });
|
||||
} catch {
|
||||
|
||||
@@ -17,6 +17,27 @@ export function captureEnv(keys: string[]) {
|
||||
};
|
||||
}
|
||||
|
||||
export function captureFullEnv() {
|
||||
const snapshot: Record<string, string | undefined> = { ...process.env };
|
||||
|
||||
return {
|
||||
restore() {
|
||||
for (const key of Object.keys(process.env)) {
|
||||
if (!(key in snapshot)) {
|
||||
delete process.env[key];
|
||||
}
|
||||
}
|
||||
for (const [key, value] of Object.entries(snapshot)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function withEnv<T>(env: Record<string, string | undefined>, fn: () => T): T {
|
||||
const snapshot = captureEnv(Object.keys(env));
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user