mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(test): reuse env snapshots in unit suites
This commit is contained in:
@@ -4,6 +4,7 @@ import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import type { WizardPrompter } from "../wizard/prompts.js";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
import { applyAuthChoiceHuggingface } from "./auth-choice.apply.huggingface.js";
|
||||
|
||||
const noopAsync = async () => {};
|
||||
@@ -11,9 +12,7 @@ const noop = () => {};
|
||||
const authProfilePathFor = (agentDir: string) => path.join(agentDir, "auth-profiles.json");
|
||||
|
||||
describe("applyAuthChoiceHuggingface", () => {
|
||||
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
|
||||
const previousHfToken = process.env.HF_TOKEN;
|
||||
const previousHubToken = process.env.HUGGINGFACE_HUB_TOKEN;
|
||||
const envSnapshot = captureEnv(["OPENCLAW_AGENT_DIR", "HF_TOKEN", "HUGGINGFACE_HUB_TOKEN"]);
|
||||
let tempStateDir: string | null = null;
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -21,21 +20,7 @@ describe("applyAuthChoiceHuggingface", () => {
|
||||
await fs.rm(tempStateDir, { recursive: true, force: true });
|
||||
tempStateDir = null;
|
||||
}
|
||||
if (previousAgentDir === undefined) {
|
||||
delete process.env.OPENCLAW_AGENT_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
|
||||
}
|
||||
if (previousHfToken === undefined) {
|
||||
delete process.env.HF_TOKEN;
|
||||
} else {
|
||||
process.env.HF_TOKEN = previousHfToken;
|
||||
}
|
||||
if (previousHubToken === undefined) {
|
||||
delete process.env.HUGGINGFACE_HUB_TOKEN;
|
||||
} else {
|
||||
process.env.HUGGINGFACE_HUB_TOKEN = previousHubToken;
|
||||
}
|
||||
envSnapshot.restore();
|
||||
});
|
||||
|
||||
it("returns null when authChoice is not huggingface-api-key", async () => {
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
import { buildPairingReply } from "./pairing-messages.js";
|
||||
|
||||
describe("buildPairingReply", () => {
|
||||
let previousProfile: string | undefined;
|
||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||
|
||||
beforeEach(() => {
|
||||
previousProfile = process.env.OPENCLAW_PROFILE;
|
||||
envSnapshot = captureEnv(["OPENCLAW_PROFILE"]);
|
||||
process.env.OPENCLAW_PROFILE = "isolated";
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (previousProfile === undefined) {
|
||||
delete process.env.OPENCLAW_PROFILE;
|
||||
return;
|
||||
}
|
||||
process.env.OPENCLAW_PROFILE = previousProfile;
|
||||
envSnapshot.restore();
|
||||
});
|
||||
|
||||
const cases = [
|
||||
|
||||
@@ -4,6 +4,7 @@ import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { resolveOAuthDir } from "../config/paths.js";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
import { listChannelPairingRequests, upsertChannelPairingRequest } from "./pairing-store.js";
|
||||
|
||||
let fixtureRoot = "";
|
||||
@@ -20,18 +21,14 @@ afterAll(async () => {
|
||||
});
|
||||
|
||||
async function withTempStateDir<T>(fn: (stateDir: string) => Promise<T>) {
|
||||
const previous = process.env.OPENCLAW_STATE_DIR;
|
||||
const envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
|
||||
const dir = path.join(fixtureRoot, `case-${caseId++}`);
|
||||
await fs.mkdir(dir, { recursive: true });
|
||||
process.env.OPENCLAW_STATE_DIR = dir;
|
||||
try {
|
||||
return await fn(dir);
|
||||
} finally {
|
||||
if (previous === undefined) {
|
||||
delete process.env.OPENCLAW_STATE_DIR;
|
||||
} else {
|
||||
process.env.OPENCLAW_STATE_DIR = previous;
|
||||
}
|
||||
envSnapshot.restore();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { captureEnv } from "../test-utils/env.js";
|
||||
import { runCommandWithTimeout, shouldSpawnWithShell } from "./exec.js";
|
||||
|
||||
describe("runCommandWithTimeout", () => {
|
||||
@@ -25,7 +26,7 @@ describe("runCommandWithTimeout", () => {
|
||||
});
|
||||
|
||||
it("merges custom env with process.env", async () => {
|
||||
const previous = process.env.OPENCLAW_BASE_ENV;
|
||||
const envSnapshot = captureEnv(["OPENCLAW_BASE_ENV"]);
|
||||
process.env.OPENCLAW_BASE_ENV = "base";
|
||||
try {
|
||||
const result = await runCommandWithTimeout(
|
||||
@@ -43,11 +44,7 @@ describe("runCommandWithTimeout", () => {
|
||||
expect(result.code).toBe(0);
|
||||
expect(result.stdout).toBe("base|ok");
|
||||
} finally {
|
||||
if (previous === undefined) {
|
||||
delete process.env.OPENCLAW_BASE_ENV;
|
||||
} else {
|
||||
process.env.OPENCLAW_BASE_ENV = previous;
|
||||
}
|
||||
envSnapshot.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user