From 468ac585696e128cdb2d0e5e70e1d548727e460e Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Tue, 23 Jan 2024 01:22:21 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(core):=20Treat=20empty=20cac?= =?UTF-8?q?he=20dirs=20as=20noise=20(#1078)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/utils/isDirEmpty.ts | 12 ++++++++-- .../core/src/utils/triggerCacheCreation.ts | 23 +++++++++++++------ packages/core/test/utils/isDirEmpty.test.ts | 5 ++++ .../test/utils/triggerCacheCreation.test.ts | 9 ++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/core/src/utils/isDirEmpty.ts b/packages/core/src/utils/isDirEmpty.ts index e3fb88f2b..69c1bc2c7 100644 --- a/packages/core/src/utils/isDirEmpty.ts +++ b/packages/core/src/utils/isDirEmpty.ts @@ -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 + } } diff --git a/packages/core/src/utils/triggerCacheCreation.ts b/packages/core/src/utils/triggerCacheCreation.ts index d26ba2179..491f8643e 100644 --- a/packages/core/src/utils/triggerCacheCreation.ts +++ b/packages/core/src/utils/triggerCacheCreation.ts @@ -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>, @@ -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}') diff --git a/packages/core/test/utils/isDirEmpty.test.ts b/packages/core/test/utils/isDirEmpty.test.ts index d28d15518..b83b78d91 100644 --- a/packages/core/test/utils/isDirEmpty.test.ts +++ b/packages/core/test/utils/isDirEmpty.test.ts @@ -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) diff --git a/packages/core/test/utils/triggerCacheCreation.test.ts b/packages/core/test/utils/triggerCacheCreation.test.ts index ed41eea7a..acd067ea3 100644 --- a/packages/core/test/utils/triggerCacheCreation.test.ts +++ b/packages/core/test/utils/triggerCacheCreation.test.ts @@ -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,