|
| 1 | +import { SysInfo } from "../lib/sys-info"; |
| 2 | +import { Yok } from "../lib/common/yok"; |
| 3 | +import { assert } from "chai"; |
| 4 | +import { format } from "util"; |
| 5 | +import * as sinon from "sinon"; |
| 6 | +import { MacOSVersions, MacOSDeprecationStringFormat } from "../lib/constants"; |
| 7 | +const verifyNodeVersion = require("../lib/common/verify-node-version"); |
| 8 | + |
| 9 | +describe("sysInfo", () => { |
| 10 | + let sandbox: sinon.SinonSandbox = null; |
| 11 | + beforeEach(() => { |
| 12 | + sandbox = sinon.sandbox.create(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + sandbox.restore(); |
| 17 | + }); |
| 18 | + |
| 19 | + const createTestInjector = (): IInjector => { |
| 20 | + const testInjector = new Yok(); |
| 21 | + testInjector.register("hostInfo", { |
| 22 | + getMacOSVersion: async (): Promise<string> => null |
| 23 | + }); |
| 24 | + |
| 25 | + testInjector.register("fs", { |
| 26 | + readJson: (filename: string, encoding?: string): any => null |
| 27 | + }); |
| 28 | + |
| 29 | + testInjector.register("sysInfo", SysInfo); |
| 30 | + |
| 31 | + return testInjector; |
| 32 | + }; |
| 33 | + |
| 34 | + describe("getSystemWarnings", () => { |
| 35 | + const getSystemWarnings = async (opts?: { nodeJsWarning?: string, macOSDeprecatedVersion?: string }): Promise<string[]> => { |
| 36 | + sandbox.stub(verifyNodeVersion, "getNodeWarning").returns(opts && opts.nodeJsWarning); |
| 37 | + |
| 38 | + const testInjector = createTestInjector(); |
| 39 | + const $hostInfo = testInjector.resolve<IHostInfo>("hostInfo"); |
| 40 | + $hostInfo.getMacOSVersion = async (): Promise<string> => opts && opts.macOSDeprecatedVersion; |
| 41 | + const sysInfo = testInjector.resolve<ISysInfo>("sysInfo"); |
| 42 | + const warnings = await sysInfo.getSystemWarnings(); |
| 43 | + return warnings; |
| 44 | + }; |
| 45 | + |
| 46 | + it("returns empty array when there are no warnings", async () => { |
| 47 | + const warnings = await getSystemWarnings(); |
| 48 | + assert.deepEqual(warnings, []); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns correct single warning when macOS version is deprecated", async () => { |
| 52 | + const macOSDeprecatedVersion = MacOSVersions.Sierra; |
| 53 | + const macOSWarning = format(MacOSDeprecationStringFormat, macOSDeprecatedVersion); |
| 54 | + const warnings = await getSystemWarnings({ macOSDeprecatedVersion }); |
| 55 | + assert.deepEqual(warnings, [macOSWarning]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns correct single warning when Node.js version is deprecated", async () => { |
| 59 | + const nodeJsWarning = "Node.js Warning"; |
| 60 | + const warnings = await getSystemWarnings({ nodeJsWarning }); |
| 61 | + assert.deepEqual(warnings, [nodeJsWarning]); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns correct warnings when both Node.js and macOS versions are deprecated", async () => { |
| 65 | + const macOSDeprecatedVersion = MacOSVersions.Sierra; |
| 66 | + const macOSWarning = format(MacOSDeprecationStringFormat, macOSDeprecatedVersion); |
| 67 | + const nodeJsWarning = "Node.js Warning"; |
| 68 | + const warnings = await getSystemWarnings({ macOSDeprecatedVersion, nodeJsWarning }); |
| 69 | + assert.deepEqual(warnings, [macOSWarning, nodeJsWarning]); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("getMacOSWarningMessage", () => { |
| 74 | + const getMacOSWarning = async (macOSDeprecatedVersion?: string): Promise<string> => { |
| 75 | + sandbox.stub(verifyNodeVersion, "getNodeWarning").returns(null); |
| 76 | + |
| 77 | + const testInjector = createTestInjector(); |
| 78 | + const $hostInfo = testInjector.resolve<IHostInfo>("hostInfo"); |
| 79 | + $hostInfo.getMacOSVersion = async (): Promise<string> => macOSDeprecatedVersion; |
| 80 | + const sysInfo = testInjector.resolve<ISysInfo>("sysInfo"); |
| 81 | + const warning = await sysInfo.getMacOSWarningMessage(); |
| 82 | + return warning; |
| 83 | + }; |
| 84 | + |
| 85 | + it("returns null when macOS version is supported", async () => { |
| 86 | + const warning = await getMacOSWarning(); |
| 87 | + assert.deepEqual(warning, null); |
| 88 | + }); |
| 89 | + |
| 90 | + it("returns correct single warning when macOS version is deprecated", async () => { |
| 91 | + const macOSDeprecatedVersion = MacOSVersions.Sierra; |
| 92 | + const macOSWarning = format(MacOSDeprecationStringFormat, macOSDeprecatedVersion); |
| 93 | + const warning = await getMacOSWarning(macOSDeprecatedVersion); |
| 94 | + assert.deepEqual(warning, macOSWarning); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("getSupportedNodeVersionRange", () => { |
| 99 | + it("returns range from CLI's package.json", () => { |
| 100 | + const testInjector = createTestInjector(); |
| 101 | + const expectedRange = require("../package.json").engines.node; |
| 102 | + const fs = testInjector.resolve<IFileSystem>("fs"); |
| 103 | + fs.readJson = () => require("../package.json"); |
| 104 | + const sysInfo = testInjector.resolve<ISysInfo>("sysInfo"); |
| 105 | + const actualRange = sysInfo.getSupportedNodeVersionRange(); |
| 106 | + assert.equal(actualRange, expectedRange); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments