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
17 changes: 11 additions & 6 deletions scripts/prebuilt-package-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ export function getPlatformPackageSpecByName(packageName: string) {
return PLATFORM_PACKAGE_MATRIX.find((candidate) => candidate.packageName === packageName);
}

/** Return the Hunk package spec that matches the current machine. */
export function getHostPlatformPackageSpec() {
const normalizedPlatform = normalizeHostPlatform(os.platform());
/** Resolve the published package spec for a given Node platform/architecture pair. */
export function getPlatformPackageSpecForHost(platform: NodeJS.Platform, arch: NodeJS.Architecture) {
const normalizedPlatform = normalizeHostPlatform(platform);
if (!normalizedPlatform) {
throw new Error(`Unsupported host platform for prebuilt packaging: ${os.platform()}`);
throw new Error(`Unsupported host platform for prebuilt packaging: ${platform}`);
}

const normalizedArch = normalizeHostArch(os.arch());
const normalizedArch = normalizeHostArch(arch);
if (!normalizedArch) {
throw new Error(`Unsupported host architecture for prebuilt packaging: ${os.arch()}`);
throw new Error(`Unsupported host architecture for prebuilt packaging: ${arch}`);
}

const spec = PLATFORM_PACKAGE_MATRIX.find((candidate) => candidate.os === normalizedPlatform && candidate.cpu === normalizedArch);
Expand All @@ -67,6 +67,11 @@ export function getHostPlatformPackageSpec() {
return spec;
}

/** Return the Hunk package spec that matches the current machine. */
export function getHostPlatformPackageSpec() {
return getPlatformPackageSpecForHost(os.platform(), os.arch());
}

/** Build the optional dependency map for the top-level hunkdiff package. */
export function buildOptionalDependencyMap(version: string, specs: readonly PlatformPackageSpec[] = PLATFORM_PACKAGE_MATRIX) {
return Object.fromEntries(specs.map((spec) => [spec.packageName, version]));
Expand Down
43 changes: 43 additions & 0 deletions test/prebuilt-package-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import {
PLATFORM_PACKAGE_MATRIX,
binaryFilenameForSpec,
buildOptionalDependencyMap,
getHostPlatformPackageSpec,
getPlatformPackageSpecByName,
getPlatformPackageSpecForHost,
normalizeHostArch,
normalizeHostPlatform,
sortPlatformPackageSpecs,
type PlatformPackageSpec,
} from "../scripts/prebuilt-package-helpers";

describe("prebuilt package helpers", () => {
Expand All @@ -22,12 +27,50 @@ describe("prebuilt package helpers", () => {
}
});

test("binaryFilenameForSpec adds .exe for windows packages", () => {
const windowsSpec: PlatformPackageSpec = {
packageName: "hunkdiff-windows-x64",
os: "windows",
cpu: "x64",
binaryName: "hunk",
binaryRelativePath: "bin/hunk.exe",
};

expect(binaryFilenameForSpec(windowsSpec)).toBe("hunk.exe");
});

test("normalizeHostPlatform and normalizeHostArch reject unsupported values", () => {
expect(normalizeHostPlatform("linux")).toBe("linux");
expect(normalizeHostPlatform("win32")).toBe("windows");
expect(normalizeHostPlatform("freebsd" as NodeJS.Platform)).toBeUndefined();

expect(normalizeHostArch("x64")).toBe("x64");
expect(normalizeHostArch("arm64")).toBe("arm64");
expect(normalizeHostArch("ia32" as NodeJS.Architecture)).toBeUndefined();
});

test("getPlatformPackageSpecByName returns known package specs", () => {
expect(getPlatformPackageSpecByName("hunkdiff-linux-x64")?.cpu).toBe("x64");
expect(getPlatformPackageSpecByName("hunkdiff-darwin-arm64")?.os).toBe("darwin");
expect(getPlatformPackageSpecByName("hunkdiff-does-not-exist")).toBeUndefined();
});

test("getPlatformPackageSpecForHost resolves supported combinations and rejects unsupported ones", () => {
expect(getPlatformPackageSpecForHost("linux", "x64").packageName).toBe("hunkdiff-linux-x64");
expect(getPlatformPackageSpecForHost("darwin", "arm64").packageName).toBe("hunkdiff-darwin-arm64");
expect(() => getPlatformPackageSpecForHost("freebsd" as NodeJS.Platform, "x64")).toThrow(
"Unsupported host platform for prebuilt packaging: freebsd",
);
expect(() => getPlatformPackageSpecForHost("linux", "ia32" as NodeJS.Architecture)).toThrow(
"Unsupported host architecture for prebuilt packaging: ia32",
);
expect(() => getPlatformPackageSpecForHost("linux", "arm64")).toThrow("No published prebuilt package spec matches linux/arm64");
});

test("getHostPlatformPackageSpec resolves the current machine", () => {
expect(getHostPlatformPackageSpec()).toEqual(getPlatformPackageSpecForHost(process.platform, process.arch));
});

test("sortPlatformPackageSpecs keeps package publish order stable", () => {
const reversed = [...PLATFORM_PACKAGE_MATRIX].reverse();
expect(sortPlatformPackageSpecs(reversed).map((spec) => spec.packageName)).toEqual([
Expand Down
Loading