Skip to content

Commit f481d7f

Browse files
authored
Merge pull request #139 from fwal/system-tracking
Track supported OS:es for each Swift version
2 parents 0b65615 + 49cce36 commit f481d7f

File tree

4 files changed

+85
-48
lines changed

4 files changed

+85
-48
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
strategy:
2424
matrix:
2525
os: [ubuntu-latest, macos-latest]
26-
swift: ["5.3", "4.2.4"]
26+
swift: ["5.3", "4.2.1"]
2727
steps:
2828
- uses: actions/checkout@v2
2929
- run: npm install

__tests__/swift-versions.test.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,56 @@
1+
import { OS, System } from "../src/os";
12
import * as versions from "../src/swift-versions";
23

4+
const macOS: System = { os: OS.MacOS, version: "latest", name: "macOS" };
5+
const ubuntu: System = { os: OS.Ubuntu, version: "latest", name: "Ubuntu" };
6+
37
describe("swift version resolver", () => {
48
it("identifies X.X.X versions", async () => {
5-
const version = await versions.verify("5.0.3");
6-
expect(version).toBe("5.0.3");
9+
const version = await versions.verify("5.0.1", macOS);
10+
expect(version).toBe("5.0.1");
711
});
812

913
it("identifies X.X.0 versions", async () => {
10-
const version = await versions.verify("5.0.0");
14+
const version = await versions.verify("5.0.0", macOS);
1115
expect(version).toBe("5.0");
1216
});
1317

1418
it("identifies X.X versions", async () => {
15-
const version = await versions.verify("5.0");
16-
expect(version).toBe("5.0.3");
19+
const version = await versions.verify("5.0", macOS);
20+
expect(version).toBe("5.0.1");
1721
});
1822

1923
it("identifies ~X.X versions", async () => {
20-
const version = await versions.verify("~5.0");
21-
expect(version).toBe("5.0.3");
24+
const version = await versions.verify("~5.0", macOS);
25+
expect(version).toBe("5.0.1");
2226
});
2327

2428
it("identifies X versions", async () => {
25-
const version = await versions.verify("5");
29+
const version = await versions.verify("5", macOS);
2630
expect(version).toBe("5.3");
2731
});
2832

33+
it("identifies versions based on system", async () => {
34+
const macVersion = await versions.verify("5.0", macOS);
35+
expect(macVersion).toBe("5.0.1");
36+
37+
const ubuntuVersion = await versions.verify("5.0", ubuntu);
38+
expect(ubuntuVersion).toBe("5.0.3");
39+
});
40+
41+
it("throws an error if the version isn't available for the system", async () => {
42+
expect.assertions(1);
43+
try {
44+
await versions.verify("5.0.3", macOS);
45+
} catch (e) {
46+
expect(e).toEqual(new Error('Version "5.0.3" is not available'));
47+
}
48+
});
49+
2950
it("throws an error if version is invalid", async () => {
3051
expect.assertions(1);
3152
try {
32-
await versions.verify("foo");
53+
await versions.verify("foo", macOS);
3354
} catch (e) {
3455
expect(e).toEqual(new Error("Version must be a valid semver format."));
3556
}
@@ -38,7 +59,7 @@ describe("swift version resolver", () => {
3859
it("throws an error if no matching version is found", async () => {
3960
expect.assertions(1);
4061
try {
41-
await versions.verify("1.0");
62+
await versions.verify("1.0", macOS);
4263
} catch (e) {
4364
expect(e).toEqual(new Error('Version "1.0" is not available'));
4465
}

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import { getVersion } from "./get-version";
99
async function run() {
1010
try {
1111
const requestedVersion = core.getInput("swift-version", { required: true });
12-
let version = versions.verify(requestedVersion);
1312

1413
let platform = await system.getSystem();
14+
let version = versions.verify(requestedVersion, platform);
15+
1516
switch (platform.os) {
1617
case system.OS.MacOS:
1718
await macos.install(version, platform);

src/swift-versions.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,51 @@ import * as semver from "semver";
22
import * as core from "@actions/core";
33
import { System, OS } from "./os";
44

5-
const AVAILABLE_VERSIONS = [
6-
"5.3",
7-
"5.2.4",
8-
"5.2.2",
9-
"5.2.1",
10-
"5.2",
11-
"5.1.1",
12-
"5.1",
13-
"5.0.3",
14-
"5.0.2",
15-
"5.0.1",
16-
"5.0",
17-
"4.2.4",
18-
"4.2.3",
19-
"4.2.2",
20-
"4.2.1",
21-
"4.2",
22-
"4.1.3",
23-
"4.1.2",
24-
"4.1.1",
25-
"4.1",
26-
"4.0.3",
27-
"4.0.2",
28-
"4.0",
29-
"3.1.1",
30-
"3.1",
31-
"3.0.2",
32-
"3.0.1",
33-
"3.0",
34-
"2.2.1",
35-
"2.2",
36-
]
37-
.map((version) => semver.coerce(version))
38-
.filter(notEmpty);
5+
const VERSIONS_LIST: [string, OS[]][] = [
6+
["5.3", [OS.MacOS, OS.Ubuntu]],
7+
["5.2.5", [OS.Ubuntu]],
8+
["5.2.4", [OS.MacOS, OS.Ubuntu]],
9+
["5.2.3", [OS.Ubuntu]],
10+
["5.2.2", [OS.MacOS, OS.Ubuntu]],
11+
["5.2.1", [OS.Ubuntu]],
12+
["5.2", [OS.MacOS, OS.Ubuntu]],
13+
["5.1.5", [OS.Ubuntu]],
14+
["5.1.4", [OS.Ubuntu]],
15+
["5.1.3", [OS.MacOS, OS.Ubuntu]],
16+
["5.1.2", [OS.MacOS, OS.Ubuntu]],
17+
["5.1.1", [OS.Ubuntu]],
18+
["5.1", [OS.MacOS, OS.Ubuntu]],
19+
["5.0.3", [OS.Ubuntu]],
20+
["5.0.2", [OS.Ubuntu]],
21+
["5.0.1", [OS.MacOS, OS.Ubuntu]],
22+
["5.0", [OS.MacOS, OS.Ubuntu]],
23+
["4.2.4", [OS.Ubuntu]],
24+
["4.2.3", [OS.Ubuntu]],
25+
["4.2.2", [OS.Ubuntu]],
26+
["4.2.1", [OS.MacOS, OS.Ubuntu]],
27+
["4.2", [OS.MacOS, OS.Ubuntu]],
28+
["4.1.3", [OS.Ubuntu]],
29+
["4.1.2", [OS.MacOS, OS.Ubuntu]],
30+
["4.1.1", [OS.Ubuntu]],
31+
["4.1", [OS.MacOS, OS.Ubuntu]],
32+
["4.0.3", [OS.MacOS, OS.Ubuntu]],
33+
["4.0.2", [OS.MacOS, OS.Ubuntu]],
34+
["4.0", [OS.MacOS, OS.Ubuntu]],
35+
["3.1.1", [OS.MacOS, OS.Ubuntu]],
36+
["3.1", [OS.MacOS, OS.Ubuntu]],
37+
["3.0.2", [OS.MacOS, OS.Ubuntu]],
38+
["3.0.1", [OS.MacOS, OS.Ubuntu]],
39+
["3.0", [OS.MacOS, OS.Ubuntu]],
40+
["2.2.1", [OS.MacOS, OS.Ubuntu]],
41+
["2.2", [OS.MacOS, OS.Ubuntu]],
42+
];
43+
44+
const AVAILABLE_VERSIONS: [semver.SemVer, OS[]][] = VERSIONS_LIST.map(
45+
([version, os]) => {
46+
const semverVersion = semver.coerce(version);
47+
return <[semver.SemVer, OS[]]>[semverVersion, os];
48+
}
49+
);
3950

4051
function notEmpty<T>(value: T | null | undefined): value is T {
4152
return value !== null && value !== undefined;
@@ -72,15 +83,19 @@ export function swiftPackage(version: string, system: System): Package {
7283
};
7384
}
7485

75-
export function verify(version: string) {
86+
export function verify(version: string, system: System) {
7687
let range = semver.validRange(version);
7788
if (range === null) {
7889
throw new Error("Version must be a valid semver format.");
7990
}
8091

8192
core.debug(`Resolved range ${range}`);
8293

83-
let matchingVersion = evaluateVersions(AVAILABLE_VERSIONS, version);
94+
let systemVersions = AVAILABLE_VERSIONS.filter(([_, os]) =>
95+
os.includes(system.os)
96+
).map(([version, _]) => version);
97+
98+
let matchingVersion = evaluateVersions(systemVersions, version);
8499
if (matchingVersion === null) {
85100
throw new Error(`Version "${version}" is not available`);
86101
}

0 commit comments

Comments
 (0)