

























1-import { mkdtempSync, rmSync } from "node:fs";
1+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
22import { tmpdir } from "node:os";
33import { join } from "node:path";
44import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -10,7 +10,8 @@ vi.mock("../../infra/net/fetch-guard.js", () => ({
1010fetchWithSsrFGuard: fetchWithSsrFGuardMock,
1111}));
121213-vi.mock("node:child_process", () => ({
13+vi.mock("node:child_process", async (importOriginal) => ({
14+ ...((await importOriginal()) as typeof import("node:child_process")),
1415spawnSync: spawnSyncMock,
1516}));
1617@@ -86,4 +87,71 @@ describe("ensureTool", () => {
8687expect(releaseCheckRelease).toHaveBeenCalledOnce();
8788expect(downloadRelease).toHaveBeenCalledOnce();
8889});
90+91+it("extracts Windows zip downloads with trusted System32 tools", async () => {
92+vi.doMock("node:os", async (importOriginal) => ({
93+ ...((await importOriginal()) as typeof import("node:os")),
94+arch: () => "x64",
95+platform: () => "win32",
96+}));
97+98+const { ensureTool } = await import("./tools-manager.js");
99+const releaseCheckRelease = vi.fn(async () => {});
100+const downloadRelease = vi.fn(async () => {});
101+fetchWithSsrFGuardMock
102+.mockResolvedValueOnce({
103+response: new Response(JSON.stringify({ tag_name: "14.1.1" }), { status: 200 }),
104+release: releaseCheckRelease,
105+finalUrl: "https://api.github.com/repos/BurntSushi/ripgrep/releases/latest",
106+})
107+.mockResolvedValueOnce({
108+response: new Response("zip-bytes", { status: 200 }),
109+release: downloadRelease,
110+finalUrl: "https://github.com/BurntSushi/ripgrep/releases/download/14.1.1/archive.zip",
111+});
112+spawnSyncMock.mockImplementation((command: string, args: string[]) => {
113+if (command === "C:\\Windows\\System32\\tar.exe") {
114+return {
115+error: undefined,
116+status: 1,
117+stderr: Buffer.from("tar failed"),
118+stdout: Buffer.alloc(0),
119+};
120+}
121+if (command === "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe") {
122+const extractDir = args.at(-1);
123+if (!extractDir) {
124+throw new Error("expected extraction destination");
125+}
126+writeFileSync(join(extractDir, "rg.exe"), "binary");
127+return {
128+error: undefined,
129+status: 0,
130+stderr: Buffer.alloc(0),
131+stdout: Buffer.alloc(0),
132+};
133+}
134+return {
135+error: new Error(`unexpected command: ${command}`),
136+status: null,
137+stderr: Buffer.alloc(0),
138+stdout: Buffer.alloc(0),
139+};
140+});
141+142+await expect(ensureTool("rg", true)).resolves.toBe(join(tempAgentDir!, "bin", "rg.exe"));
143+144+expect(spawnSyncMock).toHaveBeenNthCalledWith(
145+2,
146+"C:\\Windows\\System32\\tar.exe",
147+expect.any(Array),
148+{ stdio: "pipe" },
149+);
150+expect(spawnSyncMock).toHaveBeenNthCalledWith(
151+3,
152+"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
153+expect.any(Array),
154+{ stdio: "pipe" },
155+);
156+});
89157});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。