|
| 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 assert from 'node:assert'; |
| 10 | +import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { toPosixPath } from '../../../utils/path'; |
| 13 | +import { collectAssetsToEmit, createAssetMatchers } from './assets'; |
| 14 | + |
| 15 | +describe('library pipeline assets', () => { |
| 16 | + let tempRoot: string; |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 20 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 21 | + tempRoot = toPosixPath(await mkdtemp(path.join(baseTmpDir, 'angular-cli-assets-spec-'))); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + await rm(tempRoot, { recursive: true, force: true }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('createAssetMatchers', () => { |
| 29 | + it('should normalize patterns, expand directories in ignore, and skip empty strings', () => { |
| 30 | + const matchers = createAssetMatchers([ |
| 31 | + { |
| 32 | + glob: './**/*.txt', |
| 33 | + input: `${tempRoot}/assets`, |
| 34 | + output: 'assets', |
| 35 | + ignore: ['', 'ignored-dir', './ignored-dir-2/', 'specific.txt'], |
| 36 | + }, |
| 37 | + ]); |
| 38 | + |
| 39 | + expect(matchers).toHaveSize(1); |
| 40 | + const [matcher] = matchers; |
| 41 | + |
| 42 | + expect(matcher.isMatch('keep.txt')).toBeTrue(); |
| 43 | + expect(matcher.isMatch('nested/keep.txt')).toBeTrue(); |
| 44 | + expect(matcher.isMatch('.hidden.txt')).toBeTrue(); |
| 45 | + expect(matcher.isMatch('specific.txt')).toBeFalse(); |
| 46 | + expect(matcher.isMatch('ignored-dir/file.txt')).toBeFalse(); |
| 47 | + expect(matcher.isMatch('ignored-dir/sub/file.txt')).toBeFalse(); |
| 48 | + expect(matcher.isMatch('ignored-dir-2/file.txt')).toBeFalse(); |
| 49 | + expect(matcher.isMatch('.gitkeep')).toBeFalse(); |
| 50 | + expect(matcher.isMatch('nested/.DS_Store')).toBeFalse(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not throw when glob is an empty string', () => { |
| 54 | + const matchers = createAssetMatchers([ |
| 55 | + { |
| 56 | + glob: '', |
| 57 | + input: `${tempRoot}/assets`, |
| 58 | + output: 'assets', |
| 59 | + }, |
| 60 | + ]); |
| 61 | + |
| 62 | + expect(matchers).toHaveSize(1); |
| 63 | + expect(matchers[0].isMatch('any.txt')).toBeFalse(); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('collectAssetsToEmit', () => { |
| 68 | + it('should deduplicate resolved assets by destination path with later patterns overriding earlier ones', async () => { |
| 69 | + const defaultDir = `${tempRoot}/lib`; |
| 70 | + const customDir = `${tempRoot}/docs`; |
| 71 | + await mkdir(defaultDir, { recursive: true }); |
| 72 | + await mkdir(customDir, { recursive: true }); |
| 73 | + |
| 74 | + const defaultReadme = `${defaultDir}/README.md`; |
| 75 | + const customReadme = `${customDir}/README.md`; |
| 76 | + await writeFile(defaultReadme, '# Default'); |
| 77 | + await writeFile(customReadme, '# Custom'); |
| 78 | + |
| 79 | + const assets = [ |
| 80 | + { glob: 'README.md', input: defaultDir, output: '.' }, |
| 81 | + { glob: 'README.md', input: customDir, output: '.' }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const initialResult = await collectAssetsToEmit(assets, tempRoot, new Map()); |
| 85 | + expect(initialResult.filesToEmit).toEqual([ |
| 86 | + { |
| 87 | + origin: 'disk', |
| 88 | + source: customReadme, |
| 89 | + path: 'README.md', |
| 90 | + }, |
| 91 | + ]); |
| 92 | + |
| 93 | + // Watch mode: even if customReadme appears before defaultReadme in modifiedFiles, |
| 94 | + // the later asset pattern (customReadme) should still take precedence. |
| 95 | + const watchResult = await collectAssetsToEmit( |
| 96 | + assets, |
| 97 | + tempRoot, |
| 98 | + new Map(), |
| 99 | + new Set([customReadme, defaultReadme]), |
| 100 | + ); |
| 101 | + expect(watchResult.filesToEmit).toEqual([ |
| 102 | + { |
| 103 | + origin: 'disk', |
| 104 | + source: customReadme, |
| 105 | + path: 'README.md', |
| 106 | + }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should respect followSymlinks in watch mode', async () => { |
| 111 | + const assetsDir = `${tempRoot}/assets`; |
| 112 | + await mkdir(assetsDir, { recursive: true }); |
| 113 | + |
| 114 | + const targetFile = `${tempRoot}/external.txt`; |
| 115 | + const symlinkFile = `${assetsDir}/linked.txt`; |
| 116 | + await writeFile(targetFile, 'external'); |
| 117 | + await symlink(targetFile, symlinkFile); |
| 118 | + |
| 119 | + const assetsNoFollow = [ |
| 120 | + { glob: '**/*.txt', input: assetsDir, output: 'out', followSymlinks: false }, |
| 121 | + ]; |
| 122 | + const noFollowResult = await collectAssetsToEmit( |
| 123 | + assetsNoFollow, |
| 124 | + tempRoot, |
| 125 | + new Map(), |
| 126 | + new Set([symlinkFile]), |
| 127 | + ); |
| 128 | + expect(noFollowResult.filesToEmit).toEqual([]); |
| 129 | + |
| 130 | + const assetsFollow = [ |
| 131 | + { glob: '**/*.txt', input: assetsDir, output: 'out', followSymlinks: true }, |
| 132 | + ]; |
| 133 | + const followResult = await collectAssetsToEmit( |
| 134 | + assetsFollow, |
| 135 | + tempRoot, |
| 136 | + new Map(), |
| 137 | + new Set([symlinkFile]), |
| 138 | + ); |
| 139 | + expect(followResult.filesToEmit).toEqual([ |
| 140 | + { |
| 141 | + origin: 'disk', |
| 142 | + source: symlinkFile, |
| 143 | + path: 'out/linked.txt', |
| 144 | + }, |
| 145 | + ]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should queue nested emitted assets for deletion when a deleted directory is reported in watch mode', async () => { |
| 149 | + const assetsDir = `${tempRoot}/assets`; |
| 150 | + const subDir = `${assetsDir}/sub`; |
| 151 | + const keepDir = `${assetsDir}/keep-dir`; |
| 152 | + await mkdir(keepDir, { recursive: true }); |
| 153 | + |
| 154 | + const nestedFileA = `${subDir}/a.txt`; |
| 155 | + const nestedFileB = `${subDir}/deep/b.txt`; |
| 156 | + const keepFile = `${keepDir}/keep.txt`; |
| 157 | + await writeFile(keepFile, 'keep'); |
| 158 | + |
| 159 | + const emittedAssets = new Map<string, Set<string>>([ |
| 160 | + [nestedFileA, new Set(['out/sub/a.txt'])], |
| 161 | + [nestedFileB, new Set(['out/sub/deep/b.txt'])], |
| 162 | + [keepFile, new Set(['out/keep-dir/keep.txt'])], |
| 163 | + ]); |
| 164 | + |
| 165 | + const assets = [{ glob: '**/*.txt', input: assetsDir, output: 'out' }]; |
| 166 | + |
| 167 | + // subDir does not exist on disk; keepDir still exists on disk |
| 168 | + const result = await collectAssetsToEmit( |
| 169 | + assets, |
| 170 | + tempRoot, |
| 171 | + emittedAssets, |
| 172 | + new Set([subDir, keepDir]), |
| 173 | + ); |
| 174 | + |
| 175 | + expect(result.filesToEmit).toEqual([]); |
| 176 | + expect(result.filesToDelete).toEqual([nestedFileA, nestedFileB]); |
| 177 | + }); |
| 178 | + }); |
| 179 | +}); |
0 commit comments