|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { Architect } from '@angular-devkit/architect/src/index2'; |
| 10 | +import { normalize, virtualFs } from '@angular-devkit/core'; |
| 11 | +import { toArray } from 'rxjs/operators'; |
| 12 | +import { BrowserBuilderOutput } from "../../src/browser/index2"; |
| 13 | +import { createArchitect, host } from '../utils'; |
| 14 | + |
| 15 | + |
| 16 | +describe('Browser Builder assets', () => { |
| 17 | + const targetSpec = { project: 'app', target: 'build' }; |
| 18 | + let architect: Architect; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + await host.initialize().toPromise(); |
| 22 | + architect = (await createArchitect(host.root())).architect; |
| 23 | + }); |
| 24 | + afterEach(async () => host.restore().toPromise()); |
| 25 | + |
| 26 | + it('works', async () => { |
| 27 | + const assets: { [path: string]: string } = { |
| 28 | + './src/folder/.gitkeep': '', |
| 29 | + './src/string-file-asset.txt': 'string-file-asset.txt', |
| 30 | + './src/string-folder-asset/file.txt': 'string-folder-asset.txt', |
| 31 | + './src/glob-asset.txt': 'glob-asset.txt', |
| 32 | + './src/folder/folder-asset.txt': 'folder-asset.txt', |
| 33 | + './src/output-asset.txt': 'output-asset.txt', |
| 34 | + }; |
| 35 | + const matches: { [path: string]: string } = { |
| 36 | + './dist/string-file-asset.txt': 'string-file-asset.txt', |
| 37 | + './dist/string-folder-asset/file.txt': 'string-folder-asset.txt', |
| 38 | + './dist/glob-asset.txt': 'glob-asset.txt', |
| 39 | + './dist/folder/folder-asset.txt': 'folder-asset.txt', |
| 40 | + './dist/output-folder/output-asset.txt': 'output-asset.txt', |
| 41 | + }; |
| 42 | + host.writeMultipleFiles(assets); |
| 43 | + |
| 44 | + const overrides = { |
| 45 | + assets: [ |
| 46 | + 'src/string-file-asset.txt', |
| 47 | + 'src/string-folder-asset', |
| 48 | + { glob: 'glob-asset.txt', input: 'src/', output: '/' }, |
| 49 | + { glob: 'glob-asset.txt', input: 'src/', output: '/' }, |
| 50 | + { glob: 'output-asset.txt', input: 'src/', output: '/output-folder' }, |
| 51 | + { glob: '**/*', input: 'src/folder', output: '/folder' }, |
| 52 | + ], |
| 53 | + }; |
| 54 | + |
| 55 | + const run = await architect.scheduleTarget(targetSpec, overrides); |
| 56 | + const output = await run.result as BrowserBuilderOutput; |
| 57 | + expect(output.success).toBe(true); |
| 58 | + |
| 59 | + // Assets we expect should be there. |
| 60 | + Object.keys(matches).forEach(fileName => { |
| 61 | + const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName))); |
| 62 | + expect(content).toMatch(matches[fileName]); |
| 63 | + }); |
| 64 | + // .gitkeep should not be there. |
| 65 | + expect(host.scopedSync().exists(normalize('./dist/folder/.gitkeep'))).toBe(false); |
| 66 | + |
| 67 | + await run.stop(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('works with ignored patterns', async () => { |
| 71 | + const assets: { [path: string]: string } = { |
| 72 | + './src/folder/.gitkeep': '', |
| 73 | + './src/folder/asset-ignored.txt': '', |
| 74 | + './src/folder/asset.txt': '', |
| 75 | + }; |
| 76 | + |
| 77 | + host.writeMultipleFiles(assets); |
| 78 | + |
| 79 | + const overrides = { |
| 80 | + assets: [ |
| 81 | + { |
| 82 | + glob: '**/*', |
| 83 | + ignore: ['asset-ignored.txt'], |
| 84 | + input: 'src/folder', |
| 85 | + output: '/folder', |
| 86 | + }, |
| 87 | + ], |
| 88 | + }; |
| 89 | + |
| 90 | + const run = await architect.scheduleTarget(targetSpec, overrides); |
| 91 | + const output = await run.result as BrowserBuilderOutput; |
| 92 | + expect(output.success).toBe(true); |
| 93 | + |
| 94 | + expect(host.scopedSync().exists(normalize('./dist/folder/asset.txt'))).toBe(true); |
| 95 | + expect(host.scopedSync().exists(normalize('./dist/folder/asset-ignored.txt'))).toBe(false); |
| 96 | + expect(host.scopedSync().exists(normalize('./dist/folder/.gitkeep'))).toBe(false); |
| 97 | + |
| 98 | + await run.stop(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('fails with non-absolute output path', async () => { |
| 102 | + const assets: { [path: string]: string } = { |
| 103 | + './node_modules/some-package/node_modules-asset.txt': 'node_modules-asset.txt', |
| 104 | + }; |
| 105 | + host.writeMultipleFiles(assets); |
| 106 | + const overrides = { |
| 107 | + assets: [{ |
| 108 | + glob: '**/*', input: '../node_modules/some-package/', output: '../temp', |
| 109 | + }], |
| 110 | + }; |
| 111 | + |
| 112 | + const run = await architect.scheduleTarget(targetSpec, overrides); |
| 113 | + try { |
| 114 | + await run.result; |
| 115 | + expect('THE ABOVE LINE SHOULD THROW').toBe(''); |
| 116 | + } catch {} |
| 117 | + |
| 118 | + // The node_modules folder must be deleted, otherwise code that tries to find the |
| 119 | + // node_modules folder will hit this one and can fail. |
| 120 | + host.scopedSync().delete(normalize('./node_modules')); |
| 121 | + |
| 122 | + await run.stop(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('fails with non-source root input path', async () => { |
| 126 | + const assets: { [path: string]: string } = { |
| 127 | + './node_modules/some-package/node_modules-asset.txt': 'node_modules-asset.txt', |
| 128 | + }; |
| 129 | + host.writeMultipleFiles(assets); |
| 130 | + const overrides = { |
| 131 | + assets: ['not-source-root/file.txt'], |
| 132 | + }; |
| 133 | + |
| 134 | + const run = await architect.scheduleTarget(targetSpec, overrides); |
| 135 | + try { |
| 136 | + await run.result; |
| 137 | + expect('THE ABOVE LINE SHOULD THROW').toBe(''); |
| 138 | + } catch {} |
| 139 | + |
| 140 | + // The node_modules folder must be deleted, otherwise code that tries to find the |
| 141 | + // node_modules folder will hit this one and can fail. |
| 142 | + host.scopedSync().delete(normalize('./node_modules')); |
| 143 | + |
| 144 | + await run.stop(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('still builds with empty asset array', async () => { |
| 148 | + const overrides = { |
| 149 | + assets: [], |
| 150 | + }; |
| 151 | + |
| 152 | + const run = await architect.scheduleTarget(targetSpec, overrides); |
| 153 | + const events = await run.output.pipe(toArray()).toPromise(); |
| 154 | + expect(events.length).toBe(1); |
| 155 | + |
| 156 | + await run.stop(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments