mirror of
https://github.com/github/codeql-action.git
synced 2026-10-21 02:01:27 +00:00
Add macOSVersion helper
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import test from "ava";
|
||||
|
||||
import { BundlePlatform, getBundlePlatform } from "./platform";
|
||||
import { BundlePlatform, getBundlePlatform, macOSVersion } from "./platform";
|
||||
import { initAllState } from "./testing-utils";
|
||||
|
||||
for (const [platform, arch, expected] of [
|
||||
["linux", "x64", BundlePlatform.Linux64],
|
||||
@@ -16,3 +17,40 @@ for (const [platform, arch, expected] of [
|
||||
t.is(getBundlePlatform(platform, arch), expected);
|
||||
});
|
||||
}
|
||||
|
||||
const platforms: NodeJS.Platform[] = ["linux", "win32", "freebsd"];
|
||||
for (const platform of platforms) {
|
||||
test(`macOSVersion returns undefined on ${platform}`, (t) => {
|
||||
t.is(macOSVersion(initAllState({ platform })), undefined);
|
||||
});
|
||||
}
|
||||
|
||||
test("macOSVersion returns raw string if semver parsing fails", (t) => {
|
||||
const invalidSemVer = "sealOS-2026";
|
||||
t.is(
|
||||
macOSVersion(
|
||||
initAllState({ platform: "darwin", osRelease: invalidSemVer }),
|
||||
),
|
||||
invalidSemVer,
|
||||
);
|
||||
});
|
||||
|
||||
test("macOSVersion returns semver if parsing succeeds", (t) => {
|
||||
const validSemVer = "27.0.1";
|
||||
|
||||
const version = macOSVersion(
|
||||
initAllState({ platform: "darwin", osRelease: validSemVer }),
|
||||
);
|
||||
|
||||
// Check that `version` is not undefined and narrow the type; throws if undefined.
|
||||
if (t.truthy(version)) {
|
||||
// Check that it's also not just a string.
|
||||
t.not(typeof version, "string");
|
||||
|
||||
// Should be an object with the expected properties.
|
||||
t.is(typeof version, "object");
|
||||
t.is(version["major"], 27);
|
||||
t.is(version["minor"], 0);
|
||||
t.is(version["patch"], 1);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import * as semver from "semver";
|
||||
|
||||
import type { ActionState } from "./action-common";
|
||||
|
||||
/** Platform identifiers used in CodeQL bundle asset names. */
|
||||
export enum BundlePlatform {
|
||||
Linux64 = "linux64",
|
||||
@@ -24,3 +28,28 @@ export function getBundlePlatform(
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to determine the version of macOS.
|
||||
*
|
||||
* @returns
|
||||
* The version as either a semantic version object, the raw version string
|
||||
* if it is not a semantic version, or `undefined` if we are not on macOS.
|
||||
*/
|
||||
export function macOSVersion(
|
||||
action: ActionState<["Base"]>,
|
||||
): semver.SemVer | string | undefined {
|
||||
// Skip if we are not running on macOS.
|
||||
if (action.platform !== "darwin") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Try to parse the OS version string.
|
||||
const version = semver.parse(action.osRelease);
|
||||
|
||||
if (version === null) {
|
||||
return action.osRelease;
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user