|
| 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 * as fs from 'node:fs'; |
| 10 | +import * as os from 'node:os'; |
| 11 | +import * as path from 'node:path'; |
| 12 | +import { resolveAssets } from './resolve-assets'; |
| 13 | + |
| 14 | +describe('resolveAssets', () => { |
| 15 | + let tempDir: string; |
| 16 | + let workspaceRoot: string; |
| 17 | + let outsideDir: string; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + tempDir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-assets-'))); |
| 21 | + |
| 22 | + workspaceRoot = path.join(tempDir, 'workspace'); |
| 23 | + fs.mkdirSync(path.join(workspaceRoot, 'public'), { recursive: true }); |
| 24 | + fs.writeFileSync(path.join(workspaceRoot, 'public', 'favicon.ico'), 'icon'); |
| 25 | + |
| 26 | + outsideDir = path.join(tempDir, 'outside'); |
| 27 | + fs.mkdirSync(outsideDir, { recursive: true }); |
| 28 | + fs.writeFileSync(path.join(outsideDir, 'secret.txt'), 'secret'); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 33 | + }); |
| 34 | + |
| 35 | + const publicAssets = (followSymlinks?: boolean) => [ |
| 36 | + { glob: '**/*', input: 'public', output: '.', followSymlinks }, |
| 37 | + ]; |
| 38 | + |
| 39 | + it('resolves the files within the input directory', async () => { |
| 40 | + const assets = await resolveAssets(publicAssets(), workspaceRoot); |
| 41 | + |
| 42 | + expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not follow a directory symlink by default', async () => { |
| 46 | + fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public', 'docs'), 'junction'); |
| 47 | + |
| 48 | + const assets = await resolveAssets(publicAssets(), workspaceRoot); |
| 49 | + |
| 50 | + expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not follow a file symlink by default', async () => { |
| 54 | + fs.symlinkSync( |
| 55 | + path.join(outsideDir, 'secret.txt'), |
| 56 | + path.join(workspaceRoot, 'public', 'notes.txt'), |
| 57 | + ); |
| 58 | + |
| 59 | + const assets = await resolveAssets(publicAssets(), workspaceRoot); |
| 60 | + |
| 61 | + expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']); |
| 62 | + }); |
| 63 | + |
| 64 | + it('follows a directory symlink that stays within the workspace root when enabled', async () => { |
| 65 | + fs.mkdirSync(path.join(workspaceRoot, 'shared')); |
| 66 | + fs.writeFileSync(path.join(workspaceRoot, 'shared', 'logo.svg'), '<svg></svg>'); |
| 67 | + fs.symlinkSync( |
| 68 | + path.join(workspaceRoot, 'shared'), |
| 69 | + path.join(workspaceRoot, 'public', 'branding'), |
| 70 | + 'junction', |
| 71 | + ); |
| 72 | + |
| 73 | + const assets = await resolveAssets(publicAssets(true), workspaceRoot); |
| 74 | + |
| 75 | + expect(assets.map(({ destination }) => destination).sort()).toEqual([ |
| 76 | + path.join('branding', 'logo.svg'), |
| 77 | + 'favicon.ico', |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it('fails when the input directory is a symlink outside the workspace root', async () => { |
| 82 | + fs.rmSync(path.join(workspaceRoot, 'public'), { recursive: true }); |
| 83 | + fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public'), 'junction'); |
| 84 | + |
| 85 | + await expectAsync(resolveAssets(publicAssets(), workspaceRoot)).toBeRejectedWithError( |
| 86 | + /asset path must be within the workspace root/, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('fails when a followed symlink resolves outside the workspace root', async () => { |
| 91 | + fs.symlinkSync(outsideDir, path.join(workspaceRoot, 'public', 'docs'), 'junction'); |
| 92 | + |
| 93 | + await expectAsync(resolveAssets(publicAssets(true), workspaceRoot)).toBeRejectedWithError( |
| 94 | + /asset path must be within the workspace root/, |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('resolves an input directory that does not exist', async () => { |
| 99 | + const assets = await resolveAssets( |
| 100 | + [{ glob: '**/*', input: 'not-created-yet', output: '.' }], |
| 101 | + workspaceRoot, |
| 102 | + ); |
| 103 | + |
| 104 | + expect(assets).toEqual([]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('resolves a missing input directory when the workspace root is reached through a symlink', async () => { |
| 108 | + const linkedRoot = path.join(tempDir, 'linked-workspace'); |
| 109 | + fs.symlinkSync(workspaceRoot, linkedRoot, 'junction'); |
| 110 | + |
| 111 | + const assets = await resolveAssets( |
| 112 | + [{ glob: '**/*', input: 'not-created-yet', output: '.' }], |
| 113 | + linkedRoot, |
| 114 | + ); |
| 115 | + |
| 116 | + expect(assets).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + it('resolves assets when the workspace root itself is reached through a symlink', async () => { |
| 120 | + const linkedRoot = path.join(tempDir, 'linked-workspace'); |
| 121 | + fs.symlinkSync(workspaceRoot, linkedRoot, 'junction'); |
| 122 | + |
| 123 | + const assets = await resolveAssets(publicAssets(), linkedRoot); |
| 124 | + |
| 125 | + expect(assets.map(({ destination }) => destination)).toEqual(['favicon.ico']); |
| 126 | + }); |
| 127 | +}); |
0 commit comments