|
| 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, writeFile } from 'node:fs/promises'; |
| 11 | +import path, { join } from 'node:path'; |
| 12 | +import { normalizeAssetPatterns } from './normalize-asset-patterns'; |
| 13 | +import { resolveAssets } from './resolve-assets'; |
| 14 | + |
| 15 | +describe('normalizeAssetPatterns', () => { |
| 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 = await mkdtemp(join(baseTmpDir, 'angular-cli-assets-test-')); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + if (tempRoot) { |
| 26 | + await rm(tempRoot, { recursive: true, force: true }); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('should return an empty array when given no asset patterns', () => { |
| 31 | + expect(normalizeAssetPatterns([], tempRoot, '', 'src')).toEqual([]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should normalize a string asset within sourceRoot relative to sourceRoot', async () => { |
| 35 | + await mkdir(join(tempRoot, 'src/assets'), { recursive: true }); |
| 36 | + await writeFile(join(tempRoot, 'src/assets/logo.png'), 'PNG'); |
| 37 | + |
| 38 | + const result = normalizeAssetPatterns( |
| 39 | + ['src/assets', 'src/assets/logo.png'], |
| 40 | + tempRoot, |
| 41 | + '', |
| 42 | + 'src', |
| 43 | + ); |
| 44 | + |
| 45 | + expect(result).toEqual([ |
| 46 | + { glob: '**/*', input: 'src/assets', output: 'assets' }, |
| 47 | + { glob: 'logo.png', input: 'src/assets', output: 'assets' }, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should normalize a string asset in a sibling directory sharing a prefix with sourceRoot (e.g. src-assets)', async () => { |
| 52 | + await mkdir(join(tempRoot, 'src-assets'), { recursive: true }); |
| 53 | + await writeFile(join(tempRoot, 'src-assets/logo.png'), 'PNG'); |
| 54 | + |
| 55 | + const result = normalizeAssetPatterns( |
| 56 | + ['src-assets', 'src-assets/logo.png'], |
| 57 | + tempRoot, |
| 58 | + '', |
| 59 | + 'src', |
| 60 | + ); |
| 61 | + |
| 62 | + expect(result).toEqual([ |
| 63 | + { glob: '**/*', input: 'src-assets', output: 'src-assets' }, |
| 64 | + { glob: 'logo.png', input: 'src-assets', output: 'src-assets' }, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should normalize a library string asset in a sibling directory sharing a prefix with projectSourceRoot', async () => { |
| 69 | + await mkdir(join(tempRoot, 'projects/lib/src-assets'), { recursive: true }); |
| 70 | + await writeFile(join(tempRoot, 'projects/lib/src-assets/logo.png'), 'PNG'); |
| 71 | + |
| 72 | + const result = normalizeAssetPatterns( |
| 73 | + ['projects/lib/src-assets', 'projects/lib/src-assets/logo.png'], |
| 74 | + tempRoot, |
| 75 | + 'projects/lib', |
| 76 | + 'projects/lib/src', |
| 77 | + ); |
| 78 | + |
| 79 | + expect(result).toEqual([ |
| 80 | + { |
| 81 | + glob: '**/*', |
| 82 | + input: path.normalize('projects/lib/src-assets'), |
| 83 | + output: 'src-assets', |
| 84 | + }, |
| 85 | + { |
| 86 | + glob: 'logo.png', |
| 87 | + input: path.normalize('projects/lib/src-assets'), |
| 88 | + output: 'src-assets', |
| 89 | + }, |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should normalize a string asset in a sibling directory sharing a prefix with projectRoot relative to workspaceRoot', async () => { |
| 94 | + await mkdir(join(tempRoot, 'projects/lib-shared'), { recursive: true }); |
| 95 | + await writeFile(join(tempRoot, 'projects/lib-shared/logo.png'), 'PNG'); |
| 96 | + |
| 97 | + const result = normalizeAssetPatterns( |
| 98 | + ['projects/lib-shared/logo.png'], |
| 99 | + tempRoot, |
| 100 | + 'projects/lib', |
| 101 | + 'projects/lib/src', |
| 102 | + ); |
| 103 | + |
| 104 | + expect(result).toEqual([ |
| 105 | + { |
| 106 | + glob: 'logo.png', |
| 107 | + input: path.normalize('projects/lib-shared'), |
| 108 | + output: path.normalize('projects/lib-shared'), |
| 109 | + }, |
| 110 | + ]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should throw when a string asset is in a sibling directory sharing a prefix with workspaceRoot', () => { |
| 114 | + const siblingDir = `../${path.basename(tempRoot)}-outside/logo.png`; |
| 115 | + |
| 116 | + expect(() => normalizeAssetPatterns([siblingDir], tempRoot, '', 'src')).toThrowError( |
| 117 | + /asset path must be within the workspace root/, |
| 118 | + ); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('resolveAssets', () => { |
| 123 | + let tempRoot: string; |
| 124 | + |
| 125 | + beforeEach(async () => { |
| 126 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 127 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 128 | + tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-resolve-assets-test-')); |
| 129 | + }); |
| 130 | + |
| 131 | + afterEach(async () => { |
| 132 | + if (tempRoot) { |
| 133 | + await rm(tempRoot, { recursive: true, force: true }); |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + it('should ignore both root and nested .gitkeep files by default', async () => { |
| 138 | + await mkdir(join(tempRoot, 'src/assets/nested/deep'), { recursive: true }); |
| 139 | + await writeFile(join(tempRoot, 'src/assets/.gitkeep'), ''); |
| 140 | + await writeFile(join(tempRoot, 'src/assets/nested/.gitkeep'), ''); |
| 141 | + await writeFile(join(tempRoot, 'src/assets/nested/deep/.gitkeep'), ''); |
| 142 | + await writeFile(join(tempRoot, 'src/assets/nested/deep/logo.png'), 'PNG'); |
| 143 | + |
| 144 | + const resolved = await resolveAssets( |
| 145 | + [{ glob: '**/*', input: 'src/assets', output: 'assets' }], |
| 146 | + tempRoot, |
| 147 | + ); |
| 148 | + |
| 149 | + expect(resolved).toEqual([ |
| 150 | + { |
| 151 | + source: join(tempRoot, 'src/assets/nested/deep/logo.png'), |
| 152 | + destination: join('assets', 'nested/deep/logo.png'), |
| 153 | + }, |
| 154 | + ]); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should throw when a resolved destination escapes the output root via ".." in glob', async () => { |
| 158 | + await mkdir(join(tempRoot, 'src'), { recursive: true }); |
| 159 | + await writeFile(join(tempRoot, 'outside.txt'), 'secret'); |
| 160 | + |
| 161 | + await expectAsync( |
| 162 | + resolveAssets([{ glob: '../outside.txt', input: 'src', output: '.' }], tempRoot), |
| 163 | + ).toBeRejectedWithError('An asset cannot be written to a location outside of the output path.'); |
| 164 | + |
| 165 | + await expectAsync( |
| 166 | + resolveAssets([{ glob: '../*.txt', input: 'src', output: '.' }], tempRoot), |
| 167 | + ).toBeRejectedWithError('An asset cannot be written to a location outside of the output path.'); |
| 168 | + }); |
| 169 | +}); |
0 commit comments