]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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"); | |
5422a9cc | 15 | const os = require("os"); |
56c4a2cb | 16 | const { unIndent } = require("../../_utils"); |
eb39fafa DC |
17 | const RuntimeInfo = require("../../../lib/shared/runtime-info"); |
18 | const log = require("../../../lib/shared/logging"); | |
19 | const packageJson = require("../../../package.json"); | |
20 | ||
21 | //------------------------------------------------------------------------------ | |
22 | // Helpers | |
23 | //------------------------------------------------------------------------------ | |
24 | ||
25 | /** | |
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. | |
30 | */ | |
31 | function setupSpawnSyncStubReturnVals(stub, returnVals) { | |
32 | let stubChain = stub; | |
33 | ||
34 | for (const [i, val] of returnVals.entries()) { | |
35 | const returnVal = val instanceof Error | |
36 | ? { error: val } | |
37 | : { stdout: val }; | |
38 | ||
39 | stubChain = stubChain.onCall(i).returns(returnVal); | |
40 | } | |
41 | ||
42 | return stubChain; | |
43 | } | |
44 | ||
45 | //------------------------------------------------------------------------------ | |
46 | // Tests | |
47 | //------------------------------------------------------------------------------ | |
48 | ||
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"; | |
52 | ||
53 | describe("RuntimeInfo", () => { | |
54 | describe("environment()", () => { | |
55 | let spawnSyncStub; | |
56 | let logErrorStub; | |
57 | let originalProcessArgv; | |
58 | let spawnSyncStubArgs; | |
5422a9cc TL |
59 | const originalOsPlatform = os.platform; |
60 | const originalOsRelease = os.release; | |
eb39fafa DC |
61 | |
62 | beforeEach(() => { | |
5422a9cc TL |
63 | os.platform = () => "darwin"; |
64 | os.release = () => "20.3.0"; | |
eb39fafa DC |
65 | spawnSyncStub = sinon.stub(spawn, "sync"); |
66 | logErrorStub = sinon.stub(log, "error"); | |
67 | originalProcessArgv = process.argv; | |
68 | process.argv[1] = LOCAL_ESLINT_BIN_PATH; | |
69 | spawnSyncStubArgs = [ | |
70 | "v12.8.0", | |
71 | "6.11.3", | |
72 | unIndent` | |
73 | { | |
74 | "name": "project", | |
75 | "version": "1.0.0", | |
76 | "dependencies": { | |
77 | "eslint": { | |
78 | "version": "6.3.0" | |
79 | } | |
80 | } | |
81 | } | |
82 | `, | |
83 | NPM_BIN_PATH, | |
84 | unIndent` | |
85 | { | |
86 | "dependencies": { | |
87 | "eslint": { | |
88 | "version": "5.16.0", | |
89 | "from": "eslint", | |
90 | "resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz" | |
91 | } | |
92 | } | |
93 | } | |
94 | ` | |
95 | ]; | |
96 | }); | |
97 | ||
98 | afterEach(() => { | |
99 | spawnSyncStub.restore(); | |
100 | logErrorStub.restore(); | |
101 | process.argv = originalProcessArgv; | |
5422a9cc TL |
102 | os.platform = originalOsPlatform; |
103 | os.release = originalOsRelease; | |
eb39fafa DC |
104 | }); |
105 | ||
106 | ||
107 | it("should return a string containing environment information when running local installation", () => { | |
108 | setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); | |
109 | ||
110 | assert.strictEqual( | |
111 | RuntimeInfo.environment(), | |
112 | unIndent` | |
113 | Environment Info: | |
114 | ||
115 | Node version: v12.8.0 | |
116 | npm version: v6.11.3 | |
117 | Local ESLint version: v6.3.0 (Currently used) | |
118 | Global ESLint version: v5.16.0 | |
5422a9cc | 119 | Operating System: darwin 20.3.0 |
eb39fafa DC |
120 | ` |
121 | ); | |
122 | }); | |
123 | ||
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; | |
127 | ||
128 | assert.strictEqual( | |
129 | RuntimeInfo.environment(), | |
130 | unIndent` | |
131 | Environment Info: | |
132 | ||
133 | Node version: v12.8.0 | |
134 | npm version: v6.11.3 | |
135 | Local ESLint version: v6.3.0 | |
136 | Global ESLint version: v5.16.0 (Currently used) | |
5422a9cc | 137 | Operating System: darwin 20.3.0 |
eb39fafa DC |
138 | ` |
139 | ); | |
140 | }); | |
141 | ||
142 | it("should return a string containing environment information when not installed locally", () => { | |
143 | spawnSyncStubArgs.splice(2, 2, unIndent` | |
144 | { | |
145 | "name": "project", | |
146 | "version": "1.0.0" | |
147 | } | |
148 | `); | |
149 | spawnSyncStubArgs.push(NPM_BIN_PATH); | |
150 | setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); | |
151 | process.argv[1] = GLOBAL_ESLINT_BIN_PATH; | |
152 | ||
153 | assert.strictEqual( | |
154 | RuntimeInfo.environment(), | |
155 | unIndent` | |
156 | Environment Info: | |
157 | ||
158 | Node version: v12.8.0 | |
159 | npm version: v6.11.3 | |
160 | Local ESLint version: Not found | |
161 | Global ESLint version: v5.16.0 (Currently used) | |
5422a9cc | 162 | Operating System: darwin 20.3.0 |
eb39fafa DC |
163 | ` |
164 | ); | |
165 | }); | |
166 | ||
167 | it("should return a string containing environment information when not installed globally", () => { | |
168 | spawnSyncStubArgs[4] = "{}"; | |
169 | setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); | |
170 | ||
171 | assert.strictEqual( | |
172 | RuntimeInfo.environment(), | |
173 | unIndent` | |
174 | Environment Info: | |
175 | ||
176 | Node version: v12.8.0 | |
177 | npm version: v6.11.3 | |
178 | Local ESLint version: v6.3.0 (Currently used) | |
179 | Global ESLint version: Not found | |
5422a9cc | 180 | Operating System: darwin 20.3.0 |
eb39fafa DC |
181 | ` |
182 | ); | |
183 | }); | |
184 | ||
185 | it("log and throw an error when npm version can not be found", () => { | |
186 | const expectedErr = new Error("npm can not be found"); | |
187 | ||
188 | spawnSyncStubArgs[1] = expectedErr; | |
189 | setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); | |
190 | ||
191 | assert.throws(RuntimeInfo.environment, expectedErr); | |
192 | assert.strictEqual(logErrorStub.args[0][0], "Error finding npm version running the command `npm --version`"); | |
193 | }); | |
194 | ||
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"); | |
197 | ||
198 | spawnSyncStubArgs[3] = expectedErr; | |
199 | setupSpawnSyncStubReturnVals(spawnSyncStub, spawnSyncStubArgs); | |
200 | ||
201 | assert.throws(RuntimeInfo.environment, expectedErr); | |
202 | assert.strictEqual(logErrorStub.args[0][0], "Error finding npm binary path when running command `npm bin -g`"); | |
203 | }); | |
204 | ||
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); | |
208 | ||
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`"); | |
211 | }); | |
212 | ||
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); | |
216 | ||
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`"); | |
219 | }); | |
220 | }); | |
221 | ||
222 | describe("version()", () => { | |
223 | it("should return the version of the package defined in package.json", () => { | |
224 | assert.strictEqual(RuntimeInfo.version(), `v${packageJson.version}`); | |
225 | }); | |
226 | }); | |
227 | }); |