-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathhost-info.ts
117 lines (97 loc) · 4.25 KB
/
host-info.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
import { Yok } from "../../yok";
import { HostInfo } from "../../host-info";
import { ErrorsStub, CommonLoggerStub } from "./stubs";
import { assert } from "chai";
// Use custom class as isDarwin has only getter in HostInfo, while for current tests we need to set it to true or false.
class MockHostInfo extends HostInfo {
private _isDarwin: boolean;
public get isDarwin(): boolean {
return this._isDarwin;
}
public set isDarwin(value: boolean) {
this._isDarwin = value;
}
constructor($errors: IErrors, $injector: IInjector) {
super($errors, $injector);
}
}
describe("hostInfo", () => {
describe("getMacOSVersion", () => {
const createTestInjector = (): IInjector => {
const testInjector = new Yok();
testInjector.register("injector", testInjector);
testInjector.register("errors", ErrorsStub);
testInjector.register("osInfo", {});
testInjector.register("logger", CommonLoggerStub);
testInjector.register("childProcess", {});
testInjector.register("hostInfo", MockHostInfo);
testInjector.resolve("hostInfo").isDarwin = true;
return testInjector;
};
it("returns null when os is not macOS", async () => {
const testInjector = createTestInjector();
const hostInfo = testInjector.resolve<IHostInfo>("hostInfo");
hostInfo.isDarwin = false;
const macOSVersion = await hostInfo.getMacOSVersion();
assert.deepEqual(macOSVersion, null);
});
it("returns correct macOS version based on system_profile", async () => {
const testInjector = createTestInjector();
const hostInfo = testInjector.resolve<IHostInfo>("hostInfo");
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
let calledCommand = "";
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
calledCommand = command;
return `Software:
System Software Overview:
System Version: macOS 10.13.3 (17D47)
Kernel Version: Darwin 17.4.0
Time since boot: 68 days 22:12`;
};
const macOSVersion = await hostInfo.getMacOSVersion();
assert.deepEqual(macOSVersion, "10.13");
assert.equal(calledCommand, "system_profiler SPSoftwareDataType -detailLevel mini");
});
it("returns correct macOS version based on system_profile, when version has two numbers only", async () => {
const testInjector = createTestInjector();
const hostInfo = testInjector.resolve<IHostInfo>("hostInfo");
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
let calledCommand = "";
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
calledCommand = command;
return `Software:
System Software Overview:
System Version: macOS 10.14 (18A391)
Kernel Version: Darwin 18.0.0
Time since boot: 1 day 5:52`;
};
const macOSVersion = await hostInfo.getMacOSVersion();
assert.deepEqual(macOSVersion, "10.14");
assert.equal(calledCommand, "system_profiler SPSoftwareDataType -detailLevel mini");
});
it("returns correct macOS version when system_profile call throws", async () => {
const testInjector = createTestInjector();
const hostInfo = testInjector.resolve<IHostInfo>("hostInfo");
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
throw new Error("Err");
};
const osInfo = testInjector.resolve<IOsInfo>("osInfo");
osInfo.release = (): string => "17.4.0";
const macOSVersion = await hostInfo.getMacOSVersion();
assert.deepEqual(macOSVersion, "10.13");
});
it("returns correct macOS version when system_profile call returns data that does not match our RegExp", async () => {
const testInjector = createTestInjector();
const hostInfo = testInjector.resolve<IHostInfo>("hostInfo");
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
return "Non-matching data";
};
const osInfo = testInjector.resolve<IOsInfo>("osInfo");
osInfo.release = (): string => "17.4.0";
const macOSVersion = await hostInfo.getMacOSVersion();
assert.deepEqual(macOSVersion, "10.13");
});
});
});