]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/shared/runtime-info.js
first commit
[pve-eslint.git] / eslint / tests / lib / shared / runtime-info.js
1 /**
2 * @fileoverview Tests for RuntimeInfo util.
3 * @author Kai Cataldo
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
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");
19
20 //------------------------------------------------------------------------------
21 // Helpers
22 //------------------------------------------------------------------------------
23
24 /**
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.
29 */
30 function setupSpawnSyncStubReturnVals(stub, returnVals) {
31 let stubChain = stub;
32
33 for (const [i, val] of returnVals.entries()) {
34 const returnVal = val instanceof Error
35 ? { error: val }
36 : { stdout: val };
37
38 stubChain = stubChain.onCall(i).returns(returnVal);
39 }
40
41 return stubChain;
42 }
43
44 //------------------------------------------------------------------------------
45 // Tests
46 //------------------------------------------------------------------------------
47
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";
51
52 describe("RuntimeInfo", () => {
53 describe("environment()", () => {
54 let spawnSyncStub;
55 let logErrorStub;
56 let originalProcessArgv;
57 let spawnSyncStubArgs;
58
59 beforeEach(() => {
60 spawnSyncStub = sinon.stub(spawn, "sync");
61 logErrorStub = sinon.stub(log, "error");
62 originalProcessArgv = process.argv;
63 process.argv[1] = LOCAL_ESLINT_BIN_PATH;
64 spawnSyncStubArgs = [
65 "v12.8.0",
66 "6.11.3",
67 unIndent`
68 {
69 "name": "project",
70 "version": "1.0.0",
71 "dependencies": {
72 "eslint": {
73 "version": "6.3.0"
74 }
75 }
76 }
77 `,
78 NPM_BIN_PATH,
79 unIndent`
80 {
81 "dependencies": {
82 "eslint": {
83 "version": "5.16.0",
84 "from": "eslint",
85 "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz"
86 }
87 }
88 }
89 `
90 ];
91 });
92
93 afterEach(() => {
94 spawnSyncStub.restore();
95 logErrorStub.restore();
96 process.argv = originalProcessArgv;
97 });
98
99
100 it("should return a string containing environment information when running local installation", () => {
101 setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs);
102
103 assert.strictEqual(
104 RuntimeInfo.environment(),
105 unIndent`
106 Environment Info:
107
108 Node version: v12.8.0
109 npm version: v6.11.3
110 Local ESLint version: v6.3.0 (Currently used)
111 Global ESLint version: v5.16.0
112 `
113 );
114 });
115
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;
119
120 assert.strictEqual(
121 RuntimeInfo.environment(),
122 unIndent`
123 Environment Info:
124
125 Node version: v12.8.0
126 npm version: v6.11.3
127 Local ESLint version: v6.3.0
128 Global ESLint version: v5.16.0 (Currently used)
129 `
130 );
131 });
132
133 it("should return a string containing environment information when not installed locally", () => {
134 spawnSyncStubArgs.splice(2, 2, unIndent`
135 {
136 "name": "project",
137 "version": "1.0.0"
138 }
139 `);
140 spawnSyncStubArgs.push(NPM_BIN_PATH);
141 setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs);
142 process.argv[1] = GLOBAL_ESLINT_BIN_PATH;
143
144 assert.strictEqual(
145 RuntimeInfo.environment(),
146 unIndent`
147 Environment Info:
148
149 Node version: v12.8.0
150 npm version: v6.11.3
151 Local ESLint version: Not found
152 Global ESLint version: v5.16.0 (Currently used)
153 `
154 );
155 });
156
157 it("should return a string containing environment information when not installed globally", () => {
158 spawnSyncStubArgs[4] = "{}";
159 setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs);
160
161 assert.strictEqual(
162 RuntimeInfo.environment(),
163 unIndent`
164 Environment Info:
165
166 Node version: v12.8.0
167 npm version: v6.11.3
168 Local ESLint version: v6.3.0 (Currently used)
169 Global ESLint version: Not found
170 `
171 );
172 });
173
174 it("log and throw an error when npm version can not be found", () => {
175 const expectedErr = new Error("npm can not be found");
176
177 spawnSyncStubArgs[1] = expectedErr;
178 setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs);
179
180 assert.throws(RuntimeInfo.environment, expectedErr);
181 assert.strictEqual(logErrorStub.args[0][0], "Error finding npm version running the command `npm --version`");
182 });
183
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");
186
187 spawnSyncStubArgs[3] = expectedErr;
188 setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs);
189
190 assert.throws(RuntimeInfo.environment, expectedErr);
191 assert.strictEqual(logErrorStub.args[0][0], "Error finding npm binary path when running command `npm bin -g`");
192 });
193
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);
197
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`");
200 });
201
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);
205
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`");
208 });
209 });
210
211 describe("version()", () => {
212 it("should return the version of the package defined in package.json", () => {
213 assert.strictEqual(RuntimeInfo.version(), `v${packageJson.version}`);
214 });
215 });
216 });