-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathxamarin-selector.test.ts
173 lines (153 loc) · 6.89 KB
/
xamarin-selector.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
import * as utils from "../src/utils";
import { MonoToolSelector } from "../src/mono-selector";
import { XamarinIosToolSelector } from "../src/xamarin-ios-selector";
import { XamarinMacToolSelector } from "../src/xamarin-mac-selector";
import { XamarinAndroidToolSelector } from "../src/xamarin-android-selector";
import { ToolSelector } from "../src/tool-selector";
import { VersionUtils } from "../src/version-utils";
jest.mock("fs");
jest.mock("@actions/core");
jest.mock("../src/utils");
const buildFsDirentItem = (name: string, opt: { isSymbolicLink: boolean; isDirectory: boolean }): fs.Dirent => {
return {
name,
isSymbolicLink: () => opt.isSymbolicLink,
isDirectory: () => opt.isDirectory
} as fs.Dirent;
};
const fakeReadDirResults = [
buildFsDirentItem("13.10", { isSymbolicLink: true, isDirectory: true }),
buildFsDirentItem("13.10.0.21", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("13.4", { isSymbolicLink: true, isDirectory: false }),
buildFsDirentItem("13.4.0.2", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("13.6", { isSymbolicLink: true, isDirectory: false }),
buildFsDirentItem("13.6.0.12", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("6_4_0", { isSymbolicLink: false, isDirectory: false }),
buildFsDirentItem("6_6_0", { isSymbolicLink: true, isDirectory: true }),
buildFsDirentItem("third_party_folder", { isSymbolicLink: false, isDirectory: true }),
buildFsDirentItem("Current", { isSymbolicLink: true, isDirectory: true }),
buildFsDirentItem("Latest", { isSymbolicLink: false, isDirectory: false })
];
const fakeGetVersionsResult = VersionUtils.sortVersions([
"13.2.0.47",
"13.4.0.2",
"13.6.0.12",
"13.8.2.9",
"13.8.3.0",
"13.8.3.2",
"13.9.1.0",
"13.10.0.21",
"14.0.2.1"
]);
describe.each([
MonoToolSelector,
XamarinIosToolSelector,
XamarinMacToolSelector,
XamarinAndroidToolSelector
])("%s", (selectorClass: { new (): ToolSelector }) => {
describe("getAllVersions", () => {
beforeEach(() => {
jest.spyOn(fs, "readdirSync").mockImplementation(() => fakeReadDirResults);
jest.spyOn(fs, "readFileSync").mockImplementation(filepath => path.basename(path.dirname(filepath.toString())));
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
it("versions are filtered correctly", () => {
const sel = new selectorClass();
const expectedVersions = [
"13.10.0.21",
"13.6.0.12",
"13.4.0.2"
];
expect(sel.getAllVersions()).toEqual(expectedVersions);
});
});
describe("findVersion", () => {
it.each([
["latest", "14.0.2.1", "latest is matched"],
["14", "14.0.2.1", "one digit is matched"],
["13", "13.10.0.21", "one digit is matched and latest version is selected"],
["11", null, "one digit is not matched"],
["14.0", "14.0.2.1", "two digits are matched"],
["13.8", "13.8.3.2", "two digits are matched and latest version is selected"],
["13.7", null, "two digits are not matched"],
["11.0", null, "two digits are not matched"],
["13.2.0", "13.2.0.47", "three digits are matched"],
["13.8.3", "13.8.3.2", "three digits are matched and latest version is selected"],
["11.0.2", null, "three digits are not matched"],
["13.5.4", null, "three digits are not matched"],
["13.8.1", null, "three digits are not matched"],
["13.9.1.0", "13.9.1.0", "four digits are matched"],
["13.10.0.22", null, "four digits are not matched"]
] as [string, string | null, string][])("'%s' -> '%s' (%s)", (versionSpec: string, expected: string | null) => {
const sel = new selectorClass();
sel.getAllVersions = (): string[] => fakeGetVersionsResult;
const matchedVersion = sel.findVersion(versionSpec);
expect(matchedVersion).toBe(expected);
});
});
describe("setVersion", () => {
let fsExistsSpy: jest.SpyInstance;
let fsInvokeCommandSpy: jest.SpyInstance;
beforeEach(() => {
fsExistsSpy = jest.spyOn(fs, "existsSync");
fsInvokeCommandSpy = jest.spyOn(utils, "invokeCommandSync");
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
it("symlink is created", () => {
fsExistsSpy.mockImplementation((path: string) => {
return !path.endsWith("/Current");
});
const sel = new selectorClass();
sel.setVersion("1.2.3.4");
expect(fsInvokeCommandSpy).toHaveBeenCalledTimes(1);
expect(fsInvokeCommandSpy).toHaveBeenCalledWith("ln", expect.any(Array), expect.any(Object));
});
it("symlink is recreated", () => {
fsExistsSpy.mockImplementation(() => true);
const sel = new selectorClass();
sel.setVersion("1.2.3.4");
expect(fsInvokeCommandSpy).toHaveBeenCalledTimes(2);
expect(fsInvokeCommandSpy).toHaveBeenCalledWith("rm", expect.any(Array), expect.any(Object));
expect(fsInvokeCommandSpy).toHaveBeenCalledWith("ln", expect.any(Array), expect.any(Object));
});
it("error is thrown if version doesn't exist", () => {
fsExistsSpy.mockImplementation(() => false);
const sel = new selectorClass();
expect(() => sel.setVersion("1.2.3.4")).toThrow();
expect(fsInvokeCommandSpy).toHaveBeenCalledTimes(0);
});
});
});
describe("MonoToolSelector", () => {
describe("setVersion", () => {
let coreExportVariableSpy: jest.SpyInstance;
let coreAddPathSpy: jest.SpyInstance;
let fsExistsSpy: jest.SpyInstance;
beforeEach(() => {
coreExportVariableSpy = jest.spyOn(core, "exportVariable");
coreAddPathSpy = jest.spyOn(core, "addPath");
fsExistsSpy = jest.spyOn(fs, "existsSync");
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
it("environment variables are set correctly", () => {
fsExistsSpy.mockImplementation(() => true);
const sel = new MonoToolSelector();
sel.setVersion("1.2.3.4");
expect(coreExportVariableSpy).toHaveBeenCalledWith("DYLD_LIBRARY_FALLBACK_PATH", expect.any(String));
expect(coreExportVariableSpy).toHaveBeenCalledWith("PKG_CONFIG_PATH", expect.any(String));
expect(coreAddPathSpy).toHaveBeenCalledWith("/Library/Frameworks/Mono.framework/Versions/1.2.3/bin");
});
});
});