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
5 changes: 5 additions & 0 deletions tests/pack-result.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface PackRecord {
files: Array<{ path: string }>;
}

export function readPackRecord(result: unknown): PackRecord;
27 changes: 27 additions & 0 deletions tests/pack-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** @typedef {{ files: Array<{ path: string }> }} PackRecord */

/** Normalize the array and name-keyed object shapes used by npm pack --json. */
export function readPackRecord(result) {
const records = Array.isArray(result)
? result
: result !== null && typeof result === "object"
? Object.values(result)
: [];

if (records.length !== 1)
throw new Error(`Expected one packed artifact, received ${records.length}.`);

const record = records[0];
if (
record === null ||
typeof record !== "object" ||
!Array.isArray(record.files) ||
!record.files.every(
(file) => file !== null && typeof file === "object" && typeof file.path === "string",
)
) {
throw new Error("npm pack returned an invalid artifact record.");
}

return /** @type {PackRecord} */ (record);
}
26 changes: 26 additions & 0 deletions tests/pack-result.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from "vitest";
import { readPackRecord } from "./pack-result.js";

const record = { filename: "package.tgz", files: [{ path: "dist/index.js" }] };

describe("npm pack JSON normalization", () => {
it.each([[record], { "@askrjs/node": record }])(
"should read one artifact from the supported npm result shape",
(result) => {
expect(readPackRecord(result)).toBe(record);
},
);

it.each([[], [record, record], {}, null, "invalid"])(
"should reject a result without exactly one artifact",
(result) => {
expect(() => readPackRecord(result)).toThrow(/Expected one packed artifact/);
},
);

it("should reject malformed artifact records", () => {
expect(() => readPackRecord([{ files: [{ path: 1 }] }])).toThrow(
"npm pack returned an invalid artifact record.",
);
});
});
19 changes: 9 additions & 10 deletions tests/package-artifacts.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { dirname, join, normalize } from "node:path";
import { readPackRecord } from "./pack-result.js";

const npmCli = process.env.npm_execpath;
if (!npmCli) throw new Error("npm_execpath is unavailable; run this check through npm");
const result = JSON.parse(
execFileSync(process.execPath, [npmCli, "pack", "--ignore-scripts", "--dry-run", "--json"], {
encoding: "utf8",
}),
const result = readPackRecord(
JSON.parse(
execFileSync(process.execPath, [npmCli, "pack", "--ignore-scripts", "--dry-run", "--json"], {
encoding: "utf8",
}),
),
);

const manifest = JSON.parse(readFileSync("package.json", "utf8"));
Expand All @@ -17,11 +20,7 @@ if (JSON.stringify(dependencies) !== JSON.stringify(allowedDependencies)) {
throw new Error(`Unexpected production dependencies: ${dependencies.join(", ")}`);
}

if (result.length !== 1) {
throw new Error(`Expected one packed artifact, received ${result.length}.`);
}

const packedFiles = new Set(result[0].files.map(({ path }) => normalize(path)));
const packedFiles = new Set(result.files.map(({ path }) => normalize(path)));
for (const required of [
"LICENSE",
"README.md",
Expand All @@ -45,7 +44,7 @@ for (const file of packedFiles) {
}
const sourceMappingPattern = /[#@]\s*sourceMappingURL=([^\s*]+)/gu;

for (const file of result[0].files) {
for (const file of result.files) {
if (!/\.(?:css|d\.ts|js)$/u.test(file.path)) continue;

const source = readFileSync(file.path, "utf8");
Expand Down
Loading