]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/cli-engine/lint-result-cache.js
a3b6ee3ab2c0ef3626d520676b95bbf4bb283b48
2 * @fileoverview Unit tests for lint result cache.
3 * @author Kevin Partington
7 //-----------------------------------------------------------------------------
9 //-----------------------------------------------------------------------------
11 const assert
= require("chai").assert
,
12 { CLIEngine
} = require("../../../lib/cli-engine"),
14 path
= require("path"),
15 proxyquire
= require("proxyquire"),
16 sinon
= require("sinon");
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 describe("LintResultCache", () => {
23 const fixturePath
= path
.resolve(__dirname
, "../../fixtures/lint-result-cache");
24 const cacheFileLocation
= path
.join(fixturePath
, ".eslintcache");
25 const fileEntryCacheStubs
= {};
31 fakeErrorResultsAutofix
;
34 hashStub
= sinon
.stub();
36 let shouldFix
= false;
38 // Get lint results for test fixtures
39 const cliEngine
= new CLIEngine({
42 globInputPaths
: false,
46 // Get results without autofixing...
47 fakeErrorResults
= cliEngine
.executeOnFiles([path
.join(fixturePath
, "test-with-errors.js")]).results
[0];
49 // ...and with autofixing
51 fakeErrorResultsAutofix
= cliEngine
.executeOnFiles([path
.join(fixturePath
, "test-with-errors.js")]).results
[0];
53 // Set up LintResultCache with fake fileEntryCache module
54 LintResultCache
= proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
55 "file-entry-cache": fileEntryCacheStubs
,
63 fs
.unlink(cacheFileLocation
, err
=> {
64 if (err
&& err
.code
!== "ENOENT") {
72 describe("constructor", () => {
73 it("should throw an error if cache file path is not provided", () => {
74 assert
.throws(() => new LintResultCache(), /Cache file location is required/u);
77 it("should successfully create an instance if cache file location is provided", () => {
78 const instance
= new LintResultCache(cacheFileLocation
);
80 assert
.ok(instance
, "Instance should have been created successfully");
84 describe("getCachedLintResults", () => {
85 const filePath
= path
.join(fixturePath
, "test-with-errors.js");
86 const hashOfConfig
= "hashOfConfig";
89 getFileDescriptorStub
,
93 getFileDescriptorStub
= sinon
.stub();
95 fileEntryCacheStubs
.create
= () => ({
96 getFileDescriptor
: getFileDescriptorStub
101 delete fileEntryCacheStubs
.create
;
108 // Serialized results will have null source
109 results
: Object
.assign({}, fakeErrorResults
, { source
: null }),
115 getFileDescriptorStub
.withArgs(filePath
)
116 .returns(cacheEntry
);
120 lintResultsCache
= new LintResultCache(cacheFileLocation
);
123 describe("when calculating the hashing", () => {
124 it("contains eslint version during hashing", () => {
125 const version
= "eslint-=-version";
126 const NewLintResultCache
= proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
127 "../../package.json": { version
},
130 const newLintResultCache
= new NewLintResultCache(cacheFileLocation
);
132 newLintResultCache
.getCachedLintResults(filePath
, fakeConfig
);
133 assert
.ok(hashStub
.calledOnce
);
134 assert
.ok(hashStub
.calledWithMatch(version
));
137 it("contains node version during hashing", () => {
138 const version
= "node-=-version";
140 sinon
.stub(process
, "version").value(version
);
141 const NewLintResultCache
= proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
144 const newLintResultCache
= new NewLintResultCache(cacheFileLocation
);
146 newLintResultCache
.getCachedLintResults(filePath
, fakeConfig
);
147 assert
.ok(hashStub
.calledOnce
);
148 assert
.ok(hashStub
.calledWithMatch(version
));
152 describe("When file is changed", () => {
154 hashStub
.returns(hashOfConfig
);
155 cacheEntry
.changed
= true;
158 it("should return null", () => {
159 const result
= lintResultsCache
.getCachedLintResults(filePath
, fakeConfig
);
161 assert
.ok(getFileDescriptorStub
.calledOnce
);
162 assert
.isNull(result
);
166 describe("When config hash is changed", () => {
168 hashStub
.returns("differentHash");
171 it("should return null", () => {
172 const result
= lintResultsCache
.getCachedLintResults(filePath
, fakeConfig
);
174 assert
.ok(getFileDescriptorStub
.calledOnce
);
175 assert
.isNull(result
);
179 describe("When file is not found on filesystem", () => {
181 cacheEntry
.notFound
= true;
182 hashStub
.returns(hashOfConfig
);
185 it("should return null", () => {
186 const result
= lintResultsCache
.getCachedLintResults(filePath
, fakeConfig
);
188 assert
.ok(getFileDescriptorStub
.calledOnce
);
189 assert
.isNull(result
);
193 describe("When file is present and unchanged and config is unchanged", () => {
195 hashStub
.returns(hashOfConfig
);
198 it("should return expected results", () => {
199 const result
= lintResultsCache
.getCachedLintResults(filePath
, fakeConfig
);
201 assert
.deepStrictEqual(result
, fakeErrorResults
);
202 assert
.ok(result
.source
, "source property should be hydrated from filesystem");
207 describe("setCachedLintResults", () => {
208 const filePath
= path
.join(fixturePath
, "test-with-errors.js");
209 const hashOfConfig
= "hashOfConfig";
212 getFileDescriptorStub
,
216 getFileDescriptorStub
= sinon
.stub();
218 fileEntryCacheStubs
.create
= () => ({
219 getFileDescriptor
: getFileDescriptorStub
224 delete fileEntryCacheStubs
.create
;
232 getFileDescriptorStub
.withArgs(filePath
)
233 .returns(cacheEntry
);
237 hashStub
.returns(hashOfConfig
);
239 lintResultsCache
= new LintResultCache(cacheFileLocation
);
242 describe("When lint result has output property", () => {
243 it("does not modify file entry", () => {
244 lintResultsCache
.setCachedLintResults(filePath
, fakeConfig
, fakeErrorResultsAutofix
);
246 assert
.notProperty(cacheEntry
.meta
, "results");
247 assert
.notProperty(cacheEntry
.meta
, "hashOfConfig");
251 describe("When file is not found on filesystem", () => {
253 cacheEntry
.notFound
= true;
256 it("does not modify file entry", () => {
257 lintResultsCache
.setCachedLintResults(filePath
, fakeConfig
, fakeErrorResults
);
259 assert
.notProperty(cacheEntry
.meta
, "results");
260 assert
.notProperty(cacheEntry
.meta
, "hashOfConfig");
264 describe("When file is found on filesystem", () => {
266 lintResultsCache
.setCachedLintResults(filePath
, fakeConfig
, fakeErrorResults
);
269 it("stores hash of config in file entry", () => {
270 assert
.strictEqual(cacheEntry
.meta
.hashOfConfig
, hashOfConfig
);
273 it("stores results (except source) in file entry", () => {
274 const expectedCachedResults
= Object
.assign(
280 assert
.deepStrictEqual(cacheEntry
.meta
.results
, expectedCachedResults
);
284 describe("When file is found and empty", () => {
286 lintResultsCache
.setCachedLintResults(
289 Object
.assign({}, fakeErrorResults
, { source
: "" })
293 it("stores hash of config in file entry", () => {
294 assert
.strictEqual(cacheEntry
.meta
.hashOfConfig
, hashOfConfig
);
297 it("stores results (except source) in file entry", () => {
298 const expectedCachedResults
= Object
.assign(
304 assert
.deepStrictEqual(cacheEntry
.meta
.results
, expectedCachedResults
);
309 describe("reconcile", () => {
314 reconcileStub
= sinon
.stub();
316 fileEntryCacheStubs
.create
= () => ({
317 reconcile
: reconcileStub
322 delete fileEntryCacheStubs
.create
;
326 lintResultsCache
= new LintResultCache(cacheFileLocation
);
329 it("calls reconcile on the underlying cache", () => {
330 lintResultsCache
.reconcile();
332 assert
.isTrue(reconcileStub
.calledOnce
);