|
| 1 | +import { existsSync } from 'node:fs' |
| 2 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | + |
| 5 | +import { runCommand } from 'citty' |
| 6 | +import { join } from 'pathe' |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +import cleanup from '../../../src/commands/cleanup' |
| 10 | + |
| 11 | +const { loadNuxtConfig } = vi.hoisted(() => ({ |
| 12 | + loadNuxtConfig: vi.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +vi.mock('../../../src/utils/kit', () => ({ |
| 16 | + loadKit: () => Promise.resolve({ loadNuxtConfig }), |
| 17 | +})) |
| 18 | + |
| 19 | +let cwd: string |
| 20 | + |
| 21 | +async function createFile(path: string) { |
| 22 | + await mkdir(join(path, '..'), { recursive: true }) |
| 23 | + await writeFile(path, '') |
| 24 | +} |
| 25 | + |
| 26 | +describe('cleanup', () => { |
| 27 | + beforeEach(async () => { |
| 28 | + vi.clearAllMocks() |
| 29 | + cwd = await mkdtemp(join(tmpdir(), 'nuxt-cleanup-')) |
| 30 | + loadNuxtConfig.mockResolvedValue({ rootDir: cwd, buildDir: join(cwd, '.nuxt') }) |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(async () => { |
| 34 | + await rm(cwd, { recursive: true, force: true }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('loads the development config and removes generated directories', async () => { |
| 38 | + const generated = [ |
| 39 | + '.nuxt/nuxt.json', |
| 40 | + '.output/server/index.mjs', |
| 41 | + 'dist/index.html', |
| 42 | + 'node_modules/.vite/cache', |
| 43 | + 'node_modules/.cache/nuxt/client.json', |
| 44 | + ] |
| 45 | + await Promise.all(generated.map(path => createFile(join(cwd, path)))) |
| 46 | + await createFile(join(cwd, 'node_modules/nuxt/package.json')) |
| 47 | + |
| 48 | + await runCommand(cleanup, { rawArgs: [cwd] }) |
| 49 | + |
| 50 | + expect(loadNuxtConfig).toHaveBeenCalledWith({ cwd, overrides: { dev: true } }) |
| 51 | + expect(generated.every(path => !existsSync(join(cwd, path)))).toBe(true) |
| 52 | + expect(existsSync(join(cwd, 'node_modules/nuxt/package.json'))).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it('removes a custom build directory only once when it overlaps a cache directory', async () => { |
| 56 | + const buildDir = join(cwd, 'node_modules/.cache') |
| 57 | + loadNuxtConfig.mockResolvedValue({ rootDir: cwd, buildDir }) |
| 58 | + await createFile(join(buildDir, 'nuxt/client.json')) |
| 59 | + |
| 60 | + await expect(runCommand(cleanup, { rawArgs: [cwd] })).resolves.toBeDefined() |
| 61 | + }) |
| 62 | + |
| 63 | + it.each([ |
| 64 | + ['the project root', () => cwd], |
| 65 | + ['a parent of the project root', () => join(cwd, '..')], |
| 66 | + ])('refuses to remove %s', async (_, getBuildDir) => { |
| 67 | + loadNuxtConfig.mockResolvedValue({ rootDir: cwd, buildDir: getBuildDir() }) |
| 68 | + await createFile(join(cwd, 'package.json')) |
| 69 | + |
| 70 | + await expect(runCommand(cleanup, { rawArgs: [cwd] })).rejects.toThrow('Cannot clean a build directory that contains the project root.') |
| 71 | + expect(existsSync(join(cwd, 'package.json'))).toBe(true) |
| 72 | + }) |
| 73 | +}) |
0 commit comments