refactor(test): snapshot bundled hooks env in loader tests

This commit is contained in:
Peter Steinberger
2026-02-21 18:42:56 +00:00
parent 5e607ae1eb
commit c3e1c82871

View File

@@ -3,6 +3,7 @@ import os from "node:os";
import path from "node:path"; import path from "node:path";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import { captureEnv } from "../test-utils/env.js";
import { import {
clearInternalHooks, clearInternalHooks,
getRegisteredEventKeys, getRegisteredEventKeys,
@@ -15,7 +16,7 @@ describe("loader", () => {
let fixtureRoot = ""; let fixtureRoot = "";
let caseId = 0; let caseId = 0;
let tmpDir: string; let tmpDir: string;
let originalBundledDir: string | undefined; let envSnapshot: ReturnType<typeof captureEnv>;
beforeAll(async () => { beforeAll(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hooks-loader-")); fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-hooks-loader-"));
@@ -28,18 +29,13 @@ describe("loader", () => {
await fs.mkdir(tmpDir, { recursive: true }); await fs.mkdir(tmpDir, { recursive: true });
// Disable bundled hooks during tests by setting env var to non-existent directory // Disable bundled hooks during tests by setting env var to non-existent directory
originalBundledDir = process.env.OPENCLAW_BUNDLED_HOOKS_DIR; envSnapshot = captureEnv(["OPENCLAW_BUNDLED_HOOKS_DIR"]);
process.env.OPENCLAW_BUNDLED_HOOKS_DIR = "/nonexistent/bundled/hooks"; process.env.OPENCLAW_BUNDLED_HOOKS_DIR = "/nonexistent/bundled/hooks";
}); });
afterEach(async () => { afterEach(async () => {
clearInternalHooks(); clearInternalHooks();
// Restore original env var envSnapshot.restore();
if (originalBundledDir === undefined) {
delete process.env.OPENCLAW_BUNDLED_HOOKS_DIR;
} else {
process.env.OPENCLAW_BUNDLED_HOOKS_DIR = originalBundledDir;
}
}); });
afterAll(async () => { afterAll(async () => {