|
| 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