refactor(test): simplify state dir env restore

This commit is contained in:
Peter Steinberger
2026-02-15 23:29:56 +00:00
parent 35ab521e07
commit f0e373b82e
2 changed files with 9 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import sharp from "sharp";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import * as ssrf from "../infra/net/ssrf.js";
import { optimizeImageToPng } from "../media/image-ops.js";
import { captureEnv } from "../test-utils/env.js";
import { loadWebMedia, loadWebMediaRaw, optimizeImageToJpeg } from "./media.js";
let fixtureRoot = "";
@@ -19,7 +20,7 @@ let alphaPngFile = "";
let fallbackPngBuffer: Buffer;
let fallbackPngFile = "";
let fallbackPngCap = 0;
let previousStateDir: string | undefined;
let stateDirSnapshot: ReturnType<typeof captureEnv>;
async function writeTempFile(buffer: Buffer, ext: string): Promise<string> {
const file = path.join(fixtureRoot, `media-${fixtureFileCount++}${ext}`);
@@ -100,7 +101,7 @@ describe("web media loading", () => {
beforeAll(() => {
// Ensure state dir is stable and not influenced by other tests that stub OPENCLAW_STATE_DIR.
// Also keep it outside os.tmpdir() so tmpdir localRoots doesn't accidentally make all state readable.
previousStateDir = process.env.OPENCLAW_STATE_DIR;
stateDirSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
process.env.OPENCLAW_STATE_DIR = path.join(
path.parse(os.tmpdir()).root,
"var",
@@ -110,11 +111,7 @@ describe("web media loading", () => {
});
afterAll(() => {
if (previousStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = previousStateDir;
}
stateDirSnapshot.restore();
});
beforeAll(() => {