]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/shared/runtime-info.js
2 * @fileoverview Tests for RuntimeInfo util.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const assert
= require("chai").assert
;
13 const sinon
= require("sinon");
14 const spawn
= require("cross-spawn");
15 const { unIndent
} = require("../_utils");
16 const RuntimeInfo
= require("../../../lib/shared/runtime-info");
17 const log
= require("../../../lib/shared/logging");
18 const packageJson
= require("../../../package.json");
20 //------------------------------------------------------------------------------
22 //------------------------------------------------------------------------------
25 * Sets up spawn.sync() stub calls to return values and throw errors in the order in which they are given.
26 * @param {Function} stub The stub to set up.
27 * @param {Array} returnVals Values to be returned by subsequent stub calls.
28 * @returns {Function} The set up stub.
30 function setupSpawnSyncStubReturnVals(stub
, returnVals
) {
33 for (const [i
, val
] of returnVals
.entries()) {
34 const returnVal
= val
instanceof Error
38 stubChain
= stubChain
.onCall(i
).returns(returnVal
);
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
48 const LOCAL_ESLINT_BIN_PATH
= "/Users/username/code/project/node_modules/eslint/bin/eslint.js";
49 const GLOBAL_ESLINT_BIN_PATH
= "/usr/local/bin/npm/node_modules/eslint/bin/eslint.js";
50 const NPM_BIN_PATH
= "/usr/local/bin/npm";
52 describe("RuntimeInfo", () => {
53 describe("environment()", () => {
56 let originalProcessArgv
;
57 let spawnSyncStubArgs
;
60 spawnSyncStub
= sinon
.stub(spawn
, "sync");
61 logErrorStub
= sinon
.stub(log
, "error");
62 originalProcessArgv
= process
.argv
;
63 process
.argv
[1] = LOCAL_ESLINT_BIN_PATH
;
85 "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz"
94 spawnSyncStub
.restore();
95 logErrorStub
.restore();
96 process
.argv
= originalProcessArgv
;
100 it("should return a string containing environment information when running local installation", () => {
101 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
104 RuntimeInfo
.environment(),
108 Node version: v12.8.0
110 Local ESLint version: v6.3.0 (Currently used)
111 Global ESLint version: v5.16.0
116 it("should return a string containing environment information when running global installation", () => {
117 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
118 process
.argv
[1] = GLOBAL_ESLINT_BIN_PATH
;
121 RuntimeInfo
.environment(),
125 Node version: v12.8.0
127 Local ESLint version: v6.3.0
128 Global ESLint version: v5.16.0 (Currently used)
133 it("should return a string containing environment information when not installed locally", () => {
134 spawnSyncStubArgs
.splice(2, 2, unIndent
`
140 spawnSyncStubArgs
.push(NPM_BIN_PATH
);
141 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
142 process
.argv
[1] = GLOBAL_ESLINT_BIN_PATH
;
145 RuntimeInfo
.environment(),
149 Node version: v12.8.0
151 Local ESLint version: Not found
152 Global ESLint version: v5.16.0 (Currently used)
157 it("should return a string containing environment information when not installed globally", () => {
158 spawnSyncStubArgs
[4] = "{}";
159 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
162 RuntimeInfo
.environment(),
166 Node version: v12.8.0
168 Local ESLint version: v6.3.0 (Currently used)
169 Global ESLint version: Not found
174 it("log and throw an error when npm version can not be found", () => {
175 const expectedErr
= new Error("npm can not be found");
177 spawnSyncStubArgs
[1] = expectedErr
;
178 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
180 assert
.throws(RuntimeInfo
.environment
, expectedErr
);
181 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding npm version running the command `npm --version`");
184 it("log and throw an error when npm binary path can not be found", () => {
185 const expectedErr
= new Error("npm can not be found");
187 spawnSyncStubArgs
[3] = expectedErr
;
188 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
190 assert
.throws(RuntimeInfo
.environment
, expectedErr
);
191 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding npm binary path when running command `npm bin -g`");
194 it("log and throw an error when checking for local ESLint version when returned output of command is malformed", () => {
195 spawnSyncStubArgs
[2] = "This is not JSON";
196 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
198 assert
.throws(RuntimeInfo
.environment
, "Unexpected token T in JSON at position 0");
199 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint`");
202 it("log and throw an error when checking for global ESLint version when returned output of command is malformed", () => {
203 spawnSyncStubArgs
[4] = "This is not JSON";
204 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
206 assert
.throws(RuntimeInfo
.environment
, "Unexpected token T in JSON at position 0");
207 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint -g`");
211 describe("version()", () => {
212 it("should return the version of the package defined in package.json", () => {
213 assert
.strictEqual(RuntimeInfo
.version(), `v${packageJson.version}`);