|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import { dirname, join } from 'pathe' |
| 3 | +import { expect, test } from 'vitest' |
| 4 | +import { runInlineTests } from '#test-utils' |
| 5 | + |
| 6 | +const SIGNATURE = 'Signature: 8a477f597d28d172789f06886806bc55' |
| 7 | + |
| 8 | +const passingTest = /* js */ ` |
| 9 | +import { test } from 'vitest' |
| 10 | +test('passes', () => {}) |
| 11 | +` |
| 12 | + |
| 13 | +function readTag(dir: string) { |
| 14 | + return readFileSync(join(dir, 'CACHEDIR.TAG'), 'utf-8') |
| 15 | +} |
| 16 | + |
| 17 | +test('tags the results cache and the fs module caches', async () => { |
| 18 | + const { ctx, root, stderr } = await runInlineTests( |
| 19 | + { |
| 20 | + 'unit/basic.test.js': passingTest, |
| 21 | + 'api/basic.test.js': passingTest, |
| 22 | + 'vitest.config.js': { |
| 23 | + test: { |
| 24 | + fsModuleCache: true, |
| 25 | + fsModuleCachePath: './node_modules/.vitest-fs-cache', |
| 26 | + projects: [ |
| 27 | + { test: { name: 'unit', include: ['unit/*.test.js'] } }, |
| 28 | + { |
| 29 | + test: { |
| 30 | + name: 'api', |
| 31 | + include: ['api/*.test.js'], |
| 32 | + fsModuleCachePath: './node_modules/.vitest-api-cache', |
| 33 | + }, |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { cache: true }, |
| 40 | + ) |
| 41 | + expect(stderr).toBe('') |
| 42 | + |
| 43 | + // the results are in "cacheDir/vitest/<hash>", Vitest owns "cacheDir/vitest" |
| 44 | + const vitestCacheDir = dirname(ctx!.viteConfig.cacheDir) |
| 45 | + expect(vitestCacheDir).toBe(join(root, 'node_modules/.vite/vitest')) |
| 46 | + const tag = readTag(vitestCacheDir) |
| 47 | + expect(tag).toMatchInlineSnapshot(` |
| 48 | + "Signature: 8a477f597d28d172789f06886806bc55 |
| 49 | + # This file is a cache directory tag created by Vitest. |
| 50 | + # For information about cache directory tags see https://bford.info/cachedir/ |
| 51 | + " |
| 52 | + `) |
| 53 | + expect(readTag(join(root, 'node_modules/.vitest-fs-cache'))).toBe(tag) |
| 54 | + expect(readTag(join(root, 'node_modules/.vitest-api-cache'))).toBe(tag) |
| 55 | +}) |
| 56 | + |
| 57 | +test('keeps an existing tag', async () => { |
| 58 | + const customTag = `${SIGNATURE}\n# custom\n` |
| 59 | + const { root, stderr } = await runInlineTests( |
| 60 | + { |
| 61 | + 'basic.test.js': passingTest, |
| 62 | + 'node_modules/.vite/vitest/CACHEDIR.TAG': customTag, |
| 63 | + 'node_modules/.vitest-fs-cache/CACHEDIR.TAG': customTag, |
| 64 | + 'vitest.config.js': { |
| 65 | + test: { |
| 66 | + fsModuleCache: true, |
| 67 | + fsModuleCachePath: './node_modules/.vitest-fs-cache', |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { cache: true }, |
| 72 | + ) |
| 73 | + expect(stderr).toBe('') |
| 74 | + expect(readTag(join(root, 'node_modules/.vite/vitest'))).toBe(customTag) |
| 75 | + expect(readTag(join(root, 'node_modules/.vitest-fs-cache'))).toBe(customTag) |
| 76 | +}) |
0 commit comments