]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/tests/lib/cli-engine/lint-result-cache.js
import eslint 7.28.0
[pve-eslint.git] / eslint / tests / lib / cli-engine / lint-result-cache.js
index a3b6ee3ab2c0ef3626d520676b95bbf4bb283b48..41c72308f42769720d1ac1d76c733ce192271f7e 100644 (file)
@@ -20,18 +20,23 @@ const assert = require("chai").assert,
 //-----------------------------------------------------------------------------
 
 describe("LintResultCache", () => {
-    const fixturePath = path.resolve(__dirname, "../../fixtures/lint-result-cache");
+    const fixturePath = path.resolve(
+        __dirname,
+        "../../fixtures/lint-result-cache"
+    );
     const cacheFileLocation = path.join(fixturePath, ".eslintcache");
     const fileEntryCacheStubs = {};
 
     let LintResultCache,
         hashStub,
+        sandbox,
         fakeConfig,
         fakeErrorResults,
         fakeErrorResultsAutofix;
 
     before(() => {
-        hashStub = sinon.stub();
+        sandbox = sinon.createSandbox();
+        hashStub = sandbox.stub();
 
         let shouldFix = false;
 
@@ -44,21 +49,28 @@ describe("LintResultCache", () => {
         });
 
         // Get results without autofixing...
-        fakeErrorResults = cliEngine.executeOnFiles([path.join(fixturePath, "test-with-errors.js")]).results[0];
+        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];
+        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
-        });
+        LintResultCache = proxyquire(
+            "../../../lib/cli-engine/lint-result-cache.js",
+            {
+                "file-entry-cache": fileEntryCacheStubs,
+                "./hash": hashStub
+            }
+        );
     });
 
     afterEach(done => {
-        sinon.reset();
+        sandbox.reset();
 
         fs.unlink(cacheFileLocation, err => {
             if (err && err.code !== "ENOENT") {
@@ -71,11 +83,28 @@ describe("LintResultCache", () => {
 
     describe("constructor", () => {
         it("should throw an error if cache file path is not provided", () => {
-            assert.throws(() => new LintResultCache(), /Cache file location is required/u);
+            assert.throws(
+                () => new LintResultCache(),
+                /Cache file location is required/u
+            );
+        });
+
+        it("should throw an error if cacheStrategy is not provided", () => {
+            assert.throws(
+                () => new LintResultCache(cacheFileLocation),
+                /Cache strategy is required/u
+            );
         });
 
-        it("should successfully create an instance if cache file location is provided", () => {
-            const instance = new LintResultCache(cacheFileLocation);
+        it("should throw an error if cacheStrategy is an invalid value", () => {
+            assert.throws(
+                () => new LintResultCache(cacheFileLocation, "foo"),
+                /Cache strategy must be one of/u
+            );
+        });
+
+        it("should successfully create an instance if cache file location and cache strategy provided", () => {
+            const instance = new LintResultCache(cacheFileLocation, "metadata");
 
             assert.ok(instance, "Instance should have been created successfully");
         });
@@ -85,12 +114,10 @@ describe("LintResultCache", () => {
         const filePath = path.join(fixturePath, "test-with-errors.js");
         const hashOfConfig = "hashOfConfig";
 
-        let cacheEntry,
-            getFileDescriptorStub,
-            lintResultsCache;
+        let cacheEntry, getFileDescriptorStub, lintResultsCache;
 
         before(() => {
-            getFileDescriptorStub = sinon.stub();
+            getFileDescriptorStub = sandbox.stub();
 
             fileEntryCacheStubs.create = () => ({
                 getFileDescriptor: getFileDescriptorStub
@@ -112,12 +139,11 @@ describe("LintResultCache", () => {
                 }
             };
 
-            getFileDescriptorStub.withArgs(filePath)
-                .returns(cacheEntry);
+            getFileDescriptorStub.withArgs(filePath).returns(cacheEntry);
 
             fakeConfig = {};
 
-            lintResultsCache = new LintResultCache(cacheFileLocation);
+            lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
         });
 
         describe("when calculating the hashing", () => {
@@ -127,7 +153,7 @@ describe("LintResultCache", () => {
                     "../../package.json": { version },
                     "./hash": hashStub
                 });
-                const newLintResultCache = new NewLintResultCache(cacheFileLocation);
+                const newLintResultCache = new NewLintResultCache(cacheFileLocation, "metadata");
 
                 newLintResultCache.getCachedLintResults(filePath, fakeConfig);
                 assert.ok(hashStub.calledOnce);
@@ -137,13 +163,14 @@ describe("LintResultCache", () => {
             it("contains node version during hashing", () => {
                 const version = "node-=-version";
 
-                sinon.stub(process, "version").value(version);
+                sandbox.stub(process, "version").value(version);
                 const NewLintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
                     "./hash": hashStub
                 });
-                const newLintResultCache = new NewLintResultCache(cacheFileLocation);
+                const newLintResultCache = new NewLintResultCache(cacheFileLocation, "metadata");
 
                 newLintResultCache.getCachedLintResults(filePath, fakeConfig);
+
                 assert.ok(hashStub.calledOnce);
                 assert.ok(hashStub.calledWithMatch(version));
             });
@@ -156,7 +183,10 @@ describe("LintResultCache", () => {
             });
 
             it("should return null", () => {
-                const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+                const result = lintResultsCache.getCachedLintResults(
+                    filePath,
+                    fakeConfig
+                );
 
                 assert.ok(getFileDescriptorStub.calledOnce);
                 assert.isNull(result);
@@ -169,7 +199,10 @@ describe("LintResultCache", () => {
             });
 
             it("should return null", () => {
-                const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+                const result = lintResultsCache.getCachedLintResults(
+                    filePath,
+                    fakeConfig
+                );
 
                 assert.ok(getFileDescriptorStub.calledOnce);
                 assert.isNull(result);
@@ -183,7 +216,10 @@ describe("LintResultCache", () => {
             });
 
             it("should return null", () => {
-                const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+                const result = lintResultsCache.getCachedLintResults(
+                    filePath,
+                    fakeConfig
+                );
 
                 assert.ok(getFileDescriptorStub.calledOnce);
                 assert.isNull(result);
@@ -196,10 +232,16 @@ describe("LintResultCache", () => {
             });
 
             it("should return expected results", () => {
-                const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+                const result = lintResultsCache.getCachedLintResults(
+                    filePath,
+                    fakeConfig
+                );
 
                 assert.deepStrictEqual(result, fakeErrorResults);
-                assert.ok(result.source, "source property should be hydrated from filesystem");
+                assert.ok(
+                    result.source,
+                    "source property should be hydrated from filesystem"
+                );
             });
         });
     });
@@ -208,12 +250,10 @@ describe("LintResultCache", () => {
         const filePath = path.join(fixturePath, "test-with-errors.js");
         const hashOfConfig = "hashOfConfig";
 
-        let cacheEntry,
-            getFileDescriptorStub,
-            lintResultsCache;
+        let cacheEntry, getFileDescriptorStub, lintResultsCache;
 
         before(() => {
-            getFileDescriptorStub = sinon.stub();
+            getFileDescriptorStub = sandbox.stub();
 
             fileEntryCacheStubs.create = () => ({
                 getFileDescriptor: getFileDescriptorStub
@@ -229,19 +269,22 @@ describe("LintResultCache", () => {
                 meta: {}
             };
 
-            getFileDescriptorStub.withArgs(filePath)
-                .returns(cacheEntry);
+            getFileDescriptorStub.withArgs(filePath).returns(cacheEntry);
 
             fakeConfig = {};
 
             hashStub.returns(hashOfConfig);
 
-            lintResultsCache = new LintResultCache(cacheFileLocation);
+            lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
         });
 
         describe("When lint result has output property", () => {
             it("does not modify file entry", () => {
-                lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResultsAutofix);
+                lintResultsCache.setCachedLintResults(
+                    filePath,
+                    fakeConfig,
+                    fakeErrorResultsAutofix
+                );
 
                 assert.notProperty(cacheEntry.meta, "results");
                 assert.notProperty(cacheEntry.meta, "hashOfConfig");
@@ -254,7 +297,11 @@ describe("LintResultCache", () => {
             });
 
             it("does not modify file entry", () => {
-                lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
+                lintResultsCache.setCachedLintResults(
+                    filePath,
+                    fakeConfig,
+                    fakeErrorResults
+                );
 
                 assert.notProperty(cacheEntry.meta, "results");
                 assert.notProperty(cacheEntry.meta, "hashOfConfig");
@@ -263,7 +310,11 @@ describe("LintResultCache", () => {
 
         describe("When file is found on filesystem", () => {
             beforeEach(() => {
-                lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
+                lintResultsCache.setCachedLintResults(
+                    filePath,
+                    fakeConfig,
+                    fakeErrorResults
+                );
             });
 
             it("stores hash of config in file entry", () => {
@@ -271,11 +322,9 @@ describe("LintResultCache", () => {
             });
 
             it("stores results (except source) in file entry", () => {
-                const expectedCachedResults = Object.assign(
-                    {},
-                    fakeErrorResults,
-                    { source: null }
-                );
+                const expectedCachedResults = Object.assign({}, fakeErrorResults, {
+                    source: null
+                });
 
                 assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
             });
@@ -295,11 +344,9 @@ describe("LintResultCache", () => {
             });
 
             it("stores results (except source) in file entry", () => {
-                const expectedCachedResults = Object.assign(
-                    {},
-                    fakeErrorResults,
-                    { source: null }
-                );
+                const expectedCachedResults = Object.assign({}, fakeErrorResults, {
+                    source: null
+                });
 
                 assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
             });
@@ -307,11 +354,10 @@ describe("LintResultCache", () => {
     });
 
     describe("reconcile", () => {
-        let reconcileStub,
-            lintResultsCache;
+        let reconcileStub, lintResultsCache;
 
         before(() => {
-            reconcileStub = sinon.stub();
+            reconcileStub = sandbox.stub();
 
             fileEntryCacheStubs.create = () => ({
                 reconcile: reconcileStub
@@ -323,7 +369,7 @@ describe("LintResultCache", () => {
         });
 
         beforeEach(() => {
-            lintResultsCache = new LintResultCache(cacheFileLocation);
+            lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
         });
 
         it("calls reconcile on the underlying cache", () => {