Skip to content

Commit 162cc94

Browse files
test: add zip extraction native tools tests
Add comprehensive tests for Windows native tools fallback behavior in zip extraction, including tar.exe and PowerShell fallback ordering. Changes: - Add src/__tests__/zip-extract.test.ts with native tool fallback tests - Update src/__tests__/node-download-extract.test.ts for Windows expectations Co-Authored-By: Hagicode <noreply@hagicode.com> Signed-off-by: newbe36524 <newbe36524@qq.com>
1 parent 61e44c6 commit 162cc94

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

src/__tests__/node-download-extract.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ describe("Node.js extraction guards", () => {
134134
});
135135

136136
expect(extractedRoot).toBe(join(staging, "node-root"));
137-
expect(runCommand).not.toHaveBeenCalled();
137+
if (process.platform === "win32") {
138+
expect(runCommand).toHaveBeenCalled();
139+
} else {
140+
expect(runCommand).not.toHaveBeenCalled();
141+
}
138142
await expect(readdir(join(extractedRoot, "bin"))).resolves.toEqual(["node.exe"]);
139143
});
140144

src/__tests__/zip-extract.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
2+
import { join } from "node:path";
3+
import { tmpdir } from "node:os";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { createZipArchive } from "./archive-test-utils.js";
6+
import { extractZipArchive } from "../runtime/zip-extract.js";
7+
8+
const tempRoots: string[] = [];
9+
10+
afterEach(async () => {
11+
await Promise.all(
12+
tempRoots.splice(0).map((root) => rm(root, { recursive: true, force: true }))
13+
);
14+
});
15+
16+
describe("zip extraction", () => {
17+
it("prefers tar.exe on Windows before falling back to Node extraction", async () => {
18+
const root = await makeTempRoot();
19+
const archivePath = join(root, "fixture.zip");
20+
const destination = join(root, "extract");
21+
const attemptedCommands: string[] = [];
22+
23+
await writeFile(
24+
archivePath,
25+
createZipArchive([{ name: "runtime/bin/tool.cmd", contents: "tool" }])
26+
);
27+
28+
await extractZipArchive(archivePath, destination, {
29+
platform: "win32",
30+
runCommand: async (command: string, args: string[]) => {
31+
attemptedCommands.push(command);
32+
33+
if (command !== "tar.exe") {
34+
throw new Error(`Unexpected fallback command: ${command}`);
35+
}
36+
37+
await mkdir(join(destination, "runtime", "bin"), { recursive: true });
38+
await writeFile(join(destination, "runtime", "bin", "tool.cmd"), "tool");
39+
return {
40+
command,
41+
args,
42+
stdout: "",
43+
stderr: "",
44+
exitCode: 0,
45+
timedOut: false
46+
};
47+
}
48+
});
49+
50+
expect(attemptedCommands).toEqual(["tar.exe"]);
51+
await expect(readdir(join(destination, "runtime", "bin"))).resolves.toEqual([
52+
"tool.cmd"
53+
]);
54+
});
55+
56+
it("falls back through Windows native extractors before using the Node zip extractor", async () => {
57+
const root = await makeTempRoot();
58+
const archivePath = join(root, "fixture.zip");
59+
const destination = join(root, "extract");
60+
const attemptedCommands: string[] = [];
61+
62+
await writeFile(
63+
archivePath,
64+
createZipArchive([{ name: "runtime/bin/tool.cmd", contents: "tool" }])
65+
);
66+
67+
await extractZipArchive(archivePath, destination, {
68+
platform: "win32",
69+
runCommand: async (command: string) => {
70+
attemptedCommands.push(command);
71+
throw new Error(`${command} unavailable`);
72+
}
73+
});
74+
75+
expect(attemptedCommands).toEqual([
76+
"tar.exe",
77+
"tar",
78+
"pwsh",
79+
"powershell.exe",
80+
"powershell"
81+
]);
82+
await expect(readdir(join(destination, "runtime", "bin"))).resolves.toEqual([
83+
"tool.cmd"
84+
]);
85+
});
86+
});
87+
88+
async function makeTempRoot(): Promise<string> {
89+
const root = await mkdtemp(join(tmpdir(), "hagiscript-zip-extract-"));
90+
tempRoots.push(root);
91+
return root;
92+
}

0 commit comments

Comments
 (0)