diff --git a/package/src/common/update-pandoc.ts b/package/src/common/update-pandoc.ts index 560394ca343..32114d667f5 100644 --- a/package/src/common/update-pandoc.ts +++ b/package/src/common/update-pandoc.ts @@ -250,7 +250,7 @@ async function writePandocTemplates( `https://github.com/jgm/pandoc/archive/refs/tags/${version}.zip`; const pandocDir = `pandoc-${version}`; - const zipFile = join(workingDir, "pandoc"); + const zipFile = join(workingDir, "pandoc.zip"); await download(srcZipUrl, zipFile); await unzip(zipFile, workingDir); diff --git a/package/src/util/utils.ts b/package/src/util/utils.ts index 2e896a70b63..44cbbe80e94 100644 --- a/package/src/util/utils.ts +++ b/package/src/util/utils.ts @@ -1,6 +1,6 @@ import { writeAll } from "io/write-all"; -import { CmdResult, runCmd } from "./cmd.ts"; -import { isWindows } from "../../../src/deno_ral/platform.ts"; +import { ensureDirSync } from "../../../src/deno_ral/fs.ts"; +import { unzip as coreUnzip } from "../../../src/core/zip.ts"; // Read an environment variable export function getEnv(name: string, defaultValue?: string) { @@ -28,19 +28,15 @@ export async function download(src: string, dest: string): Promise { file.close(); } -export async function unzip( - zipFile: string, - dest: string, -): Promise { - if (isWindows) { - return await runCmd("PowerShell", [ - "Expand-Archive", - "-Path", - `"${zipFile}"`, - "-DestinationPath", - `"${dest}"`, - ]); - } else { - return await runCmd("unzip", [zipFile, "-d", dest]); +// Expand-Archive is avoided: Windows PowerShell 5.1 can load PowerShell 7's +// Archive module from an inherited PSModulePath, fail, and still exit 0. +export async function unzip(zipFile: string, dest: string): Promise { + // the core helper runs unzip with dest as cwd on Linux/macOS + ensureDirSync(dest); + const result = await coreUnzip(zipFile, dest); + if (!result.success) { + throw new Error( + `Failed to extract ${zipFile} to ${dest}:\n${result.stderr ?? ""}`, + ); } } diff --git a/tests/unit/package-unzip.test.ts b/tests/unit/package-unzip.test.ts new file mode 100644 index 00000000000..bf45b1240ed --- /dev/null +++ b/tests/unit/package-unzip.test.ts @@ -0,0 +1,53 @@ +/* + * package-unzip.test.ts + * + * Tests for the build-time unzip helper used by configure + * (package/src/util/utils.ts). + * + * Copyright (C) 2026 Posit Software, PBC + */ + +import { unitTest } from "../test.ts"; +import { assert, assertRejects } from "../asserts.ts"; +import { existsSync } from "../../src/deno_ral/fs.ts"; +import { join } from "../../src/deno_ral/path.ts"; +import { zip } from "../../src/core/zip.ts"; +import { withTempDir, writeFixture } from "../utils.ts"; +import { unzip } from "../../package/src/util/utils.ts"; + +unitTest( + "package unzip - extracts a zip into a destination that does not exist yet", + async () => { + await withTempDir(async (dir) => { + writeFixture(dir, "src/pandoc-x/pandoc.exe", "binary"); + const archive = join(dir, "pandoc.zip"); + const created = await zip("pandoc-x", archive, { + cwd: join(dir, "src"), + }); + assert(created.success, `fixture zip failed: ${created.stderr}`); + + const dest = join(dir, "out"); + await unzip(archive, dest); + + assert( + existsSync(join(dest, "pandoc-x", "pandoc.exe")), + "pandoc-x/pandoc.exe was not extracted", + ); + }, "quarto-package-unzip-test"); + }, +); + +unitTest( + "package unzip - throws naming the archive when extraction fails", + async () => { + await withTempDir(async (dir) => { + writeFixture(dir, "corrupt.zip", "this is not a zip archive"); + const archive = join(dir, "corrupt.zip"); + await assertRejects( + () => unzip(archive, join(dir, "out")), + Error, + "corrupt.zip", + ); + }, "quarto-package-unzip-test"); + }, +);