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

Code before:
                    }
                }
                beforeEach(() => {
                    deleteCacheDir();
                });

                afterEach(() => {
                    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");

                    eslint = new FlatESLint({
                        overrideConfigFile: true,

                        // 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(`./tmp/.cacheFileDir/.cache_${hash(process.cwd())}`)), "the cache for eslint was created");

                    sinon.restore();
                });
            });

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

                eslint = new FlatESLint({
                    overrideConfigFile: true,

                    // specifying cache true the cache will be created
                    cache: true,
                    cacheLocation: "./tmp/.cacheFileDir/",
                    overrideConfig: {
                        rules: {
                            "no-console": 0,
                            "no-unused-vars": 2
                        }

Review comment (bug):
This test was failing because it lints a file in a temporary directory, which is outside of the default `cwd` (project root) and is therefore considered ignored. Thus, this test wasn't actually creating the cache file. With `npm test`, the assertion was finding a cache file that was left from running `tests/lib/eslint/eslint.js` before.

I fixed this test and added two more tests in this group, so now we'll have 3 tests where `cacheLocation` is a directory:

1. The directory doesn't already exist but `cacheLocation` has a trailing slash so it looks like directory and thus FlatESLint creates the directory and the cache file inside it.
2. The directory already exists and `cacheLocation` has a trailing slash. FlatESLint uses the existing directory and creates the cache file inside it.
2. The directory already exists and `cacheLocation` doesn't have a trailing slash. FlatESLint finds that the path is a directory and therefore creates the cache file inside it. 

Code after:
                        } else {
                            fs.rmdirSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true });
                        }

                    } catch {

                        /*
                         * we don't care if the file didn't exist, since our
                         * intention was to remove the file
                         */
                    }
                }
                beforeEach(() => {
                    deleteCacheDir();
                });

                afterEach(() => {
                    deleteCacheDir();
                });

                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 FlatESLint({
                        overrideConfigFile: true,
                        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 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/"));

                    eslint = new FlatESLint({