|
| 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 { Target } from '@angular-devkit/architect'; |
| 10 | +import { logging } from '@angular-devkit/core'; |
| 11 | +import type { ArgumentsCamelCase, Argv } from 'yargs'; |
| 12 | +import { ArchitectBaseCommandModule } from './architect-base-command-module'; |
| 13 | +import { CommandModuleError, Options, OtherOptions } from './command-module'; |
| 14 | +import { CommandContext } from './definitions'; |
| 15 | +import { Option } from './utilities/json-schema'; |
| 16 | + |
| 17 | +class TestArchitectCommand extends ArchitectBaseCommandModule<{}> { |
| 18 | + override readonly command = 'test'; |
| 19 | + override readonly describe = 'Test architect command'; |
| 20 | + override readonly shouldReportAnalytics = false; |
| 21 | + |
| 22 | + builderOptions: Option[] = []; |
| 23 | + builderName = '@angular/build:unit-test'; |
| 24 | + scheduledOptions: Record<string, unknown> | undefined; |
| 25 | + |
| 26 | + override builder(argv: Argv): Argv { |
| 27 | + return argv; |
| 28 | + } |
| 29 | + |
| 30 | + override async run(options: Options<{}> & OtherOptions): Promise<number> { |
| 31 | + return this.runSingleTarget({ project: 'app', target: 'test' }, options); |
| 32 | + } |
| 33 | + |
| 34 | + protected override getArchitectHost() { |
| 35 | + return { |
| 36 | + getBuilderNameForTarget: async (_target: Target) => this.builderName, |
| 37 | + } as never; |
| 38 | + } |
| 39 | + |
| 40 | + protected override async getOptionsForBuilder(_builderConf: string): Promise<Option[]> { |
| 41 | + return this.builderOptions; |
| 42 | + } |
| 43 | + |
| 44 | + protected override getArchitect() { |
| 45 | + return { |
| 46 | + scheduleTarget: async (_target: Target, options: Record<string, unknown>) => { |
| 47 | + this.scheduledOptions = options; |
| 48 | + |
| 49 | + return { |
| 50 | + lastOutput: Promise.resolve({ success: true }), |
| 51 | + stop: async () => {}, |
| 52 | + }; |
| 53 | + }, |
| 54 | + } as never; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function createCommandContext(): CommandContext { |
| 59 | + return { |
| 60 | + currentDirectory: '/workspace', |
| 61 | + root: '/workspace', |
| 62 | + logger: new logging.NullLogger(), |
| 63 | + args: { |
| 64 | + positional: [], |
| 65 | + options: { |
| 66 | + help: false, |
| 67 | + jsonHelp: false, |
| 68 | + getYargsCompletions: false, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } as unknown as CommandContext; |
| 72 | +} |
| 73 | + |
| 74 | +describe('ArchitectBaseCommandModule passthrough arguments', () => { |
| 75 | + it('forwards arguments after "--" to the x-passthrough option', async () => { |
| 76 | + const command = new TestArchitectCommand(createCommandContext()); |
| 77 | + command.builderOptions = [ |
| 78 | + { |
| 79 | + name: 'runnerArgs', |
| 80 | + description: 'Runner args', |
| 81 | + type: 'array', |
| 82 | + passthrough: true, |
| 83 | + }, |
| 84 | + ]; |
| 85 | + |
| 86 | + const args = { |
| 87 | + _: ['test'], |
| 88 | + $0: 'ng', |
| 89 | + '--': ['--update', '--bail=1'], |
| 90 | + watch: false, |
| 91 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 92 | + |
| 93 | + await command.handler(args); |
| 94 | + |
| 95 | + expect(command.scheduledOptions).toEqual({ |
| 96 | + watch: false, |
| 97 | + runnerArgs: ['--update', '--bail=1'], |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('concatenates explicit option values before "--" passthrough arguments', async () => { |
| 102 | + const command = new TestArchitectCommand(createCommandContext()); |
| 103 | + command.builderOptions = [ |
| 104 | + { |
| 105 | + name: 'runnerArgs', |
| 106 | + description: 'Runner args', |
| 107 | + type: 'array', |
| 108 | + passthrough: true, |
| 109 | + }, |
| 110 | + ]; |
| 111 | + |
| 112 | + const args = { |
| 113 | + _: ['test'], |
| 114 | + $0: 'ng', |
| 115 | + 'runner-args': ['--shard=1/2'], |
| 116 | + '--': ['--update'], |
| 117 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 118 | + |
| 119 | + await command.handler(args); |
| 120 | + |
| 121 | + expect(command.scheduledOptions).toEqual({ |
| 122 | + runnerArgs: ['--shard=1/2', '--update'], |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('rejects "--" arguments when the target builder has no x-passthrough option', async () => { |
| 127 | + const command = new TestArchitectCommand(createCommandContext()); |
| 128 | + command.builderName = '@angular/build:application'; |
| 129 | + command.builderOptions = [ |
| 130 | + { |
| 131 | + name: 'watch', |
| 132 | + description: 'Watch', |
| 133 | + type: 'boolean', |
| 134 | + }, |
| 135 | + ]; |
| 136 | + |
| 137 | + const args = { |
| 138 | + _: ['build'], |
| 139 | + $0: 'ng', |
| 140 | + '--': ['--update'], |
| 141 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 142 | + |
| 143 | + await expectAsync(command.handler(args)).toBeRejectedWithError( |
| 144 | + CommandModuleError, |
| 145 | + "The '@angular/build:application' builder does not accept arguments after '--'.", |
| 146 | + ); |
| 147 | + expect(command.scheduledOptions).toBeUndefined(); |
| 148 | + }); |
| 149 | + |
| 150 | + it('rejects "--" arguments when the target builder defines multiple x-passthrough options', async () => { |
| 151 | + const command = new TestArchitectCommand(createCommandContext()); |
| 152 | + command.builderOptions = [ |
| 153 | + { |
| 154 | + name: 'runnerArgs', |
| 155 | + description: 'Runner args', |
| 156 | + type: 'array', |
| 157 | + passthrough: true, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: 'extraArgs', |
| 161 | + description: 'Extra args', |
| 162 | + type: 'array', |
| 163 | + passthrough: true, |
| 164 | + }, |
| 165 | + ]; |
| 166 | + |
| 167 | + const args = { |
| 168 | + _: ['test'], |
| 169 | + $0: 'ng', |
| 170 | + '--': ['--update'], |
| 171 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 172 | + |
| 173 | + await expectAsync(command.handler(args)).toBeRejectedWithError( |
| 174 | + CommandModuleError, |
| 175 | + "The '@angular/build:unit-test' builder schema defines multiple options with 'x-passthrough'.", |
| 176 | + ); |
| 177 | + expect(command.scheduledOptions).toBeUndefined(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments