Skip to content

Commit 5fccd04

Browse files
Fix getting CLI version
In case CLI prints other things (like warnings from loading extensions) to its stdout, the `nativescript-doctor` returns incorrect version for CLI. Fix this by getting only the version from stdout. Add unit tests for this scenario.
1 parent 9b33780 commit 5fccd04

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/sys-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
298298
public getNativeScriptCliVersion(): Promise<string> {
299299
return this.getValueForProperty(() => this.nativeScriptCliVersionCache, async (): Promise<string> => {
300300
const output = await this.execCommand("tns --version");
301-
return output ? output.trim() : output;
301+
return output ? this.getVersionFromString(output.trim()) : output;
302302
});
303303
}
304304

test/sys-info.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function mockSysInfo(childProcessResult: IChildProcessResults, hostInfoOptions?:
126126
spawnFromEvent: async (command: string, args: string[], event: string, options: ISpawnFromEventOptions) => {
127127
return getResultFromChildProcess(childProcessResultDictionary[command], command, options);
128128
},
129-
execFile: async () => {
129+
execFile: async (): Promise<any> => {
130130
return undefined;
131131
}
132132
};
@@ -322,6 +322,29 @@ describe("SysInfo unit tests", () => {
322322
});
323323
});
324324

325+
describe("nativeScriptCliVersion", () => {
326+
it("is null when tns is not installed", async () => {
327+
childProcessResult.nativeScriptCliVersion = { shouldThrowError: true };
328+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
329+
let result = await sysInfo.getSysInfo();
330+
assert.deepEqual(result.nativeScriptCliVersion, null);
331+
});
332+
333+
it("is correct when the version is the only row in `tns --version` output", async () => {
334+
childProcessResult.nativeScriptCliVersion = { result: setStdOut("3.0.0") };
335+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
336+
let result = await sysInfo.getSysInfo();
337+
assert.deepEqual(result.nativeScriptCliVersion, "3.0.0");
338+
});
339+
340+
it("is correct when there are warnings in the `tns --version` output", async () => {
341+
childProcessResult.nativeScriptCliVersion = { result: setStdOut("Some warning due to invalid extensions\\n3.0.0") };
342+
sysInfo = mockSysInfo(childProcessResult, { isWindows: false, isDarwin: true, dotNetVersion: "4.5.1" });
343+
let result = await sysInfo.getSysInfo();
344+
assert.deepEqual(result.nativeScriptCliVersion, "3.0.0");
345+
});
346+
});
347+
325348
describe("returns correct results when exceptions are raised during sysInfo data collection", () => {
326349
beforeEach(() => {
327350
childProcessResult = {

0 commit comments

Comments
 (0)