diff --git a/lib/cli.js b/lib/cli.js index 4581af4fb..b613666c8 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -261,12 +261,25 @@ export default async function loadCli() { // eslint-disable-line complexity if (resetCache) { const cacheDir = path.join(projectDir, 'node_modules', '.cache', 'ava'); try { - if (fs.existsSync(cacheDir)) { - fs.rmSync(cacheDir, {recursive: true, force: true}); - fs.mkdirSync(cacheDir); - console.log(`\n${chalk.green(figures.tick)} Emptied AVA cache cache directory ${cacheDir}`); + let entries; + try { + entries = fs.readdirSync(cacheDir); + } catch (error) { + if (error.code === 'ENOENT') { + entries = []; + } else { + throw error; + } + } + + for (const entry of entries) { + fs.rmSync(path.join(cacheDir, entry), {recursive: true, force: true}); + } + + if (entries.length === 0) { + console.log(`\n${chalk.green(figures.tick)} No cache files to remove`); } else { - console.log(`\n${chalk.green(figures.tick)} No cache directory to remove`); + console.log(`\n${chalk.green(figures.tick)} Removed AVA cache files in ${cacheDir}`); } process.exit(0); // eslint-disable-line unicorn/no-process-exit diff --git a/test-tap/api.js b/test-tap/api.js index baf7d902b..b395d81f8 100644 --- a/test-tap/api.js +++ b/test-tap/api.js @@ -379,10 +379,7 @@ for (const opt of options) { }); test(`caching is enabled by default - workerThreads: ${opt.workerThreads}`, async t => { - const nodeModulesPath = path.join(__dirname, 'fixture/caching/node_modules'); - if (fs.existsSync(nodeModulesPath)) { - fs.rmSync(nodeModulesPath, {recursive: true, force: true}); - } + fs.rmSync(path.join(__dirname, 'fixture/caching/node_modules'), {recursive: true, force: true}); const api = await apiCreator({ ...opt, @@ -402,10 +399,7 @@ for (const opt of options) { }); test(`caching can be disabled - workerThreads: ${opt.workerThreads}`, async t => { - const nodeModulesPath = path.join(__dirname, 'fixture/caching/node_modules'); - if (fs.existsSync(nodeModulesPath)) { - fs.rmSync(nodeModulesPath, {recursive: true, force: true}); - } + fs.rmSync(path.join(__dirname, 'fixture/caching/node_modules'), {recursive: true, force: true}); const api = await apiCreator({ ...opt,