|
| 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 | +importcleanupfrom'../../../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 | +letcwd: string |
| 20 | + |
| 21 | +asyncfunctioncreateFile(path: string){ |
| 22 | +awaitmkdir(join(path,'..'),{recursive: true}) |
| 23 | +awaitwriteFile(path,'') |
| 24 | +} |
| 25 | + |
| 26 | +describe('cleanup',()=>{ |
| 27 | +beforeEach(async()=>{ |
| 28 | +vi.clearAllMocks() |
| 29 | +cwd=awaitmkdtemp(join(tmpdir(),'nuxt-cleanup-')) |
| 30 | +loadNuxtConfig.mockResolvedValue({rootDir: cwd,buildDir: join(cwd,'.nuxt')}) |
| 31 | +}) |
| 32 | + |
| 33 | +afterEach(async()=>{ |
| 34 | +awaitrm(cwd,{recursive: true,force: true}) |
| 35 | +}) |
| 36 | + |
| 37 | +it('loads the development config and removes generated directories',async()=>{ |
| 38 | +constgenerated=[ |
| 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 | +awaitPromise.all(generated.map(path=>createFile(join(cwd,path)))) |
| 46 | +awaitcreateFile(join(cwd,'node_modules/nuxt/package.json')) |
| 47 | + |
| 48 | +awaitrunCommand(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 | +constbuildDir=join(cwd,'node_modules/.cache') |
| 57 | +loadNuxtConfig.mockResolvedValue({rootDir: cwd, buildDir }) |
| 58 | +awaitcreateFile(join(buildDir,'nuxt/client.json')) |
| 59 | + |
| 60 | +awaitexpect(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 | +awaitcreateFile(join(cwd,'package.json')) |
| 69 | + |
| 70 | +awaitexpect(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