[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

refactor: remove VitestCache.clearCache method (#5602)

authored by

Vladimir and committed by
GitHub
(Apr 24, 2024, 12:08 PM +0200) f1bec627 23f29cea

-93
-1
test/cache/.gitignore
··· 1 - cache/*
-12
test/cache/package.json
··· 1 - { 2 - "name": "@vitest/test-cache", 3 - "type": "module", 4 - "private": true, 5 - "scripts": { 6 - "test": "vitest", 7 - "coverage": "vitest run --coverage" 8 - }, 9 - "devDependencies": { 10 - "vitest": "workspace:*" 11 - } 12 - }
-9
test/cache/vitest-custom.config.ts
··· 1 - import { defineConfig } from 'vite' 2 - 3 - export default defineConfig({ 4 - test: { 5 - cache: { 6 - dir: 'cache/.vitest-custom', 7 - }, 8 - }, 9 - })
-10
test/cache/vitest.config.ts
··· 1 - import { defineConfig } from 'vite' 2 - 3 - export default defineConfig({ 4 - test: { 5 - pool: 'forks', 6 - cache: { 7 - dir: 'cache/.vitest-base', 8 - }, 9 - }, 10 - })
-26
test/cache/test/clear-cache.test.ts
··· 1 - import fs, { promises as fsp } from 'node:fs' 2 - import { resolve } from 'pathe' 3 - import { describe, expect, test } from 'vitest' 4 - import { VitestCache } from '../../../packages/vitest/src/node/cache/index' 5 - 6 - const root = resolve(__dirname, '..') 7 - 8 - const pathBase = resolve(root, 'cache/.vitest-base') 9 - const pathCustom = resolve(root, 'cache/.vitest-custom') 10 - 11 - describe('vitest cache', async () => { 12 - await fsp.mkdir(pathBase, { recursive: true }) 13 - await fsp.mkdir(pathCustom, { recursive: true }) 14 - 15 - test('clears cache without specifying config path', async () => { 16 - await VitestCache.clearCache({}) 17 - 18 - expect(fs.existsSync(pathBase)).toBe(false) 19 - }) 20 - 21 - test('clears cache with specified config path', async () => { 22 - await VitestCache.clearCache({ config: 'vitest-custom.config.ts' }) 23 - 24 - expect(fs.existsSync(pathCustom)).toBe(false) 25 - }) 26 - })
-35
packages/vitest/src/node/cache/index.ts
··· 1 - import fs from 'node:fs' 2 1 import crypto from 'node:crypto' 3 - import { findUp } from 'find-up' 4 2 import { resolve } from 'pathe' 5 - import { loadConfigFromFile } from 'vite' 6 - import { configFiles } from '../../constants' 7 - import type { CliOptions } from '../cli/cli-api' 8 3 import { slash } from '../../utils' 9 4 import { FilesStatsCache } from './files' 10 5 import { ResultsCache } from './results' ··· 26 21 return projectName 27 22 ? resolve(root, baseDir, crypto.createHash('md5').update(projectName, 'utf-8').digest('hex')) 28 23 : resolve(root, baseDir) 29 - } 30 - 31 - static async clearCache(options: CliOptions) { 32 - const root = resolve(options.root || process.cwd()) 33 - 34 - const configPath = options.config === false 35 - ? false 36 - : options.config 37 - ? resolve(root, options.config) 38 - : await findUp(configFiles, { cwd: root } as any) 39 - 40 - const config = configPath 41 - ? (await loadConfigFromFile({ command: 'serve', mode: 'test' }, configPath))?.config 42 - : undefined 43 - 44 - const cache = config?.test?.cache 45 - const projectName = config?.test?.name 46 - 47 - if (cache === false) 48 - throw new Error('Cache is disabled') 49 - 50 - const cachePath = VitestCache.resolveCacheDir(root, cache?.dir, projectName) 51 - 52 - let cleared = false 53 - 54 - if (fs.existsSync(cachePath)) { 55 - fs.rmSync(cachePath, { recursive: true, force: true }) 56 - cleared = true 57 - } 58 - return { dir: cachePath, cleared } 59 24 } 60 25 }