From 3f6268ebd7069138d089837f805ba7ea2effd7f4 Mon Sep 17 00:00:00 2001 From: Shakker Date: Thu, 4 Jun 2026 23:05:56 +0100 Subject: [PATCH] test: scope utility home env --- src/utils.test.ts | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/utils.test.ts b/src/utils.test.ts index db95bc010bac..8591407b4fa0 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -4,6 +4,7 @@ import path from "node:path"; import { describe, expect, it, vi } from "vitest"; import { MAX_TIMER_TIMEOUT_MS } from "./shared/number-coercion.js"; import { withTempDir } from "./test-helpers/temp-dir.js"; +import { withEnv } from "./test-utils/env.js"; import { ensureDir, resolveConfigDir, @@ -84,43 +85,31 @@ describe("resolveConfigDir", () => { describe("resolveHomeDir", () => { it("prefers OPENCLAW_HOME over HOME", () => { - vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home"); - vi.stubEnv("HOME", "/home/other"); - try { + withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => { expect(resolveHomeDir()).toBe(path.resolve("/srv/openclaw-home")); - } finally { - vi.unstubAllEnvs(); - } + }); }); }); describe("shortenHomePath", () => { it("uses $OPENCLAW_HOME prefix when OPENCLAW_HOME is set", () => { - vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home"); - vi.stubEnv("HOME", "/home/other"); - try { + withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => { expect(shortenHomePath(`${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`)).toBe( "$OPENCLAW_HOME/.openclaw/openclaw.json", ); - } finally { - vi.unstubAllEnvs(); - } + }); }); }); describe("shortenHomeInString", () => { it("uses $OPENCLAW_HOME replacement when OPENCLAW_HOME is set", () => { - vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home"); - vi.stubEnv("HOME", "/home/other"); - try { + withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => { expect( shortenHomeInString( `config: ${path.resolve("/srv/openclaw-home")}/.openclaw/openclaw.json`, ), ).toBe("config: $OPENCLAW_HOME/.openclaw/openclaw.json"); - } finally { - vi.unstubAllEnvs(); - } + }); }); }); @@ -140,13 +129,9 @@ describe("resolveUserPath", () => { }); it("prefers OPENCLAW_HOME for tilde expansion", () => { - vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home"); - vi.stubEnv("HOME", "/home/other"); - try { + withEnv({ OPENCLAW_HOME: "/srv/openclaw-home", HOME: "/home/other" }, () => { expect(resolveUserPath("~/openclaw")).toBe(path.resolve("/srv/openclaw-home", "openclaw")); - } finally { - vi.unstubAllEnvs(); - } + }); }); it("uses the provided env for tilde expansion", () => {