/
runtime-info.js
216 lines (179 loc) · 8.01 KB
/
runtime-info.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @fileoverview Tests for RuntimeInfo util.
* @author Kai Cataldo
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert;
const sinon = require("sinon");
const spawn = require("cross-spawn");
const { unIndent } = require("../_utils");
const RuntimeInfo = require("../../../lib/shared/runtime-info");
const log = require("../../../lib/shared/logging");
const packageJson = require("../../../package.json");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Sets up spawn.sync() stub calls to return values and throw errors in the order in which they are given.
* @param {Function} stub - The stub to set up.
* @param {Array} returnVals - Values to be returned by subsequent stub calls.
* @returns {Function} The set up stub.
*/
function setupSpawnSyncStubReturnVals(stub, returnVals) {
let stubChain = stub;
for (const [i, val] of returnVals.entries()) {
const returnVal = val instanceof Error
? { error: val }
: { stdout: val };
stubChain = stubChain.onCall(i).returns(returnVal);
}
return stubChain;
}
//------------------------------------------------------------------------------
// 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 NPM_BIN_PATH = "/usr/local/bin/npm";
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"
}
}
}
`,
NPM_BIN_PATH,
unIndent`
{
"dependencies": {
"eslint": {
"version": "5.16.0",
"from": "eslint",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-6.16.0.tgz"
}
}
}
`
];
});
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"
}
`);
spawnSyncStubArgs.push(NPM_BIN_PATH);
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}`);
});
});
});