diff --git a/tests/lib/shared/runtime-info.js b/tests/lib/shared/runtime-info.js index 79ed4ff1ae28..520bb32e8452 100644 --- a/tests/lib/shared/runtime-info.js +++ b/tests/lib/shared/runtime-info.js @@ -21,8 +21,13 @@ const packageJson = require("../../../package.json"); // Tests //------------------------------------------------------------------------------ -const LOCAL_ESLINT_BIN_PATH = "/Users/username/code/project/node_modules/eslint/bin/eslint.js"; -const GLOBAL_ESLINT_BIN_PATH = "/usr/local/bin/npm/node_modules/eslint/bin/eslint.js"; +const NIX_NPM_BIN_PATH = "/usr/local/bin/npm"; +const NIX_LOCAL_ESLINT_BIN_PATH = "/Users/username/project/node_modules/eslint/bin/eslint.js"; +const NIX_GLOBAL_ESLINT_BIN_PATH = "/usr/local/bin/npm/node_modules/eslint/bin/eslint.js"; + +const WINDOWS_NPM_BIN_PATH = "C:\\Users\\username\\AppData\\Roaming\\npm"; +const WINDOWS_LOCAL_ESLINT_BIN_PATH = " C:\\Users\\username\\project\\node_modules\\eslint\\bin\\eslint.js"; +const WINDOWS_GLOBAL_ESLINT_BIN_PATH = "C:\\Users\\username\\AppData\\Roaming\\npm\\node_modules\\eslint\\bin\\eslint.js"; /** * Sets up spawn.sync() stub calls to return values and throw errors in the order in which they are given. @@ -45,167 +50,174 @@ function setupSpawnSyncStubReturnVals(stub, returnVals) { } describe("RuntimeInfo", () => { - describe("environment()", () => { - let spawnSyncStub; - let logErrorStub; - let originalProcessArgv; - let spawnSyncStubArgs; - - beforeEach(() => { - spawnSyncStub = sinon.stub(spawn, "sync"); - logErrorStub = sinon.stub(log, "error"); - originalProcessArgv = process.argv; - process.argv[1] = LOCAL_ESLINT_BIN_PATH; - spawnSyncStubArgs = [ - "v12.8.0", - "6.11.3", - unIndent` - { - "name": "project", - "version": "1.0.0", - "dependencies": { - "eslint": { - "version": "6.3.0" + [ + ["*nix", NIX_NPM_BIN_PATH, NIX_LOCAL_ESLINT_BIN_PATH, NIX_GLOBAL_ESLINT_BIN_PATH], + ["Windows", WINDOWS_NPM_BIN_PATH, WINDOWS_LOCAL_ESLINT_BIN_PATH, WINDOWS_GLOBAL_ESLINT_BIN_PATH] + ].forEach(([osName, binPath, localEslintBinPath, globalEslintBinPath]) => { + describe(`when run in a ${osName} environment`, () => { + describe("environment()", () => { + let spawnSyncStub; + let logErrorStub; + let originalProcessArgv; + let spawnSyncStubArgs; + + beforeEach(() => { + spawnSyncStub = sinon.stub(spawn, "sync"); + logErrorStub = sinon.stub(log, "error"); + originalProcessArgv = process.argv; + process.argv[1] = localEslintBinPath; + spawnSyncStubArgs = [ + "v12.8.0", + "6.11.3", + unIndent` + { + "name": "project", + "version": "1.0.0", + "dependencies": { + "eslint": { + "version": "6.3.0" + } + } } - } - } - `, - "/usr/local/bin/npm", - unIndent` - { - "dependencies": { - "eslint": { - "version": "5.16.0", - "from": "eslint", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz" + `, + binPath, + unIndent` + { + "dependencies": { + "eslint": { + "version": "5.16.0", + "from": "eslint", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz" + } + } } + `, + binPath + ]; + }); + + afterEach(() => { + spawnSyncStub.restore(); + logErrorStub.restore(); + process.argv = originalProcessArgv; + }); + + + it("should return a string containing environment information when running local installation", () => { + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.strictEqual( + RuntimeInfo.environment(), + unIndent` + Environment Info: + + Node version: v12.8.0 + npm version: v6.11.3 + Local ESLint version: v6.3.0 (Currently used) + Global ESLint version: v5.16.0 + ` + ); + }); + + it("should return a string containing environment information when running global installation", () => { + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + process.argv[1] = globalEslintBinPath; + + assert.strictEqual( + RuntimeInfo.environment(), + unIndent` + Environment Info: + + Node version: v12.8.0 + npm version: v6.11.3 + Local ESLint version: v6.3.0 + Global ESLint version: v5.16.0 (Currently used) + ` + ); + }); + + it("should return a string containing environment information when not installed locally", () => { + spawnSyncStubArgs.splice(2, 2, unIndent` + { + "name": "project", + "version": "1.0.0" } - } - `, - "/usr/local/bin/npm" - ]; - }); - - afterEach(() => { - spawnSyncStub.restore(); - logErrorStub.restore(); - process.argv = originalProcessArgv; - }); - - - it("should return a string containing environment information when running local installation", () => { - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.strictEqual( - RuntimeInfo.environment(), - unIndent` - Environment Info: - - Node version: v12.8.0 - npm version: v6.11.3 - Local ESLint version: v6.3.0 (Currently used) - Global ESLint version: v5.16.0 - ` - ); - }); - - it("should return a string containing environment information when running global installation", () => { - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - process.argv[1] = GLOBAL_ESLINT_BIN_PATH; - - assert.strictEqual( - RuntimeInfo.environment(), - unIndent` - Environment Info: - - Node version: v12.8.0 - npm version: v6.11.3 - Local ESLint version: v6.3.0 - Global ESLint version: v5.16.0 (Currently used) - ` - ); - }); - - it("should return a string containing environment information when not installed locally", () => { - spawnSyncStubArgs.splice(2, 2, unIndent` - { - "name": "project", - "version": "1.0.0" - } - `); - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - process.argv[1] = GLOBAL_ESLINT_BIN_PATH; - - assert.strictEqual( - RuntimeInfo.environment(), - unIndent` - Environment Info: - - Node version: v12.8.0 - npm version: v6.11.3 - Local ESLint version: Not found - Global ESLint version: v5.16.0 (Currently used) - ` - ); - }); - - it("should return a string containing environment information when not installed globally", () => { - spawnSyncStubArgs[4] = "{}"; - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.strictEqual( - RuntimeInfo.environment(), - unIndent` - Environment Info: - - Node version: v12.8.0 - npm version: v6.11.3 - Local ESLint version: v6.3.0 (Currently used) - Global ESLint version: Not found - ` - ); - }); - - it("log and throw an error when npm version can not be found", () => { - const expectedErr = new Error("npm can not be found"); - - spawnSyncStubArgs[1] = expectedErr; - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.throws(RuntimeInfo.environment, expectedErr); - assert.strictEqual(logErrorStub.args[0][0], "Error finding npm version running the command `npm --version`"); - }); - - it("log and throw an error when npm binary path can not be found", () => { - const expectedErr = new Error("npm can not be found"); - - spawnSyncStubArgs[3] = expectedErr; - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.throws(RuntimeInfo.environment, expectedErr); - assert.strictEqual(logErrorStub.args[0][0], "Error finding npm binary path when running command `npm bin -g`"); - }); - - it("log and throw an error when checking for local ESLint version when returned output of command is malformed", () => { - spawnSyncStubArgs[2] = "This is not JSON"; - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.throws(RuntimeInfo.environment, "Unexpected token T in JSON at position 0"); - assert.strictEqual(logErrorStub.args[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint`"); - }); - - it("log and throw an error when checking for global ESLint version when returned output of command is malformed", () => { - spawnSyncStubArgs[4] = "This is not JSON"; - setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); - - assert.throws(RuntimeInfo.environment, "Unexpected token T in JSON at position 0"); - assert.strictEqual(logErrorStub.args[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint -g`"); - }); - }); - - describe("version()", () => { - it("should return the version of the package defined in package.json", () => { - assert.strictEqual(RuntimeInfo.version(), `v${packageJson.version}`); + `); + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + process.argv[1] = globalEslintBinPath; + + assert.strictEqual( + RuntimeInfo.environment(), + unIndent` + Environment Info: + + Node version: v12.8.0 + npm version: v6.11.3 + Local ESLint version: Not found + Global ESLint version: v5.16.0 (Currently used) + ` + ); + }); + + it("should return a string containing environment information when not installed globally", () => { + spawnSyncStubArgs[4] = "{}"; + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.strictEqual( + RuntimeInfo.environment(), + unIndent` + Environment Info: + + Node version: v12.8.0 + npm version: v6.11.3 + Local ESLint version: v6.3.0 (Currently used) + Global ESLint version: Not found + ` + ); + }); + + it("log and throw an error when npm version can not be found", () => { + const expectedErr = new Error("npm can not be found"); + + spawnSyncStubArgs[1] = expectedErr; + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.throws(RuntimeInfo.environment, expectedErr); + assert.strictEqual(logErrorStub.args[0][0], "Error finding npm version running the command `npm --version`"); + }); + + it("log and throw an error when npm binary path can not be found", () => { + const expectedErr = new Error("npm can not be found"); + + spawnSyncStubArgs[3] = expectedErr; + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.throws(RuntimeInfo.environment, expectedErr); + assert.strictEqual(logErrorStub.args[0][0], "Error finding npm binary path when running command `npm bin -g`"); + }); + + it("log and throw an error when checking for local ESLint version when returned output of command is malformed", () => { + spawnSyncStubArgs[2] = "This is not JSON"; + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.throws(RuntimeInfo.environment, "Unexpected token T in JSON at position 0"); + assert.strictEqual(logErrorStub.args[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint`"); + }); + + it("log and throw an error when checking for global ESLint version when returned output of command is malformed", () => { + spawnSyncStubArgs[4] = "This is not JSON"; + setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); + + assert.throws(RuntimeInfo.environment, "Unexpected token T in JSON at position 0"); + assert.strictEqual(logErrorStub.args[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint -g`"); + }); + }); + + describe("version()", () => { + it("should return the version of the package defined in package.json", () => { + assert.strictEqual(RuntimeInfo.version(), `v${packageJson.version}`); + }); + }); }); }); });