Skip to content

Commit f30cefe

Browse files
authoredOct 18, 2023
test: fix FlatESLint tests for caching (#17658)
1 parent ef650cb commit f30cefe

File tree

1 file changed

+228
-159
lines changed

1 file changed

+228
-159
lines changed
 

‎tests/lib/eslint/flat-eslint.js

+228-159
Original file line numberDiff line numberDiff line change
@@ -2215,33 +2215,43 @@ describe("FlatESLint", () => {
22152215
}
22162216
}
22172217

2218-
/**
2219-
* helper method to delete the cache files created during testing
2220-
* @returns {void}
2221-
*/
2222-
function deleteCache() {
2223-
doDelete(path.resolve(".eslintcache"));
2224-
doDelete(path.resolve(".cache/custom-cache"));
2225-
}
2218+
let cacheFilePath;
22262219

22272220
beforeEach(() => {
2228-
deleteCache();
2221+
cacheFilePath = null;
22292222
});
22302223

22312224
afterEach(() => {
22322225
sinon.restore();
2233-
deleteCache();
2226+
if (cacheFilePath) {
2227+
doDelete(cacheFilePath);
2228+
}
22342229
});
22352230

2236-
describe("when the cacheFile is a directory or looks like a directory", () => {
2231+
describe("when cacheLocation is a directory or looks like a directory", () => {
2232+
2233+
const cwd = getFixturePath();
22372234

22382235
/**
2239-
* helper method to delete the cache files created during testing
2236+
* helper method to delete the directory used in testing
22402237
* @returns {void}
22412238
*/
22422239
function deleteCacheDir() {
22432240
try {
2244-
fs.unlinkSync("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory");
2241+
2242+
/*
2243+
* `fs.rmdir(path, { recursive: true })` is deprecated and will be removed.
2244+
* Use `fs.rm(path, { recursive: true })` instead.
2245+
* When supporting Node.js 14.14.0+, the compatibility condition can be removed for `fs.rmdir`.
2246+
*/
2247+
if (typeof fsp.rm === "function") {
2248+
2249+
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- conditionally used
2250+
fs.rmSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true });
2251+
} else {
2252+
fs.rmdirSync(path.resolve(cwd, "tmp/.cacheFileDir/"), { recursive: true, force: true });
2253+
}
2254+
22452255
} catch {
22462256

22472257
/*
@@ -2258,11 +2268,12 @@ describe("FlatESLint", () => {
22582268
deleteCacheDir();
22592269
});
22602270

2261-
it("should create the cache file inside the provided directory", async () => {
2262-
assert(!shell.test("-d", path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist");
2271+
it("should create the directory and the cache file inside it when cacheLocation ends with a slash", async () => {
2272+
assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted");
22632273

22642274
eslint = new FlatESLint({
22652275
overrideConfigFile: true,
2276+
cwd,
22662277

22672278
// specifying cache true the cache will be created
22682279
cache: true,
@@ -2279,41 +2290,71 @@ describe("FlatESLint", () => {
22792290

22802291
await eslint.lintFiles([file]);
22812292

2282-
assert(shell.test("-f", path.resolve(`./tmp/.cacheFileDir/.cache_${hash(process.cwd())}`)), "the cache for eslint was created");
2283-
2284-
sinon.restore();
2293+
assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created");
22852294
});
2286-
});
22872295

2288-
it("should create the cache file inside the provided directory using the cacheLocation option", async () => {
2289-
assert(!shell.test("-d", path.resolve("./tmp/.cacheFileDir/.cache_hashOfCurrentWorkingDirectory")), "the cache for eslint does not exist");
2296+
it("should create the cache file inside existing cacheLocation directory when cacheLocation ends with a slash", async () => {
2297+
assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted");
22902298

2291-
eslint = new FlatESLint({
2292-
overrideConfigFile: true,
2299+
fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"));
22932300

2294-
// specifying cache true the cache will be created
2295-
cache: true,
2296-
cacheLocation: "./tmp/.cacheFileDir/",
2297-
overrideConfig: {
2298-
rules: {
2299-
"no-console": 0,
2300-
"no-unused-vars": 2
2301-
}
2302-
},
2303-
ignore: false
2301+
eslint = new FlatESLint({
2302+
overrideConfigFile: true,
2303+
cwd,
2304+
2305+
// specifying cache true the cache will be created
2306+
cache: true,
2307+
cacheLocation: "./tmp/.cacheFileDir/",
2308+
overrideConfig: {
2309+
rules: {
2310+
"no-console": 0,
2311+
"no-unused-vars": 2
2312+
}
2313+
},
2314+
ignore: false
2315+
});
2316+
const file = getFixturePath("cache/src", "test-file.js");
2317+
2318+
await eslint.lintFiles([file]);
2319+
2320+
assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created");
23042321
});
2305-
const file = getFixturePath("cache/src", "test-file.js");
23062322

2307-
await eslint.lintFiles([file]);
2323+
it("should create the cache file inside existing cacheLocation directory when cacheLocation doesn't end with a path separator", async () => {
2324+
assert(!shell.test("-d", path.resolve(cwd, "./tmp/.cacheFileDir/")), "the cache directory already exists and wasn't successfully deleted");
23082325

2309-
assert(shell.test("-f", path.resolve(`./tmp/.cacheFileDir/.cache_${hash(process.cwd())}`)), "the cache for eslint was created");
2326+
fs.mkdirSync(path.resolve(cwd, "./tmp/.cacheFileDir/"));
23102327

2311-
sinon.restore();
2328+
eslint = new FlatESLint({
2329+
overrideConfigFile: true,
2330+
cwd,
2331+
2332+
// specifying cache true the cache will be created
2333+
cache: true,
2334+
cacheLocation: "./tmp/.cacheFileDir",
2335+
overrideConfig: {
2336+
rules: {
2337+
"no-console": 0,
2338+
"no-unused-vars": 2
2339+
}
2340+
},
2341+
ignore: false
2342+
});
2343+
const file = getFixturePath("cache/src", "test-file.js");
2344+
2345+
await eslint.lintFiles([file]);
2346+
2347+
assert(shell.test("-f", path.resolve(cwd, `./tmp/.cacheFileDir/.cache_${hash(cwd)}`)), "the cache for eslint should have been created");
2348+
});
23122349
});
23132350

23142351
it("should create the cache file inside cwd when no cacheLocation provided", async () => {
23152352
const cwd = path.resolve(getFixturePath("cli-engine"));
23162353

2354+
cacheFilePath = path.resolve(cwd, ".eslintcache");
2355+
doDelete(cacheFilePath);
2356+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2357+
23172358
eslint = new FlatESLint({
23182359
overrideConfigFile: true,
23192360
cache: true,
@@ -2329,14 +2370,15 @@ describe("FlatESLint", () => {
23292370

23302371
await eslint.lintFiles([file]);
23312372

2332-
assert(shell.test("-f", path.resolve(cwd, ".eslintcache")), "the cache for eslint was created at provided cwd");
2373+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created at provided cwd");
23332374
});
23342375

23352376
it("should invalidate the cache if the overrideConfig changed between executions", async () => {
23362377
const cwd = getFixturePath("cache/src");
2337-
const cacheLocation = path.resolve(cwd, ".eslintcache");
23382378

2339-
assert(!shell.test("-f", cacheLocation), "the cache for eslint does not exist");
2379+
cacheFilePath = path.resolve(cwd, ".eslintcache");
2380+
doDelete(cacheFilePath);
2381+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
23402382

23412383
eslint = new FlatESLint({
23422384
overrideConfigFile: true,
@@ -2361,11 +2403,11 @@ describe("FlatESLint", () => {
23612403
const results = await eslint.lintFiles([file]);
23622404

23632405
for (const { errorCount, warningCount } of results) {
2364-
assert.strictEqual(errorCount + warningCount, 0, "the file passed without errors or warnings");
2406+
assert.strictEqual(errorCount + warningCount, 0, "the file should have passed linting without errors or warnings");
23652407
}
23662408

2367-
assert(spy.calledWith(file), "ESLint should have read the file because it's considered changed");
2368-
assert(shell.test("-f", cacheLocation), "the cache for eslint should still exist");
2409+
assert(spy.calledWith(file), "ESLint should have read the file because there was no cache file");
2410+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created");
23692411

23702412
// destroy the spy
23712413
sinon.restore();
@@ -2388,17 +2430,20 @@ describe("FlatESLint", () => {
23882430
// create a new spy
23892431
spy = sinon.spy(fs.promises, "readFile");
23902432

2391-
const [cachedResult] = await eslint.lintFiles([file]);
2433+
const [newResult] = await eslint.lintFiles([file]);
23922434

2393-
assert(spy.calledWith(file), "ESLint should have read the file again because is considered changed because the config changed");
2394-
assert.strictEqual(cachedResult.errorCount, 1, "since configuration changed the cache was not used and one error was reported");
2395-
assert(shell.test("-f", cacheLocation), "The cache for ESLint should still exist (2)");
2435+
assert(spy.calledWith(file), "ESLint should have read the file again because it's considered changed because the config changed");
2436+
assert.strictEqual(newResult.errorCount, 1, "since configuration changed the cache should have not been used and one error should have been reported");
2437+
assert.strictEqual(newResult.messages[0].ruleId, "no-console");
2438+
assert(shell.test("-f", cacheFilePath), "The cache for ESLint should still exist");
23962439
});
23972440

23982441
it("should remember the files from a previous run and do not operate on them if not changed", async () => {
2399-
24002442
const cwd = getFixturePath("cache/src");
2401-
const cacheLocation = path.resolve(cwd, ".eslintcache");
2443+
2444+
cacheFilePath = path.resolve(cwd, ".eslintcache");
2445+
doDelete(cacheFilePath);
2446+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
24022447

24032448
eslint = new FlatESLint({
24042449
overrideConfigFile: true,
@@ -2423,8 +2468,8 @@ describe("FlatESLint", () => {
24232468

24242469
const result = await eslint.lintFiles([file]);
24252470

2426-
assert(spy.calledWith(file), "the module read the file because is considered changed");
2427-
assert(shell.test("-f", cacheLocation), "the cache for eslint was created");
2471+
assert(spy.calledWith(file), "ESLint should have read the file because there was no cache file");
2472+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created");
24282473

24292474
// destroy the spy
24302475
sinon.restore();
@@ -2449,20 +2494,23 @@ describe("FlatESLint", () => {
24492494

24502495
const cachedResult = await eslint.lintFiles([file]);
24512496

2452-
assert.deepStrictEqual(result, cachedResult, "the result is the same regardless of using cache or not");
2497+
assert.deepStrictEqual(result, cachedResult, "the result should have been the same");
24532498

24542499
// assert the file was not processed because the cache was used
2455-
assert(!spy.calledWith(file), "the file was not loaded because it used the cache");
2500+
assert(!spy.calledWith(file), "the file should not have been reloaded");
24562501
});
24572502

2458-
it("should remember the files from a previous run and do not operate on then if not changed", async () => {
2459-
const cacheLocation = getFixturePath(".eslintcache");
2503+
it("when `cacheLocation` is specified, should create the cache file with `cache:true` and then delete it with `cache:false`", async () => {
2504+
cacheFilePath = getFixturePath(".eslintcache");
2505+
doDelete(cacheFilePath);
2506+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2507+
24602508
const eslintOptions = {
24612509
overrideConfigFile: true,
24622510

24632511
// specifying cache true the cache will be created
24642512
cache: true,
2465-
cacheLocation,
2513+
cacheLocation: cacheFilePath,
24662514
overrideConfig: {
24672515
rules: {
24682516
"no-console": 0,
@@ -2472,8 +2520,6 @@ describe("FlatESLint", () => {
24722520
cwd: path.join(fixtureDir, "..")
24732521
};
24742522

2475-
assert(!shell.test("-f", cacheLocation), "the cache for eslint does not exist");
2476-
24772523
eslint = new FlatESLint(eslintOptions);
24782524

24792525
let file = getFixturePath("cache/src", "test-file.js");
@@ -2482,28 +2528,28 @@ describe("FlatESLint", () => {
24822528

24832529
await eslint.lintFiles([file]);
24842530

2485-
assert(shell.test("-f", cacheLocation), "the cache for eslint was created");
2531+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created");
24862532

24872533
eslintOptions.cache = false;
24882534
eslint = new FlatESLint(eslintOptions);
24892535

24902536
await eslint.lintFiles([file]);
24912537

2492-
assert(!shell.test("-f", cacheLocation), "the cache for eslint was deleted since last run did not used the cache");
2538+
assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted since last run did not use the cache");
24932539
});
24942540

2495-
it("should store in the cache a file that failed the test", async () => {
2496-
const cacheLocation = getFixturePath(".eslintcache");
2497-
2498-
assert(!shell.test("-f", cacheLocation), "the cache for eslint does not exist");
2541+
it("should store in the cache a file that has lint messages and a file that doesn't have lint messages", async () => {
2542+
cacheFilePath = getFixturePath(".eslintcache");
2543+
doDelete(cacheFilePath);
2544+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
24992545

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

25042550
// specifying cache true the cache will be created
25052551
cache: true,
2506-
cacheLocation,
2552+
cacheLocation: cacheFilePath,
25072553
overrideConfig: {
25082554
rules: {
25092555
"no-console": 0,
@@ -2514,30 +2560,35 @@ describe("FlatESLint", () => {
25142560
const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js"));
25152561
const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
25162562
const result = await eslint.lintFiles([badFile, goodFile]);
2563+
const [badFileResult, goodFileResult] = result;
2564+
2565+
assert.notStrictEqual(badFileResult.errorCount + badFileResult.warningCount, 0, "the bad file should have some lint errors or warnings");
2566+
assert.strictEqual(goodFileResult.errorCount + badFileResult.warningCount, 0, "the good file should have passed linting without errors or warnings");
2567+
2568+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created");
25172569

2518-
assert(shell.test("-f", cacheLocation), "the cache for eslint was created");
2519-
const fileCache = fCache.createFromFile(cacheLocation);
2570+
const fileCache = fCache.createFromFile(cacheFilePath);
25202571
const { cache } = fileCache;
25212572

2522-
assert.strictEqual(typeof cache.getKey(goodFile), "object", "the entry for the good file is in the cache");
2523-
assert.strictEqual(typeof cache.getKey(badFile), "object", "the entry for the bad file is in the cache");
2573+
assert.strictEqual(typeof cache.getKey(goodFile), "object", "the entry for the good file should have been in the cache");
2574+
assert.strictEqual(typeof cache.getKey(badFile), "object", "the entry for the bad file should have been in the cache");
25242575
const cachedResult = await eslint.lintFiles([badFile, goodFile]);
25252576

2526-
assert.deepStrictEqual(result, cachedResult, "result is the same with or without cache");
2577+
assert.deepStrictEqual(result, cachedResult, "result should be the same with or without cache");
25272578
});
25282579

25292580
it("should not contain in the cache a file that was deleted", async () => {
2530-
const cacheLocation = getFixturePath(".eslintcache");
2531-
2532-
doDelete(cacheLocation);
2581+
cacheFilePath = getFixturePath(".eslintcache");
2582+
doDelete(cacheFilePath);
2583+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
25332584

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

25382589
// specifying cache true the cache will be created
25392590
cache: true,
2540-
cacheLocation,
2591+
cacheLocation: cacheFilePath,
25412592
overrideConfig: {
25422593
rules: {
25432594
"no-console": 0,
@@ -2550,10 +2601,10 @@ describe("FlatESLint", () => {
25502601
const toBeDeletedFile = fs.realpathSync(getFixturePath("cache/src", "file-to-delete.js"));
25512602

25522603
await eslint.lintFiles([badFile, goodFile, toBeDeletedFile]);
2553-
const fileCache = fCache.createFromFile(cacheLocation);
2604+
const fileCache = fCache.createFromFile(cacheFilePath);
25542605
let { cache } = fileCache;
25552606

2556-
assert.strictEqual(typeof cache.getKey(toBeDeletedFile), "object", "the entry for the file to be deleted is in the cache");
2607+
assert.strictEqual(typeof cache.getKey(toBeDeletedFile), "object", "the entry for the file to be deleted should have been in the cache");
25572608

25582609
// delete the file from the file system
25592610
fs.unlinkSync(toBeDeletedFile);
@@ -2564,23 +2615,27 @@ describe("FlatESLint", () => {
25642615
*/
25652616
await eslint.lintFiles([badFile, goodFile]);
25662617

2567-
cache = JSON.parse(fs.readFileSync(cacheLocation));
2618+
cache = JSON.parse(fs.readFileSync(cacheFilePath));
2619+
2620+
assert.strictEqual(typeof cache[0][toBeDeletedFile], "undefined", "the entry for the file to be deleted should not have been in the cache");
25682621

2569-
assert.strictEqual(typeof cache[toBeDeletedFile], "undefined", "the entry for the file to be deleted is not in the cache");
2622+
// make sure that the previos assertion checks the right place
2623+
assert.notStrictEqual(typeof cache[0][badFile], "undefined", "the entry for the bad file should have been in the cache");
2624+
assert.notStrictEqual(typeof cache[0][goodFile], "undefined", "the entry for the good file should have been in the cache");
25702625
});
25712626

25722627
it("should contain files that were not visited in the cache provided they still exist", async () => {
2573-
const cacheLocation = getFixturePath(".eslintcache");
2574-
2575-
doDelete(cacheLocation);
2628+
cacheFilePath = getFixturePath(".eslintcache");
2629+
doDelete(cacheFilePath);
2630+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
25762631

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

25812636
// specifying cache true the cache will be created
25822637
cache: true,
2583-
cacheLocation,
2638+
cacheLocation: cacheFilePath,
25842639
overrideConfig: {
25852640
rules: {
25862641
"no-console": 0,
@@ -2594,31 +2649,35 @@ describe("FlatESLint", () => {
25942649

25952650
await eslint.lintFiles([badFile, goodFile, testFile2]);
25962651

2597-
let fileCache = fCache.createFromFile(cacheLocation);
2652+
let fileCache = fCache.createFromFile(cacheFilePath);
25982653
let { cache } = fileCache;
25992654

2600-
assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache");
2655+
assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 should have been in the cache");
26012656

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

2609-
fileCache = fCache.createFromFile(cacheLocation);
2664+
fileCache = fCache.createFromFile(cacheFilePath);
26102665
cache = fileCache.cache;
26112666

2612-
assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 is in the cache");
2667+
assert.strictEqual(typeof cache.getKey(testFile2), "object", "the entry for the test-file2 should have been in the cache");
26132668
});
26142669

26152670
it("should not delete cache when executing on text", async () => {
2616-
const cacheLocation = getFixturePath(".eslintcache");
2671+
cacheFilePath = getFixturePath(".eslintcache");
2672+
doDelete(cacheFilePath);
2673+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2674+
2675+
fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used
26172676

26182677
eslint = new FlatESLint({
26192678
cwd: path.join(fixtureDir, ".."),
26202679
overrideConfigFile: true,
2621-
cacheLocation,
2680+
cacheLocation: cacheFilePath,
26222681
overrideConfig: {
26232682
rules: {
26242683
"no-console": 0,
@@ -2627,20 +2686,24 @@ describe("FlatESLint", () => {
26272686
}
26282687
});
26292688

2630-
assert(shell.test("-f", cacheLocation), "the cache for eslint exists");
2689+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist");
26312690

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

2634-
assert(shell.test("-f", cacheLocation), "the cache for eslint still exists");
2693+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist");
26352694
});
26362695

26372696
it("should not delete cache when executing on text with a provided filename", async () => {
2638-
const cacheLocation = getFixturePath(".eslintcache");
2697+
cacheFilePath = getFixturePath(".eslintcache");
2698+
doDelete(cacheFilePath);
2699+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2700+
2701+
fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used
26392702

26402703
eslint = new FlatESLint({
26412704
cwd: path.join(fixtureDir, ".."),
26422705
overrideConfigFile: true,
2643-
cacheLocation,
2706+
cacheLocation: cacheFilePath,
26442707
overrideConfig: {
26452708
rules: {
26462709
"no-console": 0,
@@ -2649,21 +2712,25 @@ describe("FlatESLint", () => {
26492712
}
26502713
});
26512714

2652-
assert(shell.test("-f", cacheLocation), "the cache for eslint exists");
2715+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist");
26532716

26542717
await eslint.lintText("var bar = foo;", { filePath: "fixtures/passing.js" });
26552718

2656-
assert(shell.test("-f", cacheLocation), "the cache for eslint still exists");
2719+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist");
26572720
});
26582721

26592722
it("should not delete cache when executing on files with --cache flag", async () => {
2660-
const cacheLocation = getFixturePath(".eslintcache");
2723+
cacheFilePath = getFixturePath(".eslintcache");
2724+
doDelete(cacheFilePath);
2725+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2726+
2727+
fs.writeFileSync(cacheFilePath, "");
26612728

26622729
eslint = new FlatESLint({
26632730
cwd: path.join(fixtureDir, ".."),
26642731
overrideConfigFile: true,
26652732
cache: true,
2666-
cacheLocation,
2733+
cacheLocation: cacheFilePath,
26672734
overrideConfig: {
26682735
rules: {
26692736
"no-console": 0,
@@ -2673,90 +2740,92 @@ describe("FlatESLint", () => {
26732740
});
26742741
const file = getFixturePath("cli-engine", "console.js");
26752742

2676-
assert(shell.test("-f", cacheLocation), "the cache for eslint exists");
2743+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist");
26772744

26782745
await eslint.lintFiles([file]);
26792746

2680-
assert(shell.test("-f", cacheLocation), "the cache for eslint still exists");
2747+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should still exist");
26812748
});
26822749

26832750
it("should delete cache when executing on files without --cache flag", async () => {
2684-
const cacheLocation = getFixturePath(".eslintcache");
2751+
cacheFilePath = getFixturePath(".eslintcache");
2752+
doDelete(cacheFilePath);
2753+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
2754+
2755+
fs.writeFileSync(cacheFilePath, "[]"); // intenationally invalid to additionally make sure it isn't used
26852756

26862757
eslint = new FlatESLint({
26872758
cwd: path.join(fixtureDir, ".."),
26882759
overrideConfigFile: true,
2689-
cacheLocation,
2760+
cacheLocation: cacheFilePath,
26902761
overrideConfig: {
26912762
rules: {
26922763
"no-console": 0,
26932764
"no-unused-vars": 2
26942765
}
26952766
}
2696-
26972767
});
26982768
const file = getFixturePath("cli-engine", "console.js");
26992769

2700-
assert(shell.test("-f", cacheLocation), "the cache for eslint exists");
2770+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should exist");
27012771

27022772
await eslint.lintFiles([file]);
27032773

2704-
assert(!shell.test("-f", cacheLocation), "the cache for eslint has been deleted");
2774+
assert(!shell.test("-f", cacheFilePath), "the cache for eslint should have been deleted");
27052775
});
27062776

2707-
describe("cacheFile", () => {
2708-
it("should use the specified cache file", async () => {
2709-
const customCacheFile = path.resolve(".cache/custom-cache");
2777+
it("should use the specified cache file", async () => {
2778+
cacheFilePath = path.resolve(".cache/custom-cache");
2779+
doDelete(cacheFilePath);
2780+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
27102781

2711-
assert(!shell.test("-f", customCacheFile), "the cache for eslint does not exist");
2782+
eslint = new FlatESLint({
2783+
overrideConfigFile: true,
27122784

2713-
eslint = new FlatESLint({
2714-
overrideConfigFile: true,
2785+
// specify a custom cache file
2786+
cacheLocation: cacheFilePath,
27152787

2716-
// specify a custom cache file
2717-
cacheLocation: customCacheFile,
2788+
// specifying cache true the cache will be created
2789+
cache: true,
2790+
overrideConfig: {
2791+
rules: {
2792+
"no-console": 0,
2793+
"no-unused-vars": 2
2794+
}
2795+
},
27182796

2719-
// specifying cache true the cache will be created
2720-
cache: true,
2721-
overrideConfig: {
2722-
rules: {
2723-
"no-console": 0,
2724-
"no-unused-vars": 2
2725-
}
2726-
},
2797+
cwd: path.join(fixtureDir, "..")
2798+
});
2799+
const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js"));
2800+
const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
2801+
const result = await eslint.lintFiles([badFile, goodFile]);
27272802

2728-
cwd: path.join(fixtureDir, "..")
2729-
});
2730-
const badFile = fs.realpathSync(getFixturePath("cache/src", "fail-file.js"));
2731-
const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
2732-
const result = await eslint.lintFiles([badFile, goodFile]);
2803+
assert(shell.test("-f", cacheFilePath), "the cache for eslint should have been created");
27332804

2734-
assert(shell.test("-f", customCacheFile), "the cache for eslint was created");
2735-
const fileCache = fCache.createFromFile(customCacheFile);
2736-
const { cache } = fileCache;
2805+
const fileCache = fCache.createFromFile(cacheFilePath);
2806+
const { cache } = fileCache;
27372807

2738-
assert(typeof cache.getKey(goodFile) === "object", "the entry for the good file is in the cache");
2808+
assert(typeof cache.getKey(goodFile) === "object", "the entry for the good file should have been in the cache");
2809+
assert(typeof cache.getKey(badFile) === "object", "the entry for the bad file should have been in the cache");
27392810

2740-
assert(typeof cache.getKey(badFile) === "object", "the entry for the bad file is in the cache");
2741-
const cachedResult = await eslint.lintFiles([badFile, goodFile]);
2811+
const cachedResult = await eslint.lintFiles([badFile, goodFile]);
27422812

2743-
assert.deepStrictEqual(result, cachedResult, "result is the same with or without cache");
2744-
});
2813+
assert.deepStrictEqual(result, cachedResult, "result should be the same with or without cache");
27452814
});
27462815

27472816
describe("cacheStrategy", () => {
27482817
it("should detect changes using a file's modification time when set to 'metadata'", async () => {
2749-
const cacheLocation = getFixturePath(".eslintcache");
2750-
2751-
doDelete(cacheLocation);
2818+
cacheFilePath = getFixturePath(".eslintcache");
2819+
doDelete(cacheFilePath);
2820+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
27522821

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

27572826
// specifying cache true the cache will be created
27582827
cache: true,
2759-
cacheLocation,
2828+
cacheLocation: cacheFilePath,
27602829
cacheStrategy: "metadata",
27612830
overrideConfig: {
27622831
rules: {
@@ -2770,32 +2839,32 @@ describe("FlatESLint", () => {
27702839
const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
27712840

27722841
await eslint.lintFiles([badFile, goodFile]);
2773-
let fileCache = fCache.createFromFile(cacheLocation);
2842+
let fileCache = fCache.createFromFile(cacheFilePath);
27742843
const entries = fileCache.normalizeEntries([badFile, goodFile]);
27752844

27762845
entries.forEach(entry => {
2777-
assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`);
2846+
assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`);
27782847
});
27792848

27802849
// this should result in a changed entry
27812850
shell.touch(goodFile);
2782-
fileCache = fCache.createFromFile(cacheLocation);
2783-
assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`);
2784-
assert(fileCache.getFileDescriptor(goodFile).changed === true, `the entry for ${goodFile} is changed`);
2851+
fileCache = fCache.createFromFile(cacheFilePath);
2852+
assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} should have been unchanged`);
2853+
assert(fileCache.getFileDescriptor(goodFile).changed === true, `the entry for ${goodFile} should have been changed`);
27852854
});
27862855

27872856
it("should not detect changes using a file's modification time when set to 'content'", async () => {
2788-
const cacheLocation = getFixturePath(".eslintcache");
2789-
2790-
doDelete(cacheLocation);
2857+
cacheFilePath = getFixturePath(".eslintcache");
2858+
doDelete(cacheFilePath);
2859+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
27912860

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

27962865
// specifying cache true the cache will be created
27972866
cache: true,
2798-
cacheLocation,
2867+
cacheLocation: cacheFilePath,
27992868
cacheStrategy: "content",
28002869
overrideConfig: {
28012870
rules: {
@@ -2809,34 +2878,34 @@ describe("FlatESLint", () => {
28092878
const goodFile = fs.realpathSync(getFixturePath("cache/src", "test-file.js"));
28102879

28112880
await eslint.lintFiles([badFile, goodFile]);
2812-
let fileCache = fCache.createFromFile(cacheLocation, true);
2881+
let fileCache = fCache.createFromFile(cacheFilePath, true);
28132882
let entries = fileCache.normalizeEntries([badFile, goodFile]);
28142883

28152884
entries.forEach(entry => {
2816-
assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`);
2885+
assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`);
28172886
});
28182887

28192888
// this should NOT result in a changed entry
28202889
shell.touch(goodFile);
2821-
fileCache = fCache.createFromFile(cacheLocation, true);
2890+
fileCache = fCache.createFromFile(cacheFilePath, true);
28222891
entries = fileCache.normalizeEntries([badFile, goodFile]);
28232892
entries.forEach(entry => {
2824-
assert(entry.changed === false, `the entry for ${entry.key} remains unchanged`);
2893+
assert(entry.changed === false, `the entry for ${entry.key} should have remained unchanged`);
28252894
});
28262895
});
28272896

28282897
it("should detect changes using a file's contents when set to 'content'", async () => {
2829-
const cacheLocation = getFixturePath(".eslintcache");
2830-
2831-
doDelete(cacheLocation);
2898+
cacheFilePath = getFixturePath(".eslintcache");
2899+
doDelete(cacheFilePath);
2900+
assert(!shell.test("-f", cacheFilePath), "the cache file already exists and wasn't successfully deleted");
28322901

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

28372906
// specifying cache true the cache will be created
28382907
cache: true,
2839-
cacheLocation,
2908+
cacheLocation: cacheFilePath,
28402909
cacheStrategy: "content",
28412910
overrideConfig: {
28422911
rules: {
@@ -2853,18 +2922,18 @@ describe("FlatESLint", () => {
28532922
shell.cp(goodFile, goodFileCopy);
28542923

28552924
await eslint.lintFiles([badFile, goodFileCopy]);
2856-
let fileCache = fCache.createFromFile(cacheLocation, true);
2925+
let fileCache = fCache.createFromFile(cacheFilePath, true);
28572926
const entries = fileCache.normalizeEntries([badFile, goodFileCopy]);
28582927

28592928
entries.forEach(entry => {
2860-
assert(entry.changed === false, `the entry for ${entry.key} is initially unchanged`);
2929+
assert(entry.changed === false, `the entry for ${entry.key} should have been initially unchanged`);
28612930
});
28622931

28632932
// this should result in a changed entry
28642933
shell.sed("-i", "abc", "xzy", goodFileCopy);
2865-
fileCache = fCache.createFromFile(cacheLocation, true);
2866-
assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} is unchanged`);
2867-
assert(fileCache.getFileDescriptor(goodFileCopy).changed === true, `the entry for ${goodFileCopy} is changed`);
2934+
fileCache = fCache.createFromFile(cacheFilePath, true);
2935+
assert(fileCache.getFileDescriptor(badFile).changed === false, `the entry for ${badFile} should have been unchanged`);
2936+
assert(fileCache.getFileDescriptor(goodFileCopy).changed === true, `the entry for ${goodFileCopy} should have been changed`);
28682937
});
28692938
});
28702939
});

0 commit comments

Comments
 (0)
Please sign in to comment.