Repository: eslint/eslint
File: tests/lib/eslint/flat-eslint.js (JavaScript)

Code before:

                assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache");

                /*
                 * we pass a different set of files minus test-file2
                 * previous version of file-entry-cache would remove the non visited
                 * entries. 2.0.0 version will keep them unless they don't exist
                 */
                await eslint.lintFiles([badFile, goodFile]);

                fileCache = fCache.createFromFile(cacheLocation);
                cache = fileCache.cache;

                assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache");
            });

            it("should not delete cache when executing on text", async () => {
                const cacheLocation = getFixturePath(".eslintcache");

                eslint = new FlatESLint({
                    cwd: path.join(fixtureDir, ".."),
                    overrideConfigFile: true,
                    cacheLocation,
                    overrideConfig: {
                        rules: {
                            "no-console": 0,
                            "no-unused-vars": 2
                        }
                    }
                });

                assert(shell.test("-f", cacheLocation), "the cache for eslint exists");

                await eslint.lintText("var foo = 'bar';");

                assert(shell.test("-f", cacheLocation), "the cache for eslint still exists");
            });

            it("should not delete cache when executing on text with a provided filename", async () => {
                const cacheLocation = getFixturePath(".eslintcache");

                eslint = new FlatESLint({
                    cwd: path.join(fixtureDir, ".."),
                    overrideConfigFile: true,
                    cacheLocation,
                    overrideConfig: {
                        rules: {
                            "no-console": 0,
                            "no-unused-vars": 2
                        }
                    }

Review comment (performance):
This test wasn't working as intended because `cache[toBeDeletedFile]` is `undefined` even if the file wasn't deleted.

Code after:
                const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js"));
                const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
                const toBeDeletedFile = fs.realpathSync(getFixturePath("cache/src", "file-to-delete.js"));

                await eslint.lintFiles([badFile, goodFile, toBeDeletedFile]);
                const fileCache = fCache.createFromFile(cacheFilePath);
                let { cache } = fileCache;

                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);

                /*
                 * file-entry-cache@2.0.0 will remove from the cache deleted files
                 * even when they were not part of the array of files to be analyzed
                 */
                await eslint.lintFiles([badFile, goodFile]);

                cache = JSON.parse(fs.readFileSync(cacheFilePath));

                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 () => {
                cacheFilePath = getFixturePath(".eslintcache");
                doDelete(cacheFilePath);
                assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");

                eslint = new FlatESLint({
                    cwd: path.join(fixtureDir, ".."),
                    overrideConfigFile: true,

                    // specifying cache true the cache will be created
                    cache: true,
                    cacheLocation: cacheFilePath,
                    overrideConfig: {
                        rules: {
                            "no-console": 0,
                            "no-unused-vars": 2
                        }
                    }
                });
                const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js"));
                const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
                const testFile2 = fs.realpathSync(getFixturePath("cache/src", "test-file2.js"));