From 485ec7d08ed2040c292f52bf9b9152f6c8ef4809 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Wed, 1 Nov 2023 16:44:11 +0100 Subject: [PATCH] test: fix ESLint tests for caching (#17699) --- tests/lib/eslint/eslint.js | 391 +++++++++++++++++++------------- tests/lib/eslint/flat-eslint.js | 7 +- 2 files changed, 239 insertions(+), 159 deletions(-) diff --git a/tests/lib/eslint/eslint.js b/tests/lib/eslint/eslint.js index 74abaf42e5d..60480b7cc14 100644 --- a/tests/lib/eslint/eslint.js +++ b/tests/lib/eslint/eslint.js @@ -2321,25 +2321,22 @@ describe("ESLint", () => { } } - /** - * helper method to delete the cache files created during testing - * @returns {void} - */ - function deleteCache() { - doDelete(path.resolve(".eslintcache")); - doDelete(path.resolve(".cache/custom-cache")); - } + let cacheFilePath; beforeEach(() => { - deleteCache(); + cacheFilePath = null; }); afterEach(() => { sinon.restore(); - deleteCache(); + if (cacheFilePath) { + doDelete(cacheFilePath); + } }); - describe("when the cacheFile is a directory or looks like a directory", () => { + describe("when cacheLocation is a directory or looks like a directory", () => { + + const cwd = getFixturePath(); /** * helper method to delete the cache files created during testing @@ -2347,7 +2344,21 @@ describe("ESLint", () => { */ function deleteCacheDir() { try { - fs.unlinkSync("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory"); + + /* + * `fs.rmdir(path, { recursive: true })` is deprecated and will be removed. + * Use `fs.rm(path, { recursive: true })` instead. + * When supporting Node.js 14.14.0+, the compatibility condition can be removed for `fs.rmdir`. + */ + // eslint-disable-next-line n/no-unsupported-features/node-builtins -- just checking if it exists + if (typeof fs.rm === "function") { + + // eslint-disable-next-line n/no-unsupported-features/node-builtins -- conditionally used + fs.rmSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true }); + } else { + fs.rmdirSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true }); + } + } catch { /* @@ -2364,11 +2375,12 @@ describe("ESLint", () => { deleteCacheDir(); }); - it("should create the cache file inside the provided directory", async () => { - assert(!shell.test("-d", path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist"); + it("should create the directory and the cache file inside it when cacheLocation ends with a slash", async () => { + assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted"); eslint = new ESLint({ useEslintrc: false, + cwd, // specifying cache true the cache will be created cache: true, @@ -2386,42 +2398,71 @@ describe("ESLint", () => { await eslint.lintFiles([file]); - assert(shell.test("-f", path.resolve(`./tmp/.cacheFileDir/.cache_${hash(process.cwd())}`)), "the cache for eslint was created"); - - sinon.restore(); + assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created"); }); - }); - it("should create the cache file inside the provided directory using the cacheLocation option", async () => { - assert(!shell.test("-d", path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist"); + it("should create the cache file inside existing cacheLocation directory when cacheLocation ends with a slash", async () => { + assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted"); - eslint = new ESLint({ - useEslintrc: false, + fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"), { recursive: true }); - // specifying cache true the cache will be created - cache: true, - cacheLocation: "./tmp/.cacheFileDir/", - overrideConfig: { - rules: { - "no-console": 0, - "no-unused-vars": 2 - } - }, - extensions: ["js"], - ignore: false + eslint = new ESLint({ + useEslintrc: false, + cwd, + + // specifying cache true the cache will be created + cache: true, + cacheLocation: "./tmp/.cacheFileDir/", + overrideConfig: { + rules: { + "no-console": 0, + "no-unused-vars": 2 + } + }, + ignore: false + }); + const file = getFixturePath("cache/src", "test-file.js"); + + await eslint.lintFiles([file]); + + assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created"); }); - const file = getFixturePath("cache/src", "test-file.js"); - await eslint.lintFiles([file]); + it("should create the cache file inside existing cacheLocation directory when cacheLocation doesn't end with a path separator", async () => { + assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted"); - assert(shell.test("-f", path.resolve(`./tmp/.cacheFileDir/.cache_${hash(process.cwd())}`)), "the cache for eslint was created"); + fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"), { recursive: true }); - sinon.restore(); + eslint = new ESLint({ + useEslintrc: false, + cwd, + + // specifying cache true the cache will be created + cache: true, + cacheLocation: "./tmp/.cacheFileDir", + overrideConfig: { + rules: { + "no-console": 0, + "no-unused-vars": 2 + } + }, + ignore: false + }); + const file = getFixturePath("cache/src", "test-file.js"); + + await eslint.lintFiles([file]); + + assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created"); + }); }); it("should create the cache file inside cwd when no cacheLocation provided", async () => { const cwd = path.resolve(getFixturePath("cli-engine")); + cacheFilePath = path.resolve(cwd, ".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + eslint = new ESLint({ useEslintrc: false, cache: true, @@ -2438,14 +2479,19 @@ describe("ESLint", () => { await eslint.lintFiles([file]); - assert(shell.test("-f", path.resolve(cwd, ".eslintcache")), "the cache for eslint was created at provided cwd"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created at provided cwd"); }); it("should invalidate the cache if the configuration changed between executions", async () => { - assert(!shell.test("-f", path.resolve(".eslintcache")), "the cache for eslint does not exist"); + const cwd = getFixturePath("cache/src"); + + cacheFilePath = path.resolve(cwd, ".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ useEslintrc: false, + cwd, // specifying cache true the cache will be created cache: true, @@ -2467,16 +2513,17 @@ describe("ESLint", () => { const results = await eslint.lintFiles([file]); for (const { errorCount, warningCount } of results) { - assert.strictEqual(errorCount + warningCount, 0, "the file passed without errors or warnings"); + assert.strictEqual(errorCount + warningCount, 0, "the file should have passed linting without errors or warnings"); } - assert.strictEqual(spy.getCall(0).args[0], file, "the module read the file because is considered changed"); - assert(shell.test("-f", path.resolve(".eslintcache")), "the cache for eslint was created"); + assert(spy.calledWith(file), "ESLint should have read the file because there was no cache file"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created"); // destroy the spy sinon.restore(); eslint = new ESLint({ useEslintrc: false, + cwd, // specifying cache true the cache will be created cache: true, @@ -2493,18 +2540,24 @@ describe("ESLint", () => { // create a new spy spy = sinon.spy(fs, "readFileSync"); - const [cachedResult] = await eslint.lintFiles([file]); + const [newResult] = await eslint.lintFiles([file]); - assert.strictEqual(spy.getCall(0).args[0], file, "the module read the file because is considered changed because the config changed"); - assert.strictEqual(cachedResult.errorCount, 1, "since configuration changed the cache was not used an one error was reported"); - assert(shell.test("-f", path.resolve(".eslintcache")), "the cache for eslint was created"); + assert(spy.calledWith(file), "ESLint should have read the file again because it's considered changed because the config changed"); + assert.strictEqual(newResult.errorCount, 1, "since configuration changed the cache should have not been used and one error should have been reported"); + assert.strictEqual(newResult.messages[0].ruleId, "no-console"); + assert(shell.test("-f", cacheFilePath), "The cache for ESLint should still exist"); }); it("should remember the files from a previous run and do not operate on them if not changed", async () => { - assert(!shell.test("-f", path.resolve(".eslintcache")), "the cache for eslint does not exist"); + const cwd = getFixturePath("cache/src"); + + cacheFilePath = path.resolve(cwd, ".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ useEslintrc: false, + cwd, // specifying cache true the cache will be created cache: true, @@ -2526,14 +2579,15 @@ describe("ESLint", () => { const result = await eslint.lintFiles([file]); - assert.strictEqual(spy.getCall(0).args[0], file, "the module read the file because is considered changed"); - assert(shell.test("-f", path.resolve(".eslintcache")), "the cache for eslint was created"); + assert(spy.calledWith(file), "ESLint should have read the file because there was no cache file"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created"); // destroy the spy sinon.restore(); eslint = new ESLint({ useEslintrc: false, + cwd, // specifying cache true the cache will be created cache: true, @@ -2552,20 +2606,23 @@ describe("ESLint", () => { const cachedResult = await eslint.lintFiles([file]); - assert.deepStrictEqual(result, cachedResult, "the result is the same regardless of using cache or not"); + assert.deepStrictEqual(result, cachedResult, "the result should have been the same"); // assert the file was not processed because the cache was used - assert(!spy.calledWith(file), "the file was not loaded because it used the cache"); + assert(!spy.calledWith(file), "the file should not have been reloaded"); }); - it("should remember the files from a previous run and do not operate on then if not changed", async () => { - const cacheLocation = getFixturePath(".eslintcache"); + it("when `cacheLocation` is specified, should create the cache file with `cache:true` and then delete it with `cache:false`", async () => { + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + const eslintOptions = { useEslintrc: false, // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2576,8 +2633,6 @@ describe("ESLint", () => { cwd: path.join(fixtureDir, "..") }; - assert(!shell.test("-f", cacheLocation), "the cache for eslint does not exist"); - eslint = new ESLint(eslintOptions); let file = getFixturePath("cache/src", "test-file.js"); @@ -2586,20 +2641,20 @@ describe("ESLint", () => { await eslint.lintFiles([file]); - assert(shell.test("-f", cacheLocation), "the cache for eslint was created"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created"); eslintOptions.cache = false; eslint = new ESLint(eslintOptions); await eslint.lintFiles([file]); - assert(!shell.test("-f", cacheLocation), "the cache for eslint was deleted since last run did not used the cache"); + assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted since last run did not use the cache"); }); - it("should store in the cache a file that failed the test", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - assert(!shell.test("-f", cacheLocation), "the cache for eslint does not exist"); + it("should store in the cache a file that has lint messages and a file that doesn't have lint messages", async () => { + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2607,7 +2662,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2619,22 +2674,27 @@ describe("ESLint", () => { const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js")); const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js")); const result = await eslint.lintFiles([badFile, goodFile]); + const [badFileResult, goodFileResult] = result; + + assert.notStrictEqual(badFileResult.errorCount + badFileResult.warningCount, 0, "the bad file should have some lint errors or warnings"); + assert.strictEqual(goodFileResult.errorCount + badFileResult.warningCount, 0, "the good file should have passed linting without errors or warnings"); - assert(shell.test("-f", cacheLocation), "the cache for eslint was created"); - const fileCache = fCache.createFromFile(cacheLocation); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created"); + + const fileCache = fCache.createFromFile(cacheFilePath); const { cache } = fileCache; - assert.strictEqual(typeof cache.getKey(goodFile), "object", "the entry for the good file is in the cache"); - assert.strictEqual(typeof cache.getKey(badFile), "object", "the entry for the bad file is in the cache"); + assert.strictEqual(typeof cache.getKey(goodFile), "object", "the entry for the good file should have been in the cache"); + assert.strictEqual(typeof cache.getKey(badFile), "object", "the entry for the bad file should have been in the cache"); const cachedResult = await eslint.lintFiles([badFile, goodFile]); - assert.deepStrictEqual(result, cachedResult, "result is the same with or without cache"); + assert.deepStrictEqual(result, cachedResult, "result should be the same with or without cache"); }); it("should not contain in the cache a file that was deleted", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - doDelete(cacheLocation); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2642,7 +2702,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2656,10 +2716,10 @@ describe("ESLint", () => { const toBeDeletedFile = fs.realpathSync(getFixturePath("cache/src", "file-to-delete.js")); await eslint.lintFiles([badFile, goodFile, toBeDeletedFile]); - const fileCache = fCache.createFromFile(cacheLocation); + const fileCache = fCache.createFromFile(cacheFilePath); let { cache } = fileCache; - assert.strictEqual(typeof cache.getKey(toBeDeletedFile), "object", "the entry for the file to be deleted is in the cache"); + assert.strictEqual(typeof cache.getKey(toBeDeletedFile), "object", "the entry for the file to be deleted should have been in the cache"); // delete the file from the file system fs.unlinkSync(toBeDeletedFile); @@ -2670,15 +2730,19 @@ describe("ESLint", () => { */ await eslint.lintFiles([badFile, goodFile]); - cache = JSON.parse(fs.readFileSync(cacheLocation)); + cache = JSON.parse(fs.readFileSync(cacheFilePath)); - assert.strictEqual(typeof cache[toBeDeletedFile], "undefined", "the entry for the file to be deleted is not in the cache"); + assert.strictEqual(typeof cache[0][toBeDeletedFile], "undefined", "the entry for the file to be deleted should not have been in the cache"); + + // make sure that the previos assertion checks the right place + assert.notStrictEqual(typeof cache[0][badFile], "undefined", "the entry for the bad file should have been in the cache"); + assert.notStrictEqual(typeof cache[0][goodFile], "undefined", "the entry for the good file should have been in the cache"); }); it("should contain files that were not visited in the cache provided they still exist", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - doDelete(cacheLocation); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2686,7 +2750,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2701,10 +2765,10 @@ describe("ESLint", () => { await eslint.lintFiles([badFile, goodFile, testFile2]); - let fileCache = fCache.createFromFile(cacheLocation); + let fileCache = fCache.createFromFile(cacheFilePath); let { cache } = fileCache; - assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache"); + assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 should have been in the cache"); /* * we pass a different set of files minus test-file2 @@ -2713,19 +2777,23 @@ describe("ESLint", () => { */ await eslint.lintFiles([badFile, goodFile]); - fileCache = fCache.createFromFile(cacheLocation); + fileCache = fCache.createFromFile(cacheFilePath); cache = fileCache.cache; - assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache"); + assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 should have been in the cache"); }); it("should not delete cache when executing on text", async () => { - const cacheLocation = getFixturePath(".eslintcache"); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + + fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), useEslintrc: false, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2735,20 +2803,24 @@ describe("ESLint", () => { extensions: ["js"] }); - assert(shell.test("-f", cacheLocation), "the cache for eslint exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist"); await eslint.lintText("var foo = 'bar';"); - assert(shell.test("-f", cacheLocation), "the cache for eslint still exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist"); }); it("should not delete cache when executing on text with a provided filename", async () => { - const cacheLocation = getFixturePath(".eslintcache"); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + + fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), useEslintrc: false, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2758,21 +2830,25 @@ describe("ESLint", () => { extensions: ["js"] }); - assert(shell.test("-f", cacheLocation), "the cache for eslint exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist"); await eslint.lintText("var bar = foo;", { filePath: "fixtures/passing.js" }); - assert(shell.test("-f", cacheLocation), "the cache for eslint still exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist"); }); it("should not delete cache when executing on files with --cache flag", async () => { - const cacheLocation = getFixturePath(".eslintcache"); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + + fs.writeFileSync(cacheFilePath, ""); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), useEslintrc: false, cache: true, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2783,20 +2859,24 @@ describe("ESLint", () => { }); const file = getFixturePath("cli-engine", "console.js"); - assert(shell.test("-f", cacheLocation), "the cache for eslint exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist"); await eslint.lintFiles([file]); - assert(shell.test("-f", cacheLocation), "the cache for eslint still exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist"); }); it("should delete cache when executing on files without --cache flag", async () => { - const cacheLocation = getFixturePath(".eslintcache"); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); + + fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), useEslintrc: false, - cacheLocation, + cacheLocation: cacheFilePath, overrideConfig: { rules: { "no-console": 0, @@ -2807,58 +2887,57 @@ describe("ESLint", () => { }); const file = getFixturePath("cli-engine", "console.js"); - assert(shell.test("-f", cacheLocation), "the cache for eslint exists"); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist"); await eslint.lintFiles([file]); - assert(!shell.test("-f", cacheLocation), "the cache for eslint has been deleted"); + assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted"); }); - describe("cacheFile", () => { - it("should use the specified cache file", async () => { - const customCacheFile = path.resolve(".cache/custom-cache"); + it("should use the specified cache file", async () => { + cacheFilePath = path.resolve(".cache/custom-cache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); - assert(!shell.test("-f", customCacheFile), "the cache for eslint does not exist"); + eslint = new ESLint({ + useEslintrc: false, - eslint = new ESLint({ - useEslintrc: false, + // specify a custom cache file + cacheLocation: cacheFilePath, - // specify a custom cache file - cacheLocation: customCacheFile, + // specifying cache true the cache will be created + cache: true, + overrideConfig: { + rules: { + "no-console": 0, + "no-unused-vars": 2 + } + }, + extensions: ["js"], + cwd: path.join(fixtureDir, "..") + }); + const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js")); + const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js")); + const result = await eslint.lintFiles([badFile, goodFile]); - // specifying cache true the cache will be created - cache: true, - overrideConfig: { - rules: { - "no-console": 0, - "no-unused-vars": 2 - } - }, - extensions: ["js"], - cwd: path.join(fixtureDir, "..") - }); - const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js")); - const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js")); - const result = await eslint.lintFiles([badFile, goodFile]); + assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created"); - assert(shell.test("-f", customCacheFile), "the cache for eslint was created"); - const fileCache = fCache.createFromFile(customCacheFile); - const { cache } = fileCache; + const fileCache = fCache.createFromFile(cacheFilePath); + const { cache } = fileCache; - assert(typeof cache.getKey(goodFile) === "object", "the entry for the good file is in the cache"); + assert(typeof cache.getKey(goodFile) === "object", "the entry for the good file should have been in the cache"); + assert(typeof cache.getKey(badFile) === "object", "the entry for the bad file should have been in the cache"); - assert(typeof cache.getKey(badFile) === "object", "the entry for the bad file is in the cache"); - const cachedResult = await eslint.lintFiles([badFile, goodFile]); + const cachedResult = await eslint.lintFiles([badFile, goodFile]); - assert.deepStrictEqual(result, cachedResult, "result is the same with or without cache"); - }); + assert.deepStrictEqual(result, cachedResult, "result should be the same with or without cache"); }); describe("cacheStrategy", () => { it("should detect changes using a file's modification time when set to 'metadata'", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - doDelete(cacheLocation); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2866,7 +2945,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, cacheStrategy: "metadata", overrideConfig: { rules: { @@ -2880,24 +2959,24 @@ describe("ESLint", () => { const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js")); await eslint.lintFiles([badFile, goodFile]); - let fileCache = fCache.createFromFile(cacheLocation); + let fileCache = fCache.createFromFile(cacheFilePath); const entries = fileCache.normalizeEntries([badFile, goodFile]); entries.forEach(entry => { - assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`); + assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`); }); // this should result in a changed entry shell.touch(goodFile); - fileCache = fCache.createFromFile(cacheLocation); - assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`); - assert(fileCache.getFileDescriptor(goodFile).changed === true, `the entry for ${goodFile} is changed`); + fileCache = fCache.createFromFile(cacheFilePath); + assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} should have been unchanged`); + assert(fileCache.getFileDescriptor(goodFile).changed === true, `the entry for ${goodFile} should have been changed`); }); it("should not detect changes using a file's modification time when set to 'content'", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - doDelete(cacheLocation); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2905,7 +2984,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, cacheStrategy: "content", overrideConfig: { rules: { @@ -2919,26 +2998,26 @@ describe("ESLint", () => { const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js")); await eslint.lintFiles([badFile, goodFile]); - let fileCache = fCache.createFromFile(cacheLocation, true); + let fileCache = fCache.createFromFile(cacheFilePath, true); let entries = fileCache.normalizeEntries([badFile, goodFile]); entries.forEach(entry => { - assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`); + assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`); }); // this should NOT result in a changed entry shell.touch(goodFile); - fileCache = fCache.createFromFile(cacheLocation, true); + fileCache = fCache.createFromFile(cacheFilePath, true); entries = fileCache.normalizeEntries([badFile, goodFile]); entries.forEach(entry => { - assert(entry.changed === false, `the entry for ${entry.key} remains unchanged`); + assert(entry.changed === false, `the entry for ${entry.key} should have remained unchanged`); }); }); it("should detect changes using a file's contents when set to 'content'", async () => { - const cacheLocation = getFixturePath(".eslintcache"); - - doDelete(cacheLocation); + cacheFilePath = getFixturePath(".eslintcache"); + doDelete(cacheFilePath); + assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted"); eslint = new ESLint({ cwd: path.join(fixtureDir, ".."), @@ -2946,7 +3025,7 @@ describe("ESLint", () => { // specifying cache true the cache will be created cache: true, - cacheLocation, + cacheLocation: cacheFilePath, cacheStrategy: "content", overrideConfig: { rules: { @@ -2963,18 +3042,18 @@ describe("ESLint", () => { shell.cp(goodFile, goodFileCopy); await eslint.lintFiles([badFile, goodFileCopy]); - let fileCache = fCache.createFromFile(cacheLocation, true); + let fileCache = fCache.createFromFile(cacheFilePath, true); const entries = fileCache.normalizeEntries([badFile, goodFileCopy]); entries.forEach(entry => { - assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`); + assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`); }); // this should result in a changed entry shell.sed("-i", "abc", "xzy", goodFileCopy); - fileCache = fCache.createFromFile(cacheLocation, true); - assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`); - assert(fileCache.getFileDescriptor(goodFileCopy).changed === true, `the entry for ${goodFileCopy} is changed`); + fileCache = fCache.createFromFile(cacheFilePath, true); + assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} should have been unchanged`); + assert(fileCache.getFileDescriptor(goodFileCopy).changed === true, `the entry for ${goodFileCopy} should have been changed`); }); }); }); diff --git a/tests/lib/eslint/flat-eslint.js b/tests/lib/eslint/flat-eslint.js index 8fdb96ca7bc..5cfeca351e7 100644 --- a/tests/lib/eslint/flat-eslint.js +++ b/tests/lib/eslint/flat-eslint.js @@ -2244,7 +2244,8 @@ describe("FlatESLint", () => { * Use `fs.rm(path, { recursive: true })` instead. * When supporting Node.js 14.14.0+, the compatibility condition can be removed for `fs.rmdir`. */ - if (typeof fsp.rm === "function") { + // eslint-disable-next-line n/no-unsupported-features/node-builtins -- just checking if it exists + if (typeof fs.rm === "function") { // eslint-disable-next-line n/no-unsupported-features/node-builtins -- conditionally used fs.rmSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true }); @@ -2296,7 +2297,7 @@ describe("FlatESLint", () => { it("should create the cache file inside existing cacheLocation directory when cacheLocation ends with a slash", async () => { assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted"); - fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/")); + fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"), { recursive: true }); eslint = new FlatESLint({ overrideConfigFile: true, @@ -2323,7 +2324,7 @@ describe("FlatESLint", () => { it("should create the cache file inside existing cacheLocation directory when cacheLocation doesn't end with a path separator", async () => { assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted"); - fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/")); + fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"), { recursive: true }); eslint = new FlatESLint({ overrideConfigFile: true,