mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-02 18:20:28 +00:00
refactor(test): simplify state dir env restore
This commit is contained in:
@@ -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(() => {
|
||||
|
||||
Reference in New Issue
Block a user