

























@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
22import path from "node:path";
33import { afterEach, describe, expect, it, vi } from "vitest";
44import { withTempDir } from "../test-helpers/temp-dir.js";
5+import { withMockedPlatform } from "../test-utils/vitest-spies.js";
56import {
67isExecutableFile,
78resolveExecutable,
@@ -129,88 +130,88 @@ describe("resolveExecutable", () => {
129130});
130131131132it("returns cmd unchanged on non-Windows platforms", () => {
132-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("linux");
133-expect(resolveExecutable("gcloud")).toBe("gcloud");
134-platformSpy.mockRestore();
133+withMockedPlatform("linux", () => {
134+ expect(resolveExecutable("gcloud")).toBe("gcloud");
135+});
135136});
136137137138it("returns cmd unchanged when it already carries a known PATHEXT extension on Windows", () => {
138-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
139-expect(resolveExecutable("gcloud.cmd")).toBe("gcloud.cmd");
140-expect(resolveExecutable("gcloud.exe")).toBe("gcloud.exe");
141-expect(resolveExecutable("gcloud.bat")).toBe("gcloud.bat");
142-expect(resolveExecutable("gcloud.com")).toBe("gcloud.com");
143-platformSpy.mockRestore();
139+withMockedPlatform("win32", () => {
140+ expect(resolveExecutable("gcloud.cmd")).toBe("gcloud.cmd");
141+ expect(resolveExecutable("gcloud.exe")).toBe("gcloud.exe");
142+ expect(resolveExecutable("gcloud.bat")).toBe("gcloud.bat");
143+ expect(resolveExecutable("gcloud.com")).toBe("gcloud.com");
144+});
144145});
145146146147it("resolves to the first .cmd result from PATH on Windows without executing where.exe", async () => {
147-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
148-await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
149-const binDir = path.join(base, "bin");
150-await fs.mkdir(binDir, { recursive: true });
151-const cmdPath = path.join(binDir, "gcloud.cmd");
152-const exePath = path.join(binDir, "gcloud.exe");
153-await fs.writeFile(cmdPath, "@echo off\n", "utf8");
154-await fs.writeFile(exePath, "exe\n", "utf8");
155-156-const originalPath = process.env.PATH;
157-const originalPathext = process.env.PATHEXT;
158-process.env.PATH = binDir;
159-process.env.PATHEXT = ".EXE;.CMD;.BAT;.COM";
160-try {
161-expect(resolveExecutable("gcloud")).toBe(cmdPath);
162-} finally {
163-restoreEnvValue("PATH", originalPath);
164-restoreEnvValue("PATHEXT", originalPathext);
165-}
148+await withMockedPlatform("win32", async () => {
149+await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
150+const binDir = path.join(base, "bin");
151+await fs.mkdir(binDir, { recursive: true });
152+const cmdPath = path.join(binDir, "gcloud.cmd");
153+const exePath = path.join(binDir, "gcloud.exe");
154+await fs.writeFile(cmdPath, "@echo off\n", "utf8");
155+await fs.writeFile(exePath, "exe\n", "utf8");
156+157+const originalPath = process.env.PATH;
158+const originalPathext = process.env.PATHEXT;
159+process.env.PATH = binDir;
160+process.env.PATHEXT = ".EXE;.CMD;.BAT;.COM";
161+try {
162+expect(resolveExecutable("gcloud")).toBe(cmdPath);
163+} finally {
164+restoreEnvValue("PATH", originalPath);
165+restoreEnvValue("PATHEXT", originalPathext);
166+}
167+});
166168});
167-platformSpy.mockRestore();
168169});
169170170171it("falls back to .exe when no .cmd match exists on Windows", async () => {
171-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
172-await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
173-const binDir = path.join(base, "bin");
174-await fs.mkdir(binDir, { recursive: true });
175-const exePath = path.join(binDir, "tailscale.exe");
176-await fs.writeFile(exePath, "exe\n", "utf8");
177-178-const originalPath = process.env.PATH;
179-process.env.PATH = binDir;
180-try {
181-expect(resolveExecutable("tailscale")).toBe(exePath);
182-} finally {
183-restoreEnvValue("PATH", originalPath);
184-}
172+await withMockedPlatform("win32", async () => {
173+await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
174+const binDir = path.join(base, "bin");
175+await fs.mkdir(binDir, { recursive: true });
176+const exePath = path.join(binDir, "tailscale.exe");
177+await fs.writeFile(exePath, "exe\n", "utf8");
178+179+const originalPath = process.env.PATH;
180+process.env.PATH = binDir;
181+try {
182+expect(resolveExecutable("tailscale")).toBe(exePath);
183+} finally {
184+restoreEnvValue("PATH", originalPath);
185+}
186+});
185187});
186-platformSpy.mockRestore();
187188});
188189189190it("falls back to first PATH result when no .cmd or .exe match exists on Windows", async () => {
190-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
191-await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
192-const binDir = path.join(base, "bin");
193-await fs.mkdir(binDir, { recursive: true });
194-const ps1Path = path.join(binDir, "gcloud.ps1");
195-await fs.writeFile(ps1Path, "Write-Output ok\n", "utf8");
196-197-const originalPath = process.env.PATH;
198-const originalPathext = process.env.PATHEXT;
199-process.env.PATH = binDir;
200-process.env.PATHEXT = ".PS1";
201-try {
202-expect(resolveExecutable("gcloud")).toBe(ps1Path);
203-} finally {
204-restoreEnvValue("PATH", originalPath);
205-restoreEnvValue("PATHEXT", originalPathext);
206-}
191+await withMockedPlatform("win32", async () => {
192+await withTempDir({ prefix: "openclaw-exec-path-" }, async (base) => {
193+const binDir = path.join(base, "bin");
194+await fs.mkdir(binDir, { recursive: true });
195+const ps1Path = path.join(binDir, "gcloud.ps1");
196+await fs.writeFile(ps1Path, "Write-Output ok\n", "utf8");
197+198+const originalPath = process.env.PATH;
199+const originalPathext = process.env.PATHEXT;
200+process.env.PATH = binDir;
201+process.env.PATHEXT = ".PS1";
202+try {
203+expect(resolveExecutable("gcloud")).toBe(ps1Path);
204+} finally {
205+restoreEnvValue("PATH", originalPath);
206+restoreEnvValue("PATHEXT", originalPathext);
207+}
208+});
207209});
208-platformSpy.mockRestore();
209210});
210211211212it("returns original cmd when no PATH match exists on Windows", () => {
212-const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32");
213-expect(resolveExecutable("gog")).toBe("gog");
214-platformSpy.mockRestore();
213+withMockedPlatform("win32", () => {
214+ expect(resolveExecutable("gog")).toBe("gog");
215+});
215216});
216217});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。