|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { workspaces } from '@angular-devkit/core'; |
| 10 | +import assert from 'node:assert'; |
| 11 | +import { existsSync } from 'node:fs'; |
| 12 | +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 13 | +import { join } from 'node:path'; |
| 14 | +import type { CommandContext } from '../../../command-builder/command-module'; |
| 15 | +import type { AngularWorkspace } from '../../../utilities/config'; |
| 16 | +import { CacheCleanModule } from './cli'; |
| 17 | + |
| 18 | +describe('CacheCleanModule', () => { |
| 19 | + let tempDir: string; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 23 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 24 | + tempDir = await mkdtemp(join(baseTmpDir, 'angular-cli-cache-clean-spec-')); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(async () => { |
| 28 | + await rm(tempDir, { recursive: true, force: true }); |
| 29 | + }); |
| 30 | + |
| 31 | + function runClean(basePath: string, cliExtension?: unknown): Promise<void> { |
| 32 | + const workspace = { |
| 33 | + basePath, |
| 34 | + extensions: cliExtension ? { cli: cliExtension } : {}, |
| 35 | + projects: {} as unknown as workspaces.ProjectDefinitionCollection, |
| 36 | + filePath: join(basePath, 'angular.json'), |
| 37 | + getCli: () => cliExtension, |
| 38 | + getProjectCli: () => undefined, |
| 39 | + save: () => Promise.resolve(), |
| 40 | + } as unknown as AngularWorkspace; |
| 41 | + |
| 42 | + return new CacheCleanModule({ workspace } as unknown as CommandContext).run(); |
| 43 | + } |
| 44 | + |
| 45 | + it('should delete the cache of a standard repository', async () => { |
| 46 | + const workspaceRoot = join(tempDir, 'project'); |
| 47 | + await mkdir(join(workspaceRoot, '.git'), { recursive: true }); |
| 48 | + await mkdir(join(workspaceRoot, '.angular/cache/app'), { recursive: true }); |
| 49 | + |
| 50 | + await runClean(workspaceRoot); |
| 51 | + |
| 52 | + expect(existsSync(join(workspaceRoot, '.angular/cache'))).toBeFalse(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should delete both the shared and the worktree-local cache in a git worktree', async () => { |
| 56 | + const mainRepoRoot = join(tempDir, 'main-repo'); |
| 57 | + const mainGitDir = join(mainRepoRoot, '.git'); |
| 58 | + const worktreeRoot = join(tempDir, 'worktree'); |
| 59 | + const worktreeMetadataDir = join(mainGitDir, 'worktrees/wt-1'); |
| 60 | + |
| 61 | + await mkdir(worktreeMetadataDir, { recursive: true }); |
| 62 | + await mkdir(worktreeRoot, { recursive: true }); |
| 63 | + await writeFile(join(worktreeRoot, '.git'), `gitdir: ${worktreeMetadataDir}`); |
| 64 | + await writeFile(join(worktreeMetadataDir, 'commondir'), '../..'); |
| 65 | + |
| 66 | + // The shared cache lives in the main checkout, while the development server keeps |
| 67 | + // its Vite cache in the worktree itself. |
| 68 | + await mkdir(join(mainRepoRoot, '.angular/cache/app'), { recursive: true }); |
| 69 | + await mkdir(join(worktreeRoot, '.angular/cache/app/dev-server/vite'), { recursive: true }); |
| 70 | + |
| 71 | + await runClean(worktreeRoot); |
| 72 | + |
| 73 | + expect(existsSync(join(mainRepoRoot, '.angular/cache'))).toBeFalse(); |
| 74 | + expect(existsSync(join(worktreeRoot, '.angular/cache'))).toBeFalse(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should succeed when cache directories do not exist', async () => { |
| 78 | + const workspaceRoot = join(tempDir, 'project'); |
| 79 | + await mkdir(join(workspaceRoot, '.git'), { recursive: true }); |
| 80 | + |
| 81 | + await expectAsync(runClean(workspaceRoot)).toBeResolved(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should delete both the shared and the worktree-local cache with a custom relative path', async () => { |
| 85 | + const mainRepoRoot = join(tempDir, 'main-repo'); |
| 86 | + const mainGitDir = join(mainRepoRoot, '.git'); |
| 87 | + const worktreeRoot = join(tempDir, 'worktree'); |
| 88 | + const worktreeMetadataDir = join(mainGitDir, 'worktrees/wt-1'); |
| 89 | + |
| 90 | + await mkdir(worktreeMetadataDir, { recursive: true }); |
| 91 | + await mkdir(worktreeRoot, { recursive: true }); |
| 92 | + await writeFile(join(worktreeRoot, '.git'), `gitdir: ${worktreeMetadataDir}`); |
| 93 | + await writeFile(join(worktreeMetadataDir, 'commondir'), '../..'); |
| 94 | + |
| 95 | + const customCachePath = 'custom-cache-dir'; |
| 96 | + await mkdir(join(mainRepoRoot, customCachePath, 'app'), { recursive: true }); |
| 97 | + await mkdir(join(worktreeRoot, customCachePath, 'app/dev-server/vite'), { recursive: true }); |
| 98 | + |
| 99 | + await runClean(worktreeRoot, { cache: { path: customCachePath } }); |
| 100 | + |
| 101 | + expect(existsSync(join(mainRepoRoot, customCachePath))).toBeFalse(); |
| 102 | + expect(existsSync(join(worktreeRoot, customCachePath))).toBeFalse(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments