Skip to content

Commit

Permalink
🐛 fix(core): Treat empty cache dirs as noise
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Jan 22, 2024
1 parent 9d94362 commit 4d81dd6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/utils/isDirEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import fs from 'fs-extra'

export async function isDirEmpty(dirPath: string) {
const files = await fs.readdir(dirPath)
try {
const files = await fs.readdir(dirPath)

return files.length === 0
return files.length === 0
} catch (e) {
if (e instanceof Error && e.message.includes('ENOENT')) {
return true
}

throw e
}
}
23 changes: 16 additions & 7 deletions packages/core/src/utils/triggerCacheCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs-extra'
import { ensureCacheDirExists } from '../ensureCacheDirExists'
import { createCacheForWalletSetupFunction } from './createCacheForWalletSetupFunction'
import { getUniqueWalletSetupFunctions } from './getUniqueWalletSetupFunctions'
import { isDirEmpty } from './isDirEmpty'

export async function triggerCacheCreation(
setupFunctions: Awaited<ReturnType<typeof getUniqueWalletSetupFunctions>>,
Expand All @@ -16,14 +17,22 @@ export async function triggerCacheCreation(

for (const [funcHash, { fileName, setupFunction }] of setupFunctions) {
const cachePath = path.join(cacheDirPath, funcHash)
if (await fs.exists(cachePath)) {
if (!force) {
console.log(`Cache already exists for ${funcHash}. Skipping...`)
continue
}
const doesCacheDirExist = await fs.exists(cachePath)
const isCacheDirEmpty = await isDirEmpty(cachePath)

if (doesCacheDirExist) {
if (isCacheDirEmpty) {
// In case of incorrect Playwright setup, the cache dir will be empty. For now, we're just deleting it.
await fs.remove(cachePath)
} else {
if (!force) {
console.log(`Cache already exists for ${funcHash}. Skipping...`)
continue
}

console.log(`Cache already exists for ${funcHash} but force flag is set. Deleting cache...`)
await fs.remove(cachePath)
console.log(`Cache already exists for ${funcHash} but force flag is set. Deleting cache...`)
await fs.remove(cachePath)
}
}

const fileNameWithCorrectExtension = fileName.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}')
Expand Down
5 changes: 5 additions & 0 deletions packages/core/test/utils/isDirEmpty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe('isDirEmpty', () => {
vol.reset() // Clear the in-memory file system after each test
})

it('returns `true` if the directory does not exist', async () => {
const isEmpty = await isDirEmpty(path.join('empty_dir'))
expect(isEmpty).toEqual(true)
})

it('returns `true` if the directory is empty', async () => {
const isEmpty = await isDirEmpty(ROOT_DIR)
expect(isEmpty).toEqual(true)
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/utils/triggerCacheCreation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ vi.mock('../../src/utils/createCacheForWalletSetupFunction', async () => {
}
})

// We're not adding a test for code that uses `isDirEmpty` because soon it will be removed.
vi.mock('../../src/utils/isDirEmpty', async () => {
return {
isDirEmpty: vi.fn(async () => {
return false
})
}
})

describe('triggerCacheCreation', () => {
const createCacheForWalletSetupFunctionSpy = vi.spyOn(
CreateCacheForWalletSetupFunction,
Expand Down

0 comments on commit 4d81dd6

Please sign in to comment.