


























@@ -2,7 +2,8 @@
22import fs from "node:fs/promises";
33import os from "node:os";
44import path from "node:path";
5-import { afterEach, describe, expect, it } from "vitest";
5+import { describe, expect, it } from "vitest";
6+import { withEnvAsync } from "../test-utils/env.js";
67import {
78formatCompletionReloadCommand,
89installCompletion,
@@ -12,22 +13,6 @@ import {
1213} from "./completion-runtime.js";
13141415describe("completion-runtime", () => {
15-const originalHome = process.env.HOME;
16-const originalStateDir = process.env.OPENCLAW_STATE_DIR;
17-18-afterEach(() => {
19-if (originalHome === undefined) {
20-delete process.env.HOME;
21-} else {
22-process.env.HOME = originalHome;
23-}
24-if (originalStateDir === undefined) {
25-delete process.env.OPENCLAW_STATE_DIR;
26-} else {
27-process.env.OPENCLAW_STATE_DIR = originalStateDir;
28-}
29-});
30-3116it("formats PowerShell reload commands with single-quoted paths", () => {
3217expect(formatCompletionReloadCommand("powershell", "C:\\Users\\Ada\\profile.ps1")).toBe(
3318". 'C:\\Users\\Ada\\profile.ps1'",
@@ -86,19 +71,18 @@ describe("completion-runtime", () => {
8671const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-completion-home-"));
8772const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-completion-state-bob's-"));
887389-process.env.HOME = homeDir;
90-process.env.OPENCLAW_STATE_DIR = stateDir;
91-9274try {
93-const cachePath = resolveCompletionCachePath("powershell", "openclaw");
94-await fs.mkdir(path.dirname(cachePath), { recursive: true });
95-await fs.writeFile(cachePath, "# powershell completion\n", "utf-8");
75+await withEnvAsync({ HOME: homeDir, OPENCLAW_STATE_DIR: stateDir }, async () => {
76+const cachePath = resolveCompletionCachePath("powershell", "openclaw");
77+await fs.mkdir(path.dirname(cachePath), { recursive: true });
78+await fs.writeFile(cachePath, "# powershell completion\n", "utf-8");
967997-await installCompletion("powershell", true, "openclaw");
80+ await installCompletion("powershell", true, "openclaw");
988199-const profilePath = resolveCompletionProfilePath("powershell");
100-const profile = await fs.readFile(profilePath, "utf-8");
101-expect(profile).toBe(`# OpenClaw Completion\n. '${cachePath.replace(/'/g, "''")}'\n`);
82+const profilePath = resolveCompletionProfilePath("powershell");
83+const profile = await fs.readFile(profilePath, "utf-8");
84+expect(profile).toBe(`# OpenClaw Completion\n. '${cachePath.replace(/'/g, "''")}'\n`);
85+});
10286} finally {
10387await fs.rm(homeDir, { recursive: true, force: true });
10488await fs.rm(stateDir, { recursive: true, force: true });
@@ -109,13 +93,12 @@ describe("completion-runtime", () => {
10993const homeDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-completion-home-"));
11094const stateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-completion-state-"));
11195112-process.env.HOME = homeDir;
113-process.env.OPENCLAW_STATE_DIR = stateDir;
114-11596try {
116-await expect(installCompletion("zsh", true, "openclaw")).rejects.toThrow(
117-"Completion cache not found",
118-);
97+await withEnvAsync({ HOME: homeDir, OPENCLAW_STATE_DIR: stateDir }, async () => {
98+await expect(installCompletion("zsh", true, "openclaw")).rejects.toThrow(
99+"Completion cache not found",
100+);
101+});
119102} finally {
120103await fs.rm(homeDir, { recursive: true, force: true });
121104await fs.rm(stateDir, { recursive: true, force: true });
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。