Skip to content

Commit d97c0d5

Browse files
Add more unit tests
1 parent a4329b5 commit d97c0d5

File tree

1 file changed

+274
-14
lines changed

1 file changed

+274
-14
lines changed

test/sys-info.ts

Lines changed: 274 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,136 @@ import { SysInfo } from "../lib/sys-info";
44
import { ChildProcess } from "../lib/wrappers/child-process";
55

66
const JavaHomeName = "JAVA_HOME";
7+
const AndroidHomeName = "ANDROID_HOME";
8+
9+
interface IChildProcessResultDescription {
10+
result?: any;
11+
shouldThrowError?: boolean;
12+
}
13+
14+
interface IChildProcessResults {
15+
uname: IChildProcessResultDescription;
16+
npmV: IChildProcessResultDescription;
17+
javaVersion: IChildProcessResultDescription;
18+
javacVersion: IChildProcessResultDescription;
19+
nodeGypVersion: IChildProcessResultDescription;
20+
xCodeVersion: IChildProcessResultDescription;
21+
adbVersion: IChildProcessResultDescription;
22+
androidInstalled: IChildProcessResultDescription;
23+
monoVersion: IChildProcessResultDescription;
24+
gradleVersion: IChildProcessResultDescription;
25+
gitVersion: IChildProcessResultDescription;
26+
podVersion: IChildProcessResultDescription;
27+
pod: IChildProcessResultDescription;
28+
}
29+
30+
interface IHostInfoMockOptions {
31+
isWindows?: boolean;
32+
dotNetVersion?: string;
33+
isDarwin?: boolean;
34+
}
35+
36+
interface IFileSystemMockOptions {
37+
existsResult?: boolean;
38+
}
39+
40+
function createChildProcessResults(childProcessResult: IChildProcessResults): IDictionary<IChildProcessResultDescription> {
41+
return {
42+
"uname -a": childProcessResult.uname,
43+
"npm -v": childProcessResult.npmV,
44+
"java": childProcessResult.javaVersion,
45+
'"javac" -version': childProcessResult.javacVersion,
46+
"node-gyp -v": childProcessResult.nodeGypVersion,
47+
"xcodebuild -version": childProcessResult.xCodeVersion,
48+
"pod --version": childProcessResult.podVersion,
49+
"pod": childProcessResult.pod,
50+
'adb version': childProcessResult.adbVersion,
51+
"'adb' version": childProcessResult.adbVersion, // for Mac and Linux
52+
'android': childProcessResult.androidInstalled,
53+
'android.bat': childProcessResult.androidInstalled, // for Windows
54+
"mono --version": childProcessResult.monoVersion,
55+
"git --version": childProcessResult.gitVersion,
56+
"gradle -v": childProcessResult.gradleVersion
57+
};
58+
}
59+
60+
function getResultFromChildProcess(childProcessResultDescription: IChildProcessResultDescription, command: string): any {
61+
if (childProcessResultDescription.shouldThrowError) {
62+
throw new Error(`This one throws error. (${command})`);
63+
}
64+
65+
return childProcessResultDescription.result;
66+
}
67+
68+
function mockSysInfo(childProcessResult: IChildProcessResults, hostInfoOptions?: IHostInfoMockOptions, fileSystemOptions?: IFileSystemMockOptions): SysInfo {
69+
hostInfoOptions = hostInfoOptions || {};
70+
const winreg: any = {
71+
getRegistryValue: (valueName: string, hive?: IHiveId, key?: string, host?: string) => { return { value: "registryKey" }; },
72+
registryKeys: {
73+
HKLM: "HKLM"
74+
}
75+
};
76+
const hostInfo: any = {
77+
dotNetVersion: () => Promise.resolve(hostInfoOptions.dotNetVersion),
78+
isDarwin: hostInfoOptions.isDarwin,
79+
isWindows: hostInfoOptions.isWindows,
80+
isLinux: (!hostInfoOptions.isDarwin && !hostInfoOptions.isWindows),
81+
winreg
82+
};
83+
const childProcessResultDictionary = createChildProcessResults(childProcessResult);
84+
const childProcess = {
85+
exec: async (command: string) => {
86+
return getResultFromChildProcess(childProcessResultDictionary[command], command);
87+
},
88+
89+
spawnFromEvent: async (command: string, args: string[], event: string) => {
90+
return getResultFromChildProcess(childProcessResultDictionary[command], command);
91+
}
92+
};
93+
94+
const fileSystem: any = {
95+
exists: () => Promise.resolve((fileSystemOptions || {}).existsResult),
96+
extractZip: () => Promise.resolve()
97+
};
98+
99+
return new SysInfo(childProcess, hostInfo, fileSystem, winreg, null);
100+
}
101+
102+
function setStdOut(value: string): { stdout: string } {
103+
return { stdout: value };
104+
}
105+
function setStdErr(value: string): { stderr: string } {
106+
return { stderr: value };
107+
}
7108

8109
describe("SysInfo unit tests", () => {
9110
let sysInfo: SysInfo;
10-
let spawnFromEventCommand: string;
11-
let execCommand: string;
12111

13112
beforeEach(() => {
14-
const childProcess: ChildProcess = {
15-
spawnFromEvent: async (command: string, args: string[], event: string) => {
16-
spawnFromEventCommand = `${command} ${args.join(" ")}`;
17-
return { stdout: "", stderr: "" };
18-
},
19-
exec: async (command: string) => {
20-
execCommand = command;
21-
return { stdout: "", stderr: "" };
22-
}
23-
};
24-
25-
sysInfo = new SysInfo(childProcess, null, null, null, null);
113+
// We need to mock this because on Mac the tests in which the platform is mocked to Windows in the process there will be no CommonProgramFiles.
114+
process.env.CommonProgramFiles = process.env.CommonProgramFiles || "mocked on mac";
115+
process.env["CommonProgramFiles(x86)"] = process.env["CommonProgramFiles(x86)"] || "mocked on mac";
26116
});
27117

28118
describe("Should execute correct commands to check for", () => {
119+
let spawnFromEventCommand: string;
120+
let execCommand: string;
121+
122+
beforeEach(() => {
123+
const childProcess: ChildProcess = {
124+
spawnFromEvent: async (command: string, args: string[], event: string) => {
125+
spawnFromEventCommand = `${command} ${args.join(" ")}`;
126+
return { stdout: "", stderr: "" };
127+
},
128+
exec: async (command: string) => {
129+
execCommand = command;
130+
return { stdout: "", stderr: "" };
131+
}
132+
};
133+
134+
sysInfo = new SysInfo(childProcess, null, null, null, null);
135+
});
136+
29137
it("java version.", async () => {
30138
await sysInfo.getJavaVersion();
31139
assert.deepEqual(spawnFromEventCommand, "java -version");
@@ -53,4 +161,156 @@ describe("SysInfo unit tests", () => {
53161
assert.deepEqual(execCommand, `"javac" -version`);
54162
});
55163
});
164+
165+
describe("getSysInfo", () => {
166+
let childProcessResult: IChildProcessResults;
167+
const originalJavaHome = process.env[JavaHomeName];
168+
const originalAndroidHome = process.env[AndroidHomeName];
169+
170+
beforeEach(() => {
171+
childProcessResult = {
172+
uname: { result: setStdOut("name") },
173+
npmV: { result: setStdOut("2.14.1") },
174+
javaVersion: { result: setStdErr('java version "1.8.0_60"') },
175+
javacVersion: { result: setStdErr("javac 1.8.0_60") },
176+
nodeGypVersion: { result: setStdOut("2.0.0") },
177+
xCodeVersion: { result: setStdOut("Xcode 6.4.0") },
178+
adbVersion: { result: setStdOut("Android Debug Bridge version 1.0.32") },
179+
androidInstalled: { result: setStdOut("android") },
180+
monoVersion: { result: setStdOut("version 1.0.6 ") },
181+
gradleVersion: { result: setStdOut("Gradle 2.8") },
182+
gitVersion: { result: setStdOut("git version 1.9.5") },
183+
podVersion: { result: setStdOut("0.38.2") },
184+
pod: { result: setStdOut("success") }
185+
};
186+
187+
delete process.env[JavaHomeName];
188+
delete process.env[AndroidHomeName];
189+
});
190+
191+
afterEach(() => {
192+
process.env[JavaHomeName] = originalJavaHome;
193+
process.env[AndroidHomeName] = originalAndroidHome;
194+
});
195+
196+
describe("returns correct results when everything is installed", () => {
197+
let assertCommonValues = (result: ISysInfoData) => {
198+
assert.deepEqual(result.npmVer, childProcessResult.npmV.result.stdout);
199+
assert.deepEqual(result.javaVer, "1.8.0");
200+
assert.deepEqual(result.javacVersion, "1.8.0_60");
201+
assert.deepEqual(result.nodeGypVer, childProcessResult.nodeGypVersion.result.stdout);
202+
assert.deepEqual(result.adbVer, "1.0.32");
203+
assert.deepEqual(result.androidInstalled, true);
204+
assert.deepEqual(result.monoVer, "1.0.6");
205+
assert.deepEqual(result.gradleVer, "2.8");
206+
assert.deepEqual(result.gitVer, "1.9.5");
207+
};
208+
209+
it("on Windows", async () => {
210+
sysInfo = mockSysInfo(childProcessResult, { isWindows: true, isDarwin: false, dotNetVersion: "4.5.1" });
211+
let result = await sysInfo.getSysInfo();
212+
assertCommonValues(result);
213+
assert.deepEqual(result.xcodeVer, null);
214+
assert.deepEqual(result.cocoapodVer, null);
215+
});
216+
217+
it("on Mac", async () => {
218+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
219+
let result = await sysInfo.getSysInfo();
220+
assertCommonValues(result);
221+
assert.deepEqual(result.xcodeVer, "6.4.0");
222+
assert.deepEqual(result.cocoapodVer, childProcessResult.podVersion.result.stdout);
223+
});
224+
225+
it("on Linux", async () => {
226+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: false, dotNetVersion: "4.5.1" });
227+
let result = await sysInfo.getSysInfo();
228+
assertCommonValues(result);
229+
assert.deepEqual(result.xcodeVer, null);
230+
assert.deepEqual(result.cocoapodVer, null);
231+
});
232+
});
233+
234+
describe("cocoapods version", () => {
235+
it("is null when cocoapods are not installed", async () => {
236+
// simulate error when pod --version command is executed
237+
childProcessResult.podVersion = { shouldThrowError: true };
238+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
239+
let result = await sysInfo.getSysInfo();
240+
assert.deepEqual(result.cocoapodVer, null);
241+
});
242+
243+
it("is null when OS is not Mac", async () => {
244+
sysInfo = mockSysInfo(childProcessResult, { isWindows: true, isDarwin: false, dotNetVersion: "4.5.1" });
245+
let result = await sysInfo.getSysInfo();
246+
assert.deepEqual(result.cocoapodVer, null);
247+
});
248+
249+
it("is correct when cocoapods output has warning after version output", async () => {
250+
childProcessResult.podVersion = { result: setStdOut("0.38.2\nWARNING:\n") };
251+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
252+
let result = await sysInfo.getSysInfo();
253+
assert.deepEqual(result.cocoapodVer, "0.38.2");
254+
});
255+
256+
it("is correct when cocoapods output has warnings before version output", async () => {
257+
childProcessResult.podVersion = { result: setStdOut("WARNING\nWARNING2\n0.38.2") };
258+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
259+
let result = await sysInfo.getSysInfo();
260+
assert.deepEqual(result.cocoapodVer, "0.38.2");
261+
});
262+
});
263+
264+
describe("returns correct results when exceptions are raised during sysInfo data collection", () => {
265+
beforeEach(() => {
266+
childProcessResult = {
267+
uname: { shouldThrowError: true },
268+
npmV: { shouldThrowError: true },
269+
javaVersion: { shouldThrowError: true },
270+
javacVersion: { shouldThrowError: true },
271+
nodeGypVersion: { shouldThrowError: true },
272+
xCodeVersion: { shouldThrowError: true },
273+
adbVersion: { shouldThrowError: true },
274+
androidInstalled: { shouldThrowError: true },
275+
monoVersion: { shouldThrowError: true },
276+
gradleVersion: { shouldThrowError: true },
277+
gitVersion: { shouldThrowError: true },
278+
podVersion: { shouldThrowError: true },
279+
pod: { shouldThrowError: true }
280+
};
281+
});
282+
283+
describe("when all of calls throw", () => {
284+
let assertAllValuesAreNull = async () => {
285+
let result = await sysInfo.getSysInfo();
286+
assert.deepEqual(result.npmVer, null);
287+
assert.deepEqual(result.javaVer, null);
288+
assert.deepEqual(result.javacVersion, null);
289+
assert.deepEqual(result.nodeGypVer, null);
290+
assert.deepEqual(result.xcodeVer, null);
291+
assert.deepEqual(result.adbVer, null);
292+
assert.deepEqual(result.androidInstalled, false);
293+
assert.deepEqual(result.monoVer, null);
294+
assert.deepEqual(result.gradleVer, null);
295+
assert.deepEqual(result.gitVer, null);
296+
assert.deepEqual(result.cocoapodVer, null);
297+
};
298+
299+
it("on Windows", () => {
300+
sysInfo = mockSysInfo(childProcessResult, { isWindows: true, isDarwin: false, dotNetVersion: "4.5.1" });
301+
assertAllValuesAreNull();
302+
});
303+
304+
it("on Mac", () => {
305+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
306+
assertAllValuesAreNull();
307+
});
308+
309+
it("on Linux", () => {
310+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: false, dotNetVersion: "4.5.1" });
311+
assertAllValuesAreNull();
312+
});
313+
});
314+
});
315+
});
56316
});

0 commit comments

Comments
 (0)