@@ -40,6 +40,8 @@ import {LMDBLiteCache} from '@atlaspack/cache';
4040import tempy from 'tempy' ;
4141import { FILE_CONFIG_NO_REPORTERS } from './paths' ;
4242
43+ import { queue } from 'async' ;
44+
4345export let cacheDir : string = tempy . directory ( ) ;
4446export let cache : LMDBLiteCache = new LMDBLiteCache ( cacheDir ) ;
4547cache . ensure ( ) ;
@@ -1328,6 +1330,52 @@ export async function assertNoFilePathInCache(
13281330 fs : FileSystem ,
13291331 dir : string ,
13301332 projectRoot : string ,
1333+ ) {
1334+ // For debugging purposes, log all instances of the projectRoot in the cache.
1335+ // Otherwise, fail the test if one is found.
1336+ if ( process . env . ATLASPACK_DEBUG_CACHE_FILEPATH != null )
1337+ return assertNoFilePathInCacheDebug ( fs , dir , projectRoot ) ;
1338+
1339+ const processFile = async ( filePath : string ) => {
1340+ let contents = await fs . readFile ( filePath ) ;
1341+ if ( contents . includes ( projectRoot ) ) {
1342+ throw new Error (
1343+ `Found projectRoot ${ projectRoot } in cache file ${ filePath } ` ,
1344+ ) ;
1345+ }
1346+ } ;
1347+ // This "optimal" concurrency value was determined by using hyperfine locally on a subset of tests
1348+ // that use the cache.
1349+ const q = queue ( processFile , 50 ) ;
1350+
1351+ const queueDir = async ( dir : string ) => {
1352+ const files = await fs . readdir ( dir ) ;
1353+ for ( const file of files ) {
1354+ const fullPath = path . join ( dir , file ) ;
1355+ const stat = await fs . stat ( fullPath ) ;
1356+ if ( stat . isFile ( ) && ! file . endsWith ( '.txt' ) ) {
1357+ q . push ( fullPath ) ;
1358+ } else if ( stat . isDirectory ( ) ) {
1359+ await queueDir ( fullPath ) ;
1360+ }
1361+ }
1362+ } ;
1363+ await queueDir ( dir ) ;
1364+
1365+ q . error ( ( err : Error ) => {
1366+ assert . fail ( err ) ;
1367+ } ) ;
1368+ if ( q . length ( ) > 0 ) {
1369+ await q . drain ( ) ;
1370+ }
1371+ }
1372+
1373+ // This function is called from the assertNoFilePathInCache function when the
1374+ // ATLASPACK_DEBUG_CACHE_FILEPATH environment variable is set.
1375+ async function assertNoFilePathInCacheDebug (
1376+ fs : FileSystem ,
1377+ dir : string ,
1378+ projectRoot : string ,
13311379) {
13321380 let entries = await fs . readdir ( dir ) ;
13331381 for ( let entry of entries ) {
@@ -1343,38 +1391,29 @@ export async function assertNoFilePathInCache(
13431391 } else if ( stat . isFile ( ) ) {
13441392 let contents = await fs . readFile ( fullPath ) ;
13451393
1346- // For debugging purposes, log all instances of the projectRoot in the cache.
1347- // Otherwise, fail the test if one is found.
1348- if ( process . env . ATLASPACK_DEBUG_CACHE_FILEPATH != null ) {
1349- if ( contents . includes ( projectRoot ) ) {
1350- let deserialized ;
1351- try {
1352- deserialized = v8 . deserialize ( contents ) ;
1353- } catch ( err : any ) {
1354- // rudimentary detection of binary files
1355- if ( ! contents . includes ( 0 ) ) {
1356- deserialized = contents . toString ( ) ;
1357- } else {
1358- deserialized = contents ;
1359- }
1394+ if ( contents . includes ( projectRoot ) ) {
1395+ let deserialized ;
1396+ try {
1397+ deserialized = v8 . deserialize ( contents ) ;
1398+ } catch ( err : any ) {
1399+ // rudimentary detection of binary files
1400+ if ( ! contents . includes ( 0 ) ) {
1401+ deserialized = contents . toString ( ) ;
1402+ } else {
1403+ deserialized = contents ;
13601404 }
1405+ }
13611406
1362- if ( deserialized != null ) {
1363- // eslint-disable-next-line no-console
1364- console . log (
1365- `Found projectRoot ${ projectRoot } in cache file ${ fullPath } ` ,
1366- ) ;
1367- // eslint-disable-next-line no-console
1368- console . log (
1369- require ( 'util' ) . inspect ( deserialized , { depth : 50 , colors : true } ) ,
1370- ) ;
1371- }
1407+ if ( deserialized != null ) {
1408+ // eslint-disable-next-line no-console
1409+ console . log (
1410+ `Found projectRoot ${ projectRoot } in cache file ${ fullPath } ` ,
1411+ ) ;
1412+ // eslint-disable-next-line no-console
1413+ console . log (
1414+ require ( 'util' ) . inspect ( deserialized , { depth : 50 , colors : true } ) ,
1415+ ) ;
13721416 }
1373- } else {
1374- assert (
1375- ! contents . includes ( projectRoot ) ,
1376- `Found projectRoot ${ projectRoot } in cache file ${ fullPath } ` ,
1377- ) ;
13781417 }
13791418 }
13801419 }
0 commit comments