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