@@ -2215,33 +2215,43 @@ describe("FlatESLint", () => {
2215
2215
}
2216
2216
}
2217
2217
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 ;
2226
2219
2227
2220
beforeEach ( ( ) => {
2228
- deleteCache ( ) ;
2221
+ cacheFilePath = null ;
2229
2222
} ) ;
2230
2223
2231
2224
afterEach ( ( ) => {
2232
2225
sinon . restore ( ) ;
2233
- deleteCache ( ) ;
2226
+ if ( cacheFilePath ) {
2227
+ doDelete ( cacheFilePath ) ;
2228
+ }
2234
2229
} ) ;
2235
2230
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 ( ) ;
2237
2234
2238
2235
/**
2239
- * helper method to delete the cache files created during testing
2236
+ * helper method to delete the directory used in testing
2240
2237
* @returns {void }
2241
2238
*/
2242
2239
function deleteCacheDir ( ) {
2243
2240
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
+
2245
2255
} catch {
2246
2256
2247
2257
/*
@@ -2258,11 +2268,12 @@ describe("FlatESLint", () => {
2258
2268
deleteCacheDir ( ) ;
2259
2269
} ) ;
2260
2270
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 " ) ;
2263
2273
2264
2274
eslint = new FlatESLint ( {
2265
2275
overrideConfigFile : true ,
2276
+ cwd,
2266
2277
2267
2278
// specifying cache true the cache will be created
2268
2279
cache : true ,
@@ -2279,41 +2290,71 @@ describe("FlatESLint", () => {
2279
2290
2280
2291
await eslint . lintFiles ( [ file ] ) ;
2281
2292
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" ) ;
2285
2294
} ) ;
2286
- } ) ;
2287
2295
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 " ) ;
2290
2298
2291
- eslint = new FlatESLint ( {
2292
- overrideConfigFile : true ,
2299
+ fs . mkdirSync ( path . resolve ( cwd , "./tmp/.cacheFileDir/" ) ) ;
2293
2300
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" ) ;
2304
2321
} ) ;
2305
- const file = getFixturePath ( "cache/src" , "test-file.js" ) ;
2306
2322
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" ) ;
2308
2325
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/" ) ) ;
2310
2327
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
+ } ) ;
2312
2349
} ) ;
2313
2350
2314
2351
it ( "should create the cache file inside cwd when no cacheLocation provided" , async ( ) => {
2315
2352
const cwd = path . resolve ( getFixturePath ( "cli-engine" ) ) ;
2316
2353
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
+
2317
2358
eslint = new FlatESLint ( {
2318
2359
overrideConfigFile : true ,
2319
2360
cache : true ,
@@ -2329,14 +2370,15 @@ describe("FlatESLint", () => {
2329
2370
2330
2371
await eslint . lintFiles ( [ file ] ) ;
2331
2372
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" ) ;
2333
2374
} ) ;
2334
2375
2335
2376
it ( "should invalidate the cache if the overrideConfig changed between executions" , async ( ) => {
2336
2377
const cwd = getFixturePath ( "cache/src" ) ;
2337
- const cacheLocation = path . resolve ( cwd , ".eslintcache" ) ;
2338
2378
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" ) ;
2340
2382
2341
2383
eslint = new FlatESLint ( {
2342
2384
overrideConfigFile : true ,
@@ -2361,11 +2403,11 @@ describe("FlatESLint", () => {
2361
2403
const results = await eslint . lintFiles ( [ file ] ) ;
2362
2404
2363
2405
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" ) ;
2365
2407
}
2366
2408
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 " ) ;
2369
2411
2370
2412
// destroy the spy
2371
2413
sinon . restore ( ) ;
@@ -2388,17 +2430,20 @@ describe("FlatESLint", () => {
2388
2430
// create a new spy
2389
2431
spy = sinon . spy ( fs . promises , "readFile" ) ;
2390
2432
2391
- const [ cachedResult ] = await eslint . lintFiles ( [ file ] ) ;
2433
+ const [ newResult ] = await eslint . lintFiles ( [ file ] ) ;
2392
2434
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" ) ;
2396
2439
} ) ;
2397
2440
2398
2441
it ( "should remember the files from a previous run and do not operate on them if not changed" , async ( ) => {
2399
-
2400
2442
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" ) ;
2402
2447
2403
2448
eslint = new FlatESLint ( {
2404
2449
overrideConfigFile : true ,
@@ -2423,8 +2468,8 @@ describe("FlatESLint", () => {
2423
2468
2424
2469
const result = await eslint . lintFiles ( [ file ] ) ;
2425
2470
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" ) ;
2428
2473
2429
2474
// destroy the spy
2430
2475
sinon . restore ( ) ;
@@ -2449,20 +2494,23 @@ describe("FlatESLint", () => {
2449
2494
2450
2495
const cachedResult = await eslint . lintFiles ( [ file ] ) ;
2451
2496
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 " ) ;
2453
2498
2454
2499
// 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 " ) ;
2456
2501
} ) ;
2457
2502
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
+
2460
2508
const eslintOptions = {
2461
2509
overrideConfigFile : true ,
2462
2510
2463
2511
// specifying cache true the cache will be created
2464
2512
cache : true ,
2465
- cacheLocation,
2513
+ cacheLocation : cacheFilePath ,
2466
2514
overrideConfig : {
2467
2515
rules : {
2468
2516
"no-console" : 0 ,
@@ -2472,8 +2520,6 @@ describe("FlatESLint", () => {
2472
2520
cwd : path . join ( fixtureDir , ".." )
2473
2521
} ;
2474
2522
2475
- assert ( ! shell . test ( "-f" , cacheLocation ) , "the cache for eslint does not exist" ) ;
2476
-
2477
2523
eslint = new FlatESLint ( eslintOptions ) ;
2478
2524
2479
2525
let file = getFixturePath ( "cache/src" , "test-file.js" ) ;
@@ -2482,28 +2528,28 @@ describe("FlatESLint", () => {
2482
2528
2483
2529
await eslint . lintFiles ( [ file ] ) ;
2484
2530
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" ) ;
2486
2532
2487
2533
eslintOptions . cache = false ;
2488
2534
eslint = new FlatESLint ( eslintOptions ) ;
2489
2535
2490
2536
await eslint . lintFiles ( [ file ] ) ;
2491
2537
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" ) ;
2493
2539
} ) ;
2494
2540
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 " ) ;
2499
2545
2500
2546
eslint = new FlatESLint ( {
2501
2547
cwd : path . join ( fixtureDir , ".." ) ,
2502
2548
overrideConfigFile : true ,
2503
2549
2504
2550
// specifying cache true the cache will be created
2505
2551
cache : true ,
2506
- cacheLocation,
2552
+ cacheLocation : cacheFilePath ,
2507
2553
overrideConfig : {
2508
2554
rules : {
2509
2555
"no-console" : 0 ,
@@ -2514,30 +2560,35 @@ describe("FlatESLint", () => {
2514
2560
const badFile = fs . realpathSync ( getFixturePath ( "cache/src" , "fail-file.js" ) ) ;
2515
2561
const goodFile = fs . realpathSync ( getFixturePath ( "cache/src" , "test-file.js" ) ) ;
2516
2562
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" ) ;
2517
2569
2518
- assert ( shell . test ( "-f" , cacheLocation ) , "the cache for eslint was created" ) ;
2519
- const fileCache = fCache . createFromFile ( cacheLocation ) ;
2570
+ const fileCache = fCache . createFromFile ( cacheFilePath ) ;
2520
2571
const { cache } = fileCache ;
2521
2572
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" ) ;
2524
2575
const cachedResult = await eslint . lintFiles ( [ badFile , goodFile ] ) ;
2525
2576
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" ) ;
2527
2578
} ) ;
2528
2579
2529
2580
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" ) ;
2533
2584
2534
2585
eslint = new FlatESLint ( {
2535
2586
cwd : path . join ( fixtureDir , ".." ) ,
2536
2587
overrideConfigFile : true ,
2537
2588
2538
2589
// specifying cache true the cache will be created
2539
2590
cache : true ,
2540
- cacheLocation,
2591
+ cacheLocation : cacheFilePath ,
2541
2592
overrideConfig : {
2542
2593
rules : {
2543
2594
"no-console" : 0 ,
@@ -2550,10 +2601,10 @@ describe("FlatESLint", () => {
2550
2601
const toBeDeletedFile = fs . realpathSync ( getFixturePath ( "cache/src" , "file-to-delete.js" ) ) ;
2551
2602
2552
2603
await eslint . lintFiles ( [ badFile , goodFile , toBeDeletedFile ] ) ;
2553
- const fileCache = fCache . createFromFile ( cacheLocation ) ;
2604
+ const fileCache = fCache . createFromFile ( cacheFilePath ) ;
2554
2605
let { cache } = fileCache ;
2555
2606
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" ) ;
2557
2608
2558
2609
// delete the file from the file system
2559
2610
fs . unlinkSync ( toBeDeletedFile ) ;
@@ -2564,23 +2615,27 @@ describe("FlatESLint", () => {
2564
2615
*/
2565
2616
await eslint . lintFiles ( [ badFile , goodFile ] ) ;
2566
2617
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" ) ;
2568
2621
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" ) ;
2570
2625
} ) ;
2571
2626
2572
2627
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" ) ;
2576
2631
2577
2632
eslint = new FlatESLint ( {
2578
2633
cwd : path . join ( fixtureDir , ".." ) ,
2579
2634
overrideConfigFile : true ,
2580
2635
2581
2636
// specifying cache true the cache will be created
2582
2637
cache : true ,
2583
- cacheLocation,
2638
+ cacheLocation : cacheFilePath ,
2584
2639
overrideConfig : {
2585
2640
rules : {
2586
2641
"no-console" : 0 ,
@@ -2594,31 +2649,35 @@ describe("FlatESLint", () => {
2594
2649
2595
2650
await eslint . lintFiles ( [ badFile , goodFile , testFile2 ] ) ;
2596
2651
2597
- let fileCache = fCache . createFromFile ( cacheLocation ) ;
2652
+ let fileCache = fCache . createFromFile ( cacheFilePath ) ;
2598
2653
let { cache } = fileCache ;
2599
2654
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" ) ;
2601
2656
2602
2657
/*
2603
- * we pass a different set of files minus test-file2
2658
+ * we pass a different set of files ( minus test-file2)
2604
2659
* previous version of file-entry-cache would remove the non visited
2605
2660
* entries. 2.0.0 version will keep them unless they don't exist
2606
2661
*/
2607
2662
await eslint . lintFiles ( [ badFile , goodFile ] ) ;
2608
2663
2609
- fileCache = fCache . createFromFile ( cacheLocation ) ;
2664
+ fileCache = fCache . createFromFile ( cacheFilePath ) ;
2610
2665
cache = fileCache . cache ;
2611
2666
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" ) ;
2613
2668
} ) ;
2614
2669
2615
2670
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
2617
2676
2618
2677
eslint = new FlatESLint ( {
2619
2678
cwd : path . join ( fixtureDir , ".." ) ,
2620
2679
overrideConfigFile : true ,
2621
- cacheLocation,
2680
+ cacheLocation : cacheFilePath ,
2622
2681
overrideConfig : {
2623
2682
rules : {
2624
2683
"no-console" : 0 ,
@@ -2627,20 +2686,24 @@ describe("FlatESLint", () => {
2627
2686
}
2628
2687
} ) ;
2629
2688
2630
- assert ( shell . test ( "-f" , cacheLocation ) , "the cache for eslint exists " ) ;
2689
+ assert ( shell . test ( "-f" , cacheFilePath ) , "the cache for eslint should exist " ) ;
2631
2690
2632
2691
await eslint . lintText ( "var foo = 'bar';" ) ;
2633
2692
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 " ) ;
2635
2694
} ) ;
2636
2695
2637
2696
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
2639
2702
2640
2703
eslint = new FlatESLint ( {
2641
2704
cwd : path . join ( fixtureDir , ".." ) ,
2642
2705
overrideConfigFile : true ,
2643
- cacheLocation,
2706
+ cacheLocation : cacheFilePath ,
2644
2707
overrideConfig : {
2645
2708
rules : {
2646
2709
"no-console" : 0 ,
@@ -2649,21 +2712,25 @@ describe("FlatESLint", () => {
2649
2712
}
2650
2713
} ) ;
2651
2714
2652
- assert ( shell . test ( "-f" , cacheLocation ) , "the cache for eslint exists " ) ;
2715
+ assert ( shell . test ( "-f" , cacheFilePath ) , "the cache for eslint should exist " ) ;
2653
2716
2654
2717
await eslint . lintText ( "var bar = foo;" , { filePath : "fixtures/passing.js" } ) ;
2655
2718
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 " ) ;
2657
2720
} ) ;
2658
2721
2659
2722
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 , "" ) ;
2661
2728
2662
2729
eslint = new FlatESLint ( {
2663
2730
cwd : path . join ( fixtureDir , ".." ) ,
2664
2731
overrideConfigFile : true ,
2665
2732
cache : true ,
2666
- cacheLocation,
2733
+ cacheLocation : cacheFilePath ,
2667
2734
overrideConfig : {
2668
2735
rules : {
2669
2736
"no-console" : 0 ,
@@ -2673,90 +2740,92 @@ describe("FlatESLint", () => {
2673
2740
} ) ;
2674
2741
const file = getFixturePath ( "cli-engine" , "console.js" ) ;
2675
2742
2676
- assert ( shell . test ( "-f" , cacheLocation ) , "the cache for eslint exists " ) ;
2743
+ assert ( shell . test ( "-f" , cacheFilePath ) , "the cache for eslint should exist " ) ;
2677
2744
2678
2745
await eslint . lintFiles ( [ file ] ) ;
2679
2746
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 " ) ;
2681
2748
} ) ;
2682
2749
2683
2750
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
2685
2756
2686
2757
eslint = new FlatESLint ( {
2687
2758
cwd : path . join ( fixtureDir , ".." ) ,
2688
2759
overrideConfigFile : true ,
2689
- cacheLocation,
2760
+ cacheLocation : cacheFilePath ,
2690
2761
overrideConfig : {
2691
2762
rules : {
2692
2763
"no-console" : 0 ,
2693
2764
"no-unused-vars" : 2
2694
2765
}
2695
2766
}
2696
-
2697
2767
} ) ;
2698
2768
const file = getFixturePath ( "cli-engine" , "console.js" ) ;
2699
2769
2700
- assert ( shell . test ( "-f" , cacheLocation ) , "the cache for eslint exists " ) ;
2770
+ assert ( shell . test ( "-f" , cacheFilePath ) , "the cache for eslint should exist " ) ;
2701
2771
2702
2772
await eslint . lintFiles ( [ file ] ) ;
2703
2773
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" ) ;
2705
2775
} ) ;
2706
2776
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" ) ;
2710
2781
2711
- assert ( ! shell . test ( "-f" , customCacheFile ) , "the cache for eslint does not exist" ) ;
2782
+ eslint = new FlatESLint ( {
2783
+ overrideConfigFile : true ,
2712
2784
2713
- eslint = new FlatESLint ( {
2714
- overrideConfigFile : true ,
2785
+ // specify a custom cache file
2786
+ cacheLocation : cacheFilePath ,
2715
2787
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
+ } ,
2718
2796
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 ] ) ;
2727
2802
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" ) ;
2733
2804
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 ;
2737
2807
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" ) ;
2739
2810
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 ] ) ;
2742
2812
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" ) ;
2745
2814
} ) ;
2746
2815
2747
2816
describe ( "cacheStrategy" , ( ) => {
2748
2817
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" ) ;
2752
2821
2753
2822
eslint = new FlatESLint ( {
2754
2823
cwd : path . join ( fixtureDir , ".." ) ,
2755
2824
overrideConfigFile : true ,
2756
2825
2757
2826
// specifying cache true the cache will be created
2758
2827
cache : true ,
2759
- cacheLocation,
2828
+ cacheLocation : cacheFilePath ,
2760
2829
cacheStrategy : "metadata" ,
2761
2830
overrideConfig : {
2762
2831
rules : {
@@ -2770,32 +2839,32 @@ describe("FlatESLint", () => {
2770
2839
const goodFile = fs . realpathSync ( getFixturePath ( "cache/src" , "test-file.js" ) ) ;
2771
2840
2772
2841
await eslint . lintFiles ( [ badFile , goodFile ] ) ;
2773
- let fileCache = fCache . createFromFile ( cacheLocation ) ;
2842
+ let fileCache = fCache . createFromFile ( cacheFilePath ) ;
2774
2843
const entries = fileCache . normalizeEntries ( [ badFile , goodFile ] ) ;
2775
2844
2776
2845
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` ) ;
2778
2847
} ) ;
2779
2848
2780
2849
// this should result in a changed entry
2781
2850
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` ) ;
2785
2854
} ) ;
2786
2855
2787
2856
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" ) ;
2791
2860
2792
2861
eslint = new FlatESLint ( {
2793
2862
cwd : path . join ( fixtureDir , ".." ) ,
2794
2863
overrideConfigFile : true ,
2795
2864
2796
2865
// specifying cache true the cache will be created
2797
2866
cache : true ,
2798
- cacheLocation,
2867
+ cacheLocation : cacheFilePath ,
2799
2868
cacheStrategy : "content" ,
2800
2869
overrideConfig : {
2801
2870
rules : {
@@ -2809,34 +2878,34 @@ describe("FlatESLint", () => {
2809
2878
const goodFile = fs . realpathSync ( getFixturePath ( "cache/src" , "test-file.js" ) ) ;
2810
2879
2811
2880
await eslint . lintFiles ( [ badFile , goodFile ] ) ;
2812
- let fileCache = fCache . createFromFile ( cacheLocation , true ) ;
2881
+ let fileCache = fCache . createFromFile ( cacheFilePath , true ) ;
2813
2882
let entries = fileCache . normalizeEntries ( [ badFile , goodFile ] ) ;
2814
2883
2815
2884
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` ) ;
2817
2886
} ) ;
2818
2887
2819
2888
// this should NOT result in a changed entry
2820
2889
shell . touch ( goodFile ) ;
2821
- fileCache = fCache . createFromFile ( cacheLocation , true ) ;
2890
+ fileCache = fCache . createFromFile ( cacheFilePath , true ) ;
2822
2891
entries = fileCache . normalizeEntries ( [ badFile , goodFile ] ) ;
2823
2892
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` ) ;
2825
2894
} ) ;
2826
2895
} ) ;
2827
2896
2828
2897
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" ) ;
2832
2901
2833
2902
eslint = new FlatESLint ( {
2834
2903
cwd : path . join ( fixtureDir , ".." ) ,
2835
2904
overrideConfigFile : true ,
2836
2905
2837
2906
// specifying cache true the cache will be created
2838
2907
cache : true ,
2839
- cacheLocation,
2908
+ cacheLocation : cacheFilePath ,
2840
2909
cacheStrategy : "content" ,
2841
2910
overrideConfig : {
2842
2911
rules : {
@@ -2853,18 +2922,18 @@ describe("FlatESLint", () => {
2853
2922
shell . cp ( goodFile , goodFileCopy ) ;
2854
2923
2855
2924
await eslint . lintFiles ( [ badFile , goodFileCopy ] ) ;
2856
- let fileCache = fCache . createFromFile ( cacheLocation , true ) ;
2925
+ let fileCache = fCache . createFromFile ( cacheFilePath , true ) ;
2857
2926
const entries = fileCache . normalizeEntries ( [ badFile , goodFileCopy ] ) ;
2858
2927
2859
2928
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` ) ;
2861
2930
} ) ;
2862
2931
2863
2932
// this should result in a changed entry
2864
2933
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` ) ;
2868
2937
} ) ;
2869
2938
} ) ;
2870
2939
} ) ;
0 commit comments