From f1bec627a0bf768e1a722847489d5158d6dcc110 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 24 Apr 2024 12:08:04 +0200 Subject: [PATCH] refactor: remove VitestCache.clearCache method (#5602) --- packages/vitest/src/node/cache/index.ts | 35 ------------------------- test/cache/.gitignore | 1 - test/cache/package.json | 12 --------- test/cache/test/clear-cache.test.ts | 26 ------------------ test/cache/vitest-custom.config.ts | 9 ------- test/cache/vitest.config.ts | 10 ------- 6 files changed, 93 deletions(-) delete mode 100644 test/cache/.gitignore delete mode 100644 test/cache/package.json delete mode 100644 test/cache/test/clear-cache.test.ts delete mode 100644 test/cache/vitest-custom.config.ts delete mode 100644 test/cache/vitest.config.ts diff --git a/packages/vitest/src/node/cache/index.ts b/packages/vitest/src/node/cache/index.ts index 209722e78862..994b4d78ff8e 100644 --- a/packages/vitest/src/node/cache/index.ts +++ b/packages/vitest/src/node/cache/index.ts @@ -1,10 +1,5 @@ -import fs from 'node:fs' import crypto from 'node:crypto' -import { findUp } from 'find-up' import { resolve } from 'pathe' -import { loadConfigFromFile } from 'vite' -import { configFiles } from '../../constants' -import type { CliOptions } from '../cli/cli-api' import { slash } from '../../utils' import { FilesStatsCache } from './files' import { ResultsCache } from './results' @@ -27,34 +22,4 @@ export class VitestCache { ? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex')) : resolve(root, baseDir) } - - static async clearCache(options: CliOptions) { - const root = resolve(options.root || process.cwd()) - - const configPath = options.config === false - ? false - : options.config - ? resolve(root, options.config) - : await findUp(configFiles, { cwd: root } as any) - - const config = configPath - ? (await loadConfigFromFile({ command: 'serve', mode: 'test' }, configPath))?.config - : undefined - - const cache = config?.test?.cache - const projectName = config?.test?.name - - if (cache === false) - throw new Error('Cache is disabled') - - const cachePath = VitestCache.resolveCacheDir(root, cache?.dir, projectName) - - let cleared = false - - if (fs.existsSync(cachePath)) { - fs.rmSync(cachePath, { recursive: true, force: true }) - cleared = true - } - return { dir: cachePath, cleared } - } } diff --git a/test/cache/.gitignore b/test/cache/.gitignore deleted file mode 100644 index f8a91fd2ba6b..000000000000 --- a/test/cache/.gitignore +++ /dev/null @@ -1 +0,0 @@ -cache/* \ No newline at end of file diff --git a/test/cache/package.json b/test/cache/package.json deleted file mode 100644 index 7460dd246ae2..000000000000 --- a/test/cache/package.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "@vitest/test-cache", - "type": "module", - "private": true, - "scripts": { - "test": "vitest", - "coverage": "vitest run --coverage" - }, - "devDependencies": { - "vitest": "workspace:*" - } -} diff --git a/test/cache/test/clear-cache.test.ts b/test/cache/test/clear-cache.test.ts deleted file mode 100644 index 323461c43a66..000000000000 --- a/test/cache/test/clear-cache.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import fs, { promises as fsp } from 'node:fs' -import { resolve } from 'pathe' -import { describe, expect, test } from 'vitest' -import { VitestCache } from '../../../packages/vitest/src/node/cache/index' - -const root = resolve(__dirname, '..') - -const pathBase = resolve(root, 'cache/.vitest-base') -const pathCustom = resolve(root, 'cache/.vitest-custom') - -describe('vitest cache', async () => { - await fsp.mkdir(pathBase, { recursive: true }) - await fsp.mkdir(pathCustom, { recursive: true }) - - test('clears cache without specifying config path', async () => { - await VitestCache.clearCache({}) - - expect(fs.existsSync(pathBase)).toBe(false) - }) - - test('clears cache with specified config path', async () => { - await VitestCache.clearCache({ config: 'vitest-custom.config.ts' }) - - expect(fs.existsSync(pathCustom)).toBe(false) - }) -}) diff --git a/test/cache/vitest-custom.config.ts b/test/cache/vitest-custom.config.ts deleted file mode 100644 index 6087616010f8..000000000000 --- a/test/cache/vitest-custom.config.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - cache: { - dir: 'cache/.vitest-custom', - }, - }, -}) diff --git a/test/cache/vitest.config.ts b/test/cache/vitest.config.ts deleted file mode 100644 index 6461c042ead9..000000000000 --- a/test/cache/vitest.config.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { defineConfig } from 'vite' - -export default defineConfig({ - test: { - pool: 'forks', - cache: { - dir: 'cache/.vitest-base', - }, - }, -})