Skip to content

Commit d72ac59

Browse files
committed
Expand prebuilt helper edge coverage
1 parent 7644976 commit d72ac59

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

scripts/prebuilt-package-helpers.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ export function getPlatformPackageSpecByName(packageName: string) {
4747
return PLATFORM_PACKAGE_MATRIX.find((candidate) => candidate.packageName === packageName);
4848
}
4949

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

57-
const normalizedArch = normalizeHostArch(os.arch());
57+
const normalizedArch = normalizeHostArch(arch);
5858
if (!normalizedArch) {
59-
throw new Error(`Unsupported host architecture for prebuilt packaging: ${os.arch()}`);
59+
throw new Error(`Unsupported host architecture for prebuilt packaging: ${arch}`);
6060
}
6161

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

70+
/** Return the Hunk package spec that matches the current machine. */
71+
export function getHostPlatformPackageSpec() {
72+
return getPlatformPackageSpecForHost(os.platform(), os.arch());
73+
}
74+
7075
/** Build the optional dependency map for the top-level hunkdiff package. */
7176
export function buildOptionalDependencyMap(version: string, specs: readonly PlatformPackageSpec[] = PLATFORM_PACKAGE_MATRIX) {
7277
return Object.fromEntries(specs.map((spec) => [spec.packageName, version]));

test/prebuilt-package-helpers.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ import {
33
PLATFORM_PACKAGE_MATRIX,
44
binaryFilenameForSpec,
55
buildOptionalDependencyMap,
6+
getHostPlatformPackageSpec,
67
getPlatformPackageSpecByName,
8+
getPlatformPackageSpecForHost,
9+
normalizeHostArch,
10+
normalizeHostPlatform,
711
sortPlatformPackageSpecs,
12+
type PlatformPackageSpec,
813
} from "../scripts/prebuilt-package-helpers";
914

1015
describe("prebuilt package helpers", () => {
@@ -22,12 +27,50 @@ describe("prebuilt package helpers", () => {
2227
}
2328
});
2429

30+
test("binaryFilenameForSpec adds .exe for windows packages", () => {
31+
const windowsSpec: PlatformPackageSpec = {
32+
packageName: "hunkdiff-windows-x64",
33+
os: "windows",
34+
cpu: "x64",
35+
binaryName: "hunk",
36+
binaryRelativePath: "bin/hunk.exe",
37+
};
38+
39+
expect(binaryFilenameForSpec(windowsSpec)).toBe("hunk.exe");
40+
});
41+
42+
test("normalizeHostPlatform and normalizeHostArch reject unsupported values", () => {
43+
expect(normalizeHostPlatform("linux")).toBe("linux");
44+
expect(normalizeHostPlatform("win32")).toBe("windows");
45+
expect(normalizeHostPlatform("freebsd" as NodeJS.Platform)).toBeUndefined();
46+
47+
expect(normalizeHostArch("x64")).toBe("x64");
48+
expect(normalizeHostArch("arm64")).toBe("arm64");
49+
expect(normalizeHostArch("ia32" as NodeJS.Architecture)).toBeUndefined();
50+
});
51+
2552
test("getPlatformPackageSpecByName returns known package specs", () => {
2653
expect(getPlatformPackageSpecByName("hunkdiff-linux-x64")?.cpu).toBe("x64");
2754
expect(getPlatformPackageSpecByName("hunkdiff-darwin-arm64")?.os).toBe("darwin");
2855
expect(getPlatformPackageSpecByName("hunkdiff-does-not-exist")).toBeUndefined();
2956
});
3057

58+
test("getPlatformPackageSpecForHost resolves supported combinations and rejects unsupported ones", () => {
59+
expect(getPlatformPackageSpecForHost("linux", "x64").packageName).toBe("hunkdiff-linux-x64");
60+
expect(getPlatformPackageSpecForHost("darwin", "arm64").packageName).toBe("hunkdiff-darwin-arm64");
61+
expect(() => getPlatformPackageSpecForHost("freebsd" as NodeJS.Platform, "x64")).toThrow(
62+
"Unsupported host platform for prebuilt packaging: freebsd",
63+
);
64+
expect(() => getPlatformPackageSpecForHost("linux", "ia32" as NodeJS.Architecture)).toThrow(
65+
"Unsupported host architecture for prebuilt packaging: ia32",
66+
);
67+
expect(() => getPlatformPackageSpecForHost("linux", "arm64")).toThrow("No published prebuilt package spec matches linux/arm64");
68+
});
69+
70+
test("getHostPlatformPackageSpec resolves the current machine", () => {
71+
expect(getHostPlatformPackageSpec()).toEqual(getPlatformPackageSpecForHost(process.platform, process.arch));
72+
});
73+
3174
test("sortPlatformPackageSpecs keeps package publish order stable", () => {
3275
const reversed = [...PLATFORM_PACKAGE_MATRIX].reverse();
3376
expect(sortPlatformPackageSpecs(reversed).map((spec) => spec.packageName)).toEqual([

0 commit comments

Comments
 (0)