Skip to content

Commit 7db9c30

Browse files
committed
Add macOSVersion helper
1 parent eaca282 commit 7db9c30

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎src/platform.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "ava";
22

3-
import { BundlePlatform, getBundlePlatform } from "./platform";
3+
import { BundlePlatform, getBundlePlatform, macOSVersion } from "./platform";
4+
import { initAllState } from "./testing-utils";
45

56
for (const [platform, arch, expected] of [
67
["linux", "x64", BundlePlatform.Linux64],
@@ -16,3 +17,40 @@ for (const [platform, arch, expected] of [
1617
t.is(getBundlePlatform(platform, arch), expected);
1718
});
1819
}
20+
21+
const platforms: NodeJS.Platform[] = ["linux", "win32", "freebsd"];
22+
for (const platform of platforms) {
23+
test(`macOSVersion returns undefined on ${platform}`, (t) => {
24+
t.is(macOSVersion(initAllState({ platform })), undefined);
25+
});
26+
}
27+
28+
test("macOSVersion returns raw string if semver parsing fails", (t) => {
29+
const invalidSemVer = "sealOS-2026";
30+
t.is(
31+
macOSVersion(
32+
initAllState({ platform: "darwin", osRelease: invalidSemVer }),
33+
),
34+
invalidSemVer,
35+
);
36+
});
37+
38+
test("macOSVersion returns semver if parsing succeeds", (t) => {
39+
const validSemVer = "27.0.1";
40+
41+
const version = macOSVersion(
42+
initAllState({ platform: "darwin", osRelease: validSemVer }),
43+
);
44+
45+
// Check that `version` is not undefined and narrow the type; throws if undefined.
46+
if (t.truthy(version)) {
47+
// Check that it's also not just a string.
48+
t.not(typeof version, "string");
49+
50+
// Should be an object with the expected properties.
51+
t.is(typeof version, "object");
52+
t.is(version["major"], 27);
53+
t.is(version["minor"], 0);
54+
t.is(version["patch"], 1);
55+
}
56+
});

‎src/platform.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import * as semver from "semver";
2+
3+
import type { ActionState } from "./action-common";
4+
15
/** Platform identifiers used in CodeQL bundle asset names. */
26
export enum BundlePlatform {
37
Linux64 = "linux64",
@@ -24,3 +28,28 @@ export function getBundlePlatform(
2428
return undefined;
2529
}
2630
}
31+
32+
/**
33+
* Tries to determine the version of macOS.
34+
*
35+
* @returns
36+
* The version as either a semantic version object, the raw version string
37+
* if it is not a semantic version, or `undefined` if we are not on macOS.
38+
*/
39+
export function macOSVersion(
40+
action: ActionState<["Base"]>,
41+
): semver.SemVer | string | undefined {
42+
// Skip if we are not running on macOS.
43+
if (action.platform !== "darwin") {
44+
return undefined;
45+
}
46+
47+
// Try to parse the OS version string.
48+
const version = semver.parse(action.osRelease);
49+
50+
if (version === null) {
51+
return action.osRelease;
52+
}
53+
54+
return version;
55+
}

0 commit comments

Comments
 (0)