forked from eslint/eslint
/
lint-result-cache.js
335 lines (260 loc) · 10.6 KB
/
lint-result-cache.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* @fileoverview Unit tests for lint result cache.
* @author Kevin Partington
*/
"use strict";
//-----------------------------------------------------------------------------
// Requirements
//-----------------------------------------------------------------------------
const assert = require("chai").assert,
{ CLIEngine } = require("../../../lib/cli-engine"),
fs = require("fs"),
path = require("path"),
proxyquire = require("proxyquire"),
sinon = require("sinon");
//-----------------------------------------------------------------------------
// Tests
//-----------------------------------------------------------------------------
describe("LintResultCache", () => {
const fixturePath = path.resolve(__dirname, "../../fixtures/lint-result-cache");
const cacheFileLocation = path.join(fixturePath, ".eslintcache");
const fileEntryCacheStubs = {};
let LintResultCache,
hashStub,
fakeConfig,
fakeErrorResults,
fakeErrorResultsAutofix;
before(() => {
hashStub = sinon.stub();
let shouldFix = false;
// Get lint results for test fixtures
const cliEngine = new CLIEngine({
cache: false,
ignore: false,
globInputPaths: false,
fix: () => shouldFix
});
// Get results without autofixing...
fakeErrorResults = cliEngine.executeOnFiles([path.join(fixturePath, "test-with-errors.js")]).results[0];
// ...and with autofixing
shouldFix = true;
fakeErrorResultsAutofix = cliEngine.executeOnFiles([path.join(fixturePath, "test-with-errors.js")]).results[0];
// Set up LintResultCache with fake fileEntryCache module
LintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
"file-entry-cache": fileEntryCacheStubs,
"./hash": hashStub
});
});
afterEach(done => {
sinon.reset();
fs.unlink(cacheFileLocation, err => {
if (err && err.code !== "ENOENT") {
return done(err);
}
return done();
});
});
describe("constructor", () => {
it("should throw an error if cache file path is not provided", () => {
assert.throws(() => new LintResultCache(), /Cache file location is required/u);
});
it("should successfully create an instance if cache file location is provided", () => {
const instance = new LintResultCache(cacheFileLocation);
assert.ok(instance, "Instance should have been created successfully");
});
});
describe("getCachedLintResults", () => {
const filePath = path.join(fixturePath, "test-with-errors.js");
const hashOfConfig = "hashOfConfig";
let cacheEntry,
getFileDescriptorStub,
lintResultsCache;
before(() => {
getFileDescriptorStub = sinon.stub();
fileEntryCacheStubs.create = () => ({
getFileDescriptor: getFileDescriptorStub
});
});
after(() => {
delete fileEntryCacheStubs.create;
});
beforeEach(() => {
cacheEntry = {
meta: {
// Serialized results will have null source
results: Object.assign({}, fakeErrorResults, { source: null }),
hashOfConfig
}
};
getFileDescriptorStub.withArgs(filePath)
.returns(cacheEntry);
fakeConfig = {};
lintResultsCache = new LintResultCache(cacheFileLocation);
});
describe("when calculating the hashing", () => {
it("contains eslint version during hashing", () => {
const version = "eslint-=-version";
const NewLintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
"../../package.json": { version },
"./hash": hashStub
});
const newLintResultCache = new NewLintResultCache(cacheFileLocation);
newLintResultCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(hashStub.calledOnce);
assert.ok(hashStub.calledWithMatch(version));
});
it("contains node version during hashing", () => {
const version = "node-=-version";
sinon.stub(process, "version").value(version);
const NewLintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
"./hash": hashStub
});
const newLintResultCache = new NewLintResultCache(cacheFileLocation);
newLintResultCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(hashStub.calledOnce);
assert.ok(hashStub.calledWithMatch(version));
});
});
describe("When file is changed", () => {
beforeEach(() => {
hashStub.returns(hashOfConfig);
cacheEntry.changed = true;
});
it("should return null", () => {
const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
});
describe("When config hash is changed", () => {
beforeEach(() => {
hashStub.returns("differentHash");
});
it("should return null", () => {
const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
});
describe("When file is not found on filesystem", () => {
beforeEach(() => {
cacheEntry.notFound = true;
hashStub.returns(hashOfConfig);
});
it("should return null", () => {
const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
});
describe("When file is present and unchanged and config is unchanged", () => {
beforeEach(() => {
hashStub.returns(hashOfConfig);
});
it("should return expected results", () => {
const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
assert.deepStrictEqual(result, fakeErrorResults);
assert.ok(result.source, "source property should be hydrated from filesystem");
});
});
});
describe("setCachedLintResults", () => {
const filePath = path.join(fixturePath, "test-with-errors.js");
const hashOfConfig = "hashOfConfig";
let cacheEntry,
getFileDescriptorStub,
lintResultsCache;
before(() => {
getFileDescriptorStub = sinon.stub();
fileEntryCacheStubs.create = () => ({
getFileDescriptor: getFileDescriptorStub
});
});
after(() => {
delete fileEntryCacheStubs.create;
});
beforeEach(() => {
cacheEntry = {
meta: {}
};
getFileDescriptorStub.withArgs(filePath)
.returns(cacheEntry);
fakeConfig = {};
hashStub.returns(hashOfConfig);
lintResultsCache = new LintResultCache(cacheFileLocation);
});
describe("When lint result has output property", () => {
it("does not modify file entry", () => {
lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResultsAutofix);
assert.notProperty(cacheEntry.meta, "results");
assert.notProperty(cacheEntry.meta, "hashOfConfig");
});
});
describe("When file is not found on filesystem", () => {
beforeEach(() => {
cacheEntry.notFound = true;
});
it("does not modify file entry", () => {
lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
assert.notProperty(cacheEntry.meta, "results");
assert.notProperty(cacheEntry.meta, "hashOfConfig");
});
});
describe("When file is found on filesystem", () => {
beforeEach(() => {
lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
});
it("stores hash of config in file entry", () => {
assert.strictEqual(cacheEntry.meta.hashOfConfig, hashOfConfig);
});
it("stores results (except source) in file entry", () => {
const expectedCachedResults = Object.assign(
{},
fakeErrorResults,
{ source: null }
);
assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
});
});
describe("When file is found and empty", () => {
beforeEach(() => {
lintResultsCache.setCachedLintResults(
filePath,
fakeConfig,
Object.assign({}, fakeErrorResults, { source: "" })
);
});
it("stores hash of config in file entry", () => {
assert.strictEqual(cacheEntry.meta.hashOfConfig, hashOfConfig);
});
it("stores results (except source) in file entry", () => {
const expectedCachedResults = Object.assign(
{},
fakeErrorResults,
{ source: null }
);
assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
});
});
});
describe("reconcile", () => {
let reconcileStub,
lintResultsCache;
before(() => {
reconcileStub = sinon.stub();
fileEntryCacheStubs.create = () => ({
reconcile: reconcileStub
});
});
after(() => {
delete fileEntryCacheStubs.create;
});
beforeEach(() => {
lintResultsCache = new LintResultCache(cacheFileLocation);
});
it("calls reconcile on the underlying cache", () => {
lintResultsCache.reconcile();
assert.isTrue(reconcileStub.calledOnce);
});
});
});