Skip to content

Commit d8a4759

Browse files
committed
tests: add unit tests
1 parent 3947594 commit d8a4759

File tree

7 files changed

+123
-29
lines changed

7 files changed

+123
-29
lines changed

__tests__/os.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ describe("os resolver", () => {
1919
expect(mac.os).toBe(os.OS.MacOS);
2020
expect(mac.version).toBe("latest");
2121
expect(mac.name).toBe("macOS");
22+
23+
setSystem({ os: "win32", dist: "Windows", release: "latest" });
24+
25+
let windows = await os.getSystem();
26+
expect(windows.os).toBe(os.OS.Windows);
27+
expect(windows.version).toBe("latest");
28+
expect(windows.name).toBe("Windows");
2229
});
2330

2431
it("throws an error if the os is not supported", async () => {

__tests__/swift-versions.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as versions from "../src/swift-versions";
33

44
const macOS: System = { os: OS.MacOS, version: "latest", name: "macOS" };
55
const ubuntu: System = { os: OS.Ubuntu, version: "latest", name: "Ubuntu" };
6+
const windows: System = { os: OS.Windows, version: "latest", name: "Windows" };
67

78
describe("swift version resolver", () => {
89
it("identifies X.X.X versions", async () => {
@@ -39,12 +40,19 @@ describe("swift version resolver", () => {
3940
});
4041

4142
it("throws an error if the version isn't available for the system", async () => {
42-
expect.assertions(1);
43+
expect.assertions(2);
44+
4345
try {
4446
await versions.verify("5.0.3", macOS);
4547
} catch (e) {
4648
expect(e).toEqual(new Error('Version "5.0.3" is not available'));
4749
}
50+
51+
try {
52+
await versions.verify("5.2", windows);
53+
} catch (e) {
54+
expect(e).toEqual(new Error('Version "5.2" is not available'));
55+
}
4856
});
4957

5058
it("throws an error if version is invalid", async () => {

__tests__/visual-studio.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import os from "os";
2+
import * as vs from "../src/visual-studio";
3+
import { swiftPackage } from "../src/swift-versions";
4+
import { OS, System } from "../src/os";
5+
6+
jest.mock("fs", () => {
7+
const original = jest.requireActual("fs");
8+
return {
9+
...original,
10+
existsSync: jest.fn((path) => true),
11+
};
12+
});
13+
14+
const windows: System = { os: OS.Windows, version: "latest", name: "Windows" };
15+
16+
describe("visual studio resolver", () => {
17+
const env = process.env;
18+
19+
beforeEach(() => {
20+
jest.resetModules();
21+
process.env = { ...env };
22+
});
23+
24+
afterEach(() => {
25+
process.env = env;
26+
});
27+
28+
it("fetches visual studio requirement for swift version", async () => {
29+
jest.spyOn(os, "release").mockReturnValue("10.0.17763");
30+
31+
const req5_3 = vs.vsRequirement(swiftPackage("5.3", windows));
32+
expect(req5_3.version).toBe("16");
33+
expect(req5_3.components).toContain(
34+
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
35+
);
36+
expect(req5_3.components).toContain(
37+
"Microsoft.VisualStudio.Component.Windows10SDK.17763"
38+
);
39+
40+
const req5_6 = vs.vsRequirement(swiftPackage("5.6", windows));
41+
expect(req5_6.version).toBe("16");
42+
expect(req5_6.components).toContain(
43+
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
44+
);
45+
expect(req5_6.components).toContain(
46+
"Microsoft.VisualStudio.Component.Windows10SDK.17763"
47+
);
48+
});
49+
50+
it("adds latest sdk for release newer than or equal to build 17763", async () => {
51+
jest.spyOn(os, "release").mockReturnValue("10.0.17763");
52+
const req17763 = vs.vsRequirement(swiftPackage("5.3", windows));
53+
expect(req17763.components).toContain(
54+
"Microsoft.VisualStudio.Component.Windows10SDK.17763"
55+
);
56+
57+
jest.spyOn(os, "release").mockReturnValue("10.0.18363");
58+
const req18363 = vs.vsRequirement(swiftPackage("5.3", windows));
59+
expect(req18363.components).toContain(
60+
"Microsoft.VisualStudio.Component.Windows10SDK.18363"
61+
);
62+
});
63+
64+
it("adds recommended sdk for release older than build 17763", async () => {
65+
jest.spyOn(os, "release").mockReturnValue("10.0.16299");
66+
const req16299 = vs.vsRequirement(swiftPackage("5.3", windows));
67+
expect(req16299.components).toContain(
68+
"Microsoft.VisualStudio.Component.Windows10SDK.17763"
69+
);
70+
});
71+
72+
it("finds vswhere path from environment value", async () => {
73+
const vswherePath = "C:\\bin\\";
74+
const vswhereExe = "C:\\bin\\vswhere.exe";
75+
process.env.VSWHERE_PATH = vswherePath;
76+
expect(await vs.getVsWherePath()).toBe(vswhereExe);
77+
});
78+
79+
it("finds vswhere path from ProgramFiles environment value", async () => {
80+
const vswhereExe =
81+
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\vswhere.exe";
82+
process.env["ProgramFiles(x86)"] = "C:\\Program Files (x86)";
83+
expect(await vs.getVsWherePath()).toBe(vswhereExe);
84+
});
85+
});

dist/index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,8 +2347,7 @@ function install(version, system) {
23472347
const swiftLibPath = path.join(systemDrive, "Library");
23482348
const swiftInstallPath = path.join(swiftLibPath, "Developer", "Toolchains", "unknown-Asserts-development.xctoolchain", "usr\\bin");
23492349
if (code != 0 || !fs.existsSync(swiftInstallPath)) {
2350-
core.setFailed(`Swift installer failed with exit code: ${code}`);
2351-
return;
2350+
throw new Error(`Swift installer failed with exit code: ${code}`);
23522351
}
23532352
core.addPath(swiftInstallPath);
23542353
const additionalPaths = [
@@ -7625,7 +7624,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
76257624
});
76267625
};
76277626
Object.defineProperty(exports, "__esModule", { value: true });
7628-
exports.setupVsTools = void 0;
7627+
exports.getVsWherePath = exports.setupVsTools = exports.vsRequirement = void 0;
76297628
const os = __importStar(__webpack_require__(87));
76307629
const fs = __importStar(__webpack_require__(747));
76317630
const path = __importStar(__webpack_require__(622));
@@ -7649,6 +7648,7 @@ function vsRequirement({ version }) {
76497648
],
76507649
};
76517650
}
7651+
exports.vsRequirement = vsRequirement;
76527652
/// Do swift version based additional support files setup
76537653
function setupSupportFiles({ version }, vsInstallPath) {
76547654
return __awaiter(this, void 0, void 0, function* () {
@@ -7675,10 +7675,10 @@ function setupVsTools(pkg) {
76757675
/// https://github.com/microsoft/vswhere/wiki/Find-MSBuild
76767676
/// get visual studio properties
76777677
const vswhereExe = yield getVsWherePath();
7678-
const requirement = vsRequirement(pkg);
7678+
const req = vsRequirement(pkg);
76797679
const vsWhereExec = `-products * ` +
76807680
`-format json -utf8 ` +
7681-
`-latest -version "${requirement.version}"`;
7681+
`-latest -version "${req.version}"`;
76827682
let payload = "";
76837683
const options = {};
76847684
options.listeners = {
@@ -7693,19 +7693,17 @@ function setupVsTools(pkg) {
76937693
yield exec_1.exec(`"${vswhereExe}" ${vsWhereExec}`, [], options);
76947694
let vs = JSON.parse(payload)[0];
76957695
if (!vs.installationPath) {
7696-
core.setFailed(`Unable to find any visual studio installation for version: ${requirement.version}.`);
7697-
return;
7696+
throw new Error(`Unable to find any visual studio installation for version: ${req.version}.`);
76987697
}
76997698
/// https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022
77007699
/// install required visual studio components
77017700
const vsInstallerExec = `modify --installPath "${vs.installationPath}"` +
7702-
requirement.components.reduce((previous, current) => `${previous} --add "${current}"`, "") +
7701+
req.components.reduce((previous, current) => `${previous} --add "${current}"`, "") +
77037702
` --quiet`;
77047703
// install required visual studio components
77057704
const code = yield exec_1.exec(`"${vs.properties.setupEngineFilePath}" ${vsInstallerExec}`, []);
77067705
if (code != 0) {
7707-
core.setFailed(`Visual Studio installer failed to install required components with exit code: ${code}.`);
7708-
return;
7706+
throw new Error(`Visual Studio installer failed to install required components with exit code: ${code}.`);
77097707
}
77107708
yield setupSupportFiles(pkg, vs.installationPath);
77117709
});
@@ -7739,12 +7737,12 @@ function getVsWherePath() {
77397737
}
77407738
}
77417739
if (!fs.existsSync(vswhereToolExe)) {
7742-
core.setFailed("Action requires the path to where vswhere.exe exists");
7743-
return;
7740+
throw new Error("Action requires the path to where vswhere.exe exists");
77447741
}
77457742
return vswhereToolExe;
77467743
});
77477744
}
7745+
exports.getVsWherePath = getVsWherePath;
77487746

77497747

77507748
/***/ }),
@@ -7852,7 +7850,7 @@ var OS;
78527850
const AVAILABLE_OS = {
78537851
macOS: ["latest", "11.0", "10.15"],
78547852
Ubuntu: ["latest", "20.04", "18.04", "16.04"],
7855-
Windows: ["latest", "2022", "2019"],
7853+
Windows: ["latest", "10"],
78567854
};
78577855
function getSystem() {
78587856
return __awaiter(this, void 0, void 0, function* () {

src/os.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export namespace OS {
1515
const AVAILABLE_OS: { [platform: string]: string[] } = {
1616
macOS: ["latest", "11.0", "10.15"],
1717
Ubuntu: ["latest", "20.04", "18.04", "16.04"],
18-
Windows: ["latest", "2022", "2019"],
18+
Windows: ["latest", "10"],
1919
};
2020

2121
export interface System {

src/visual-studio.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface VsRequirement {
2929

3030
/// Setup different version and component requirement
3131
/// based on swift versions if required
32-
function vsRequirement({ version }: Package): VsRequirement {
32+
export function vsRequirement({ version }: Package): VsRequirement {
3333
const recVersion = "10.0.17763";
3434
const currentVersion = os.release();
3535
const useVersion = semver.gte(currentVersion, recVersion)
@@ -73,11 +73,11 @@ export async function setupVsTools(pkg: Package) {
7373
/// https://github.com/microsoft/vswhere/wiki/Find-MSBuild
7474
/// get visual studio properties
7575
const vswhereExe = await getVsWherePath();
76-
const requirement = vsRequirement(pkg);
76+
const req = vsRequirement(pkg);
7777
const vsWhereExec =
7878
`-products * ` +
7979
`-format json -utf8 ` +
80-
`-latest -version "${requirement.version}"`;
80+
`-latest -version "${req.version}"`;
8181

8282
let payload = "";
8383
const options: ExecOptions = {};
@@ -94,17 +94,16 @@ export async function setupVsTools(pkg: Package) {
9494
await exec(`"${vswhereExe}" ${vsWhereExec}`, [], options);
9595
let vs: VisualStudio = JSON.parse(payload)[0];
9696
if (!vs.installationPath) {
97-
core.setFailed(
98-
`Unable to find any visual studio installation for version: ${requirement.version}.`
97+
throw new Error(
98+
`Unable to find any visual studio installation for version: ${req.version}.`
9999
);
100-
return;
101100
}
102101

103102
/// https://docs.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022
104103
/// install required visual studio components
105104
const vsInstallerExec =
106105
`modify --installPath "${vs.installationPath}"` +
107-
requirement.components.reduce(
106+
req.components.reduce(
108107
(previous, current) => `${previous} --add "${current}"`,
109108
""
110109
) +
@@ -116,10 +115,9 @@ export async function setupVsTools(pkg: Package) {
116115
[]
117116
);
118117
if (code != 0) {
119-
core.setFailed(
118+
throw new Error(
120119
`Visual Studio installer failed to install required components with exit code: ${code}.`
121120
);
122-
return;
123121
}
124122

125123
await setupSupportFiles(pkg, vs.installationPath);
@@ -128,7 +126,7 @@ export async function setupVsTools(pkg: Package) {
128126
/// Get vswhere and vs_installer paths
129127
/// Borrowed from setup-msbuild action: https://github.com/microsoft/setup-msbuild
130128
/// From source file: https://github.com/microsoft/setup-msbuild/blob/master/src/main.ts
131-
async function getVsWherePath() {
129+
export async function getVsWherePath() {
132130
// check to see if we are using a specific path for vswhere
133131
let vswhereToolExe = "";
134132
// Env variable for self-hosted runner to provide custom path
@@ -155,8 +153,7 @@ async function getVsWherePath() {
155153
}
156154

157155
if (!fs.existsSync(vswhereToolExe)) {
158-
core.setFailed("Action requires the path to where vswhere.exe exists");
159-
return;
156+
throw new Error("Action requires the path to where vswhere.exe exists");
160157
}
161158

162159
return vswhereToolExe;

src/windows-install.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ export async function install(version: string, system: System) {
6161
);
6262

6363
if (code != 0 || !fs.existsSync(swiftInstallPath)) {
64-
core.setFailed(`Swift installer failed with exit code: ${code}`);
65-
return;
64+
throw new Error(`Swift installer failed with exit code: ${code}`);
6665
}
6766

6867
core.addPath(swiftInstallPath);

0 commit comments

Comments
 (0)