]>
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 os
= require("os");
16 const { unIndent
} = require("../../_utils");
17 const RuntimeInfo
= require("../../../lib/shared/runtime-info");
18 const log
= require("../../../lib/shared/logging");
19 const packageJson
= require("../../../package.json");
21 //------------------------------------------------------------------------------
23 //------------------------------------------------------------------------------
26 * Sets up spawn.sync() stub calls to return values and throw errors in the order in which they are given.
27 * @param {Function} stub The stub to set up.
28 * @param {Array} returnVals Values to be returned by subsequent stub calls.
29 * @returns {Function} The set up stub.
31 function setupSpawnSyncStubReturnVals(stub
, returnVals
) {
34 for (const [i
, val
] of returnVals
.entries()) {
35 const returnVal
= val
instanceof Error
39 stubChain
= stubChain
.onCall(i
).returns(returnVal
);
45 //------------------------------------------------------------------------------
47 //------------------------------------------------------------------------------
49 const LOCAL_ESLINT_BIN_PATH
= "/Users/username/code/project/node_modules/eslint/bin/eslint.js";
50 const GLOBAL_ESLINT_BIN_PATH
= "/usr/local/bin/npm/node_modules/eslint/bin/eslint.js";
51 const NPM_BIN_PATH
= "/usr/local/bin/npm";
53 describe("RuntimeInfo", () => {
54 describe("environment()", () => {
57 let originalProcessArgv
;
58 let spawnSyncStubArgs
;
59 const originalOsPlatform
= os
.platform
;
60 const originalOsRelease
= os
.release
;
63 os
.platform
= () => "darwin";
64 os
.release
= () => "20.3.0";
65 spawnSyncStub
= sinon
.stub(spawn
, "sync");
66 logErrorStub
= sinon
.stub(log
, "error");
67 originalProcessArgv
= process
.argv
;
68 process
.argv
[1] = LOCAL_ESLINT_BIN_PATH
;
90 "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz"
99 spawnSyncStub
.restore();
100 logErrorStub
.restore();
101 process
.argv
= originalProcessArgv
;
102 os
.platform
= originalOsPlatform
;
103 os
.release
= originalOsRelease
;
107 it("should return a string containing environment information when running local installation", () => {
108 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
111 RuntimeInfo
.environment(),
115 Node version: v12.8.0
117 Local ESLint version: v6.3.0 (Currently used)
118 Global ESLint version: v5.16.0
119 Operating System: darwin 20.3.0
124 it("should return a string containing environment information when running global installation", () => {
125 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
126 process
.argv
[1] = GLOBAL_ESLINT_BIN_PATH
;
129 RuntimeInfo
.environment(),
133 Node version: v12.8.0
135 Local ESLint version: v6.3.0
136 Global ESLint version: v5.16.0 (Currently used)
137 Operating System: darwin 20.3.0
142 it("should return a string containing environment information when not installed locally", () => {
143 spawnSyncStubArgs
.splice(2, 2, unIndent
`
149 spawnSyncStubArgs
.push(NPM_BIN_PATH
);
150 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
151 process
.argv
[1] = GLOBAL_ESLINT_BIN_PATH
;
154 RuntimeInfo
.environment(),
158 Node version: v12.8.0
160 Local ESLint version: Not found
161 Global ESLint version: v5.16.0 (Currently used)
162 Operating System: darwin 20.3.0
167 it("should return a string containing environment information when not installed globally", () => {
168 spawnSyncStubArgs
[4] = "{}";
169 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
172 RuntimeInfo
.environment(),
176 Node version: v12.8.0
178 Local ESLint version: v6.3.0 (Currently used)
179 Global ESLint version: Not found
180 Operating System: darwin 20.3.0
185 it("log and throw an error when npm version can not be found", () => {
186 const expectedErr
= new Error("npm can not be found");
188 spawnSyncStubArgs
[1] = expectedErr
;
189 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
191 assert
.throws(RuntimeInfo
.environment
, expectedErr
);
192 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding npm version running the command `npm --version`");
195 it("log and throw an error when npm binary path can not be found", () => {
196 const expectedErr
= new Error("npm can not be found");
198 spawnSyncStubArgs
[3] = expectedErr
;
199 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
201 assert
.throws(RuntimeInfo
.environment
, expectedErr
);
202 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding npm binary path when running command `npm bin -g`");
205 it("log and throw an error when checking for local ESLint version when returned output of command is malformed", () => {
206 spawnSyncStubArgs
[2] = "This is not JSON";
207 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
209 assert
.throws(RuntimeInfo
.environment
, "Unexpected token T in JSON at position 0");
210 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint`");
213 it("log and throw an error when checking for global ESLint version when returned output of command is malformed", () => {
214 spawnSyncStubArgs
[4] = "This is not JSON";
215 setupSpawnSyncStubReturnVals(spawnSyncStub
, spawnSyncStubArgs
);
217 assert
.throws(RuntimeInfo
.environment
, "Unexpected token T in JSON at position 0");
218 assert
.strictEqual(logErrorStub
.args
[0][0], "Error finding eslint version running the command `npm ls --depth=0 --json eslint -g`");
222 describe("version()", () => {
223 it("should return the version of the package defined in package.json", () => {
224 assert
.strictEqual(RuntimeInfo
.version(), `v${packageJson.version}`);