Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package/src/common/update-pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 12 additions & 16 deletions package/src/util/utils.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -28,19 +28,15 @@ export async function download(src: string, dest: string): Promise<void> {
file.close();
}

export async function unzip(
zipFile: string,
dest: string,
): Promise<CmdResult> {
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<void> {
// 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 ?? ""}`,
);
}
}
53 changes: 53 additions & 0 deletions tests/unit/package-unzip.test.ts
Original file line number Diff line number Diff line change
@@ -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");
},
);
Loading