refactor(test): reuse shared env snapshots

This commit is contained in:
Peter Steinberger
2026-02-15 23:14:38 +00:00
parent bed0e07620
commit 70f86e326d
4 changed files with 38 additions and 54 deletions

View File

@@ -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") => {