|
| 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 { logging } from '@angular-devkit/core'; |
| 10 | +import type { ArgumentsCamelCase, Argv } from 'yargs'; |
| 11 | +import { CommandModule, CommandModuleError, Options, OtherOptions } from './command-module'; |
| 12 | +import { CommandContext } from './definitions'; |
| 13 | + |
| 14 | +class UnsupportedPassthroughCommand extends CommandModule { |
| 15 | + override readonly command = 'test-cmd'; |
| 16 | + override readonly describe = 'Test command'; |
| 17 | + override readonly shouldReportAnalytics = false; |
| 18 | + receivedOptions: (Options<{}> & OtherOptions) | undefined; |
| 19 | + |
| 20 | + override builder(argv: Argv): Argv { |
| 21 | + return argv; |
| 22 | + } |
| 23 | + |
| 24 | + override run(options: Options<{}> & OtherOptions): number { |
| 25 | + this.receivedOptions = options; |
| 26 | + |
| 27 | + return 0; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class SupportedPassthroughCommand extends CommandModule { |
| 32 | + override readonly command = 'pass-cmd'; |
| 33 | + override readonly describe = 'Command supporting passthrough'; |
| 34 | + override readonly shouldReportAnalytics = false; |
| 35 | + protected override readonly acceptsPassthroughArgs = true; |
| 36 | + receivedOptions: (Options<{}> & OtherOptions) | undefined; |
| 37 | + receivedPassthroughArgs: readonly string[] | undefined; |
| 38 | + |
| 39 | + override builder(argv: Argv): Argv { |
| 40 | + return argv; |
| 41 | + } |
| 42 | + |
| 43 | + override run(options: Options<{}> & OtherOptions): number { |
| 44 | + this.receivedOptions = options; |
| 45 | + this.receivedPassthroughArgs = this.passthroughArgs; |
| 46 | + |
| 47 | + return 0; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function createCommandContext(): CommandContext { |
| 52 | + return { |
| 53 | + currentDirectory: '/workspace', |
| 54 | + root: '/workspace', |
| 55 | + logger: new logging.NullLogger(), |
| 56 | + args: { |
| 57 | + positional: [], |
| 58 | + options: { |
| 59 | + help: false, |
| 60 | + jsonHelp: false, |
| 61 | + getYargsCompletions: false, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } as unknown as CommandContext; |
| 65 | +} |
| 66 | + |
| 67 | +describe('CommandModule passthrough arguments', () => { |
| 68 | + it('rejects arguments after "--" when command does not support passthrough', async () => { |
| 69 | + const command = new UnsupportedPassthroughCommand(createCommandContext()); |
| 70 | + const args = { |
| 71 | + _: ['test-cmd'], |
| 72 | + $0: 'ng', |
| 73 | + '--': ['--update'], |
| 74 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 75 | + |
| 76 | + await expectAsync(command.handler(args)).toBeRejectedWithError( |
| 77 | + CommandModuleError, |
| 78 | + 'Arguments following "--" are not supported by the "test-cmd" command.', |
| 79 | + ); |
| 80 | + expect(command.receivedOptions).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('allows empty "--" array on commands that do not support passthrough', async () => { |
| 84 | + const command = new UnsupportedPassthroughCommand(createCommandContext()); |
| 85 | + const args = { |
| 86 | + _: ['test-cmd'], |
| 87 | + $0: 'ng', |
| 88 | + '--': [], |
| 89 | + watch: false, |
| 90 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 91 | + |
| 92 | + await command.handler(args); |
| 93 | + |
| 94 | + expect(command.receivedOptions).toEqual({ watch: false }); |
| 95 | + expect('--' in (command.receivedOptions ?? {})).toBeFalse(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('forwards "--" arguments via passthroughArgs and omits "--" from options when supported', async () => { |
| 99 | + const command = new SupportedPassthroughCommand(createCommandContext()); |
| 100 | + const args = { |
| 101 | + _: ['pass-cmd'], |
| 102 | + $0: 'ng', |
| 103 | + '--': ['--update', '--reporter=verbose'], |
| 104 | + watch: false, |
| 105 | + } as unknown as ArgumentsCamelCase<{}> & OtherOptions; |
| 106 | + |
| 107 | + await command.handler(args); |
| 108 | + |
| 109 | + expect(command.receivedPassthroughArgs).toEqual(['--update', '--reporter=verbose']); |
| 110 | + expect(command.receivedOptions).toEqual({ watch: false }); |
| 111 | + expect('--' in (command.receivedOptions ?? {})).toBeFalse(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments