diff --git a/packages/config/test/command/command_line_run_test.dart b/packages/config/test/command/command_line_run_test.dart index 4c9038f..bdcd152 100644 --- a/packages/config/test/command/command_line_run_test.dart +++ b/packages/config/test/command/command_line_run_test.dart @@ -93,6 +93,77 @@ void main() { }); }); + group( + 'when an unrecognized subcommand is followed by an invalid option', + () { + late UsageException? error; + + setUp(() async { + error = await _captureUsageError( + () => runner.run(['wrong-subcommand', '--wrong-option']), + ); + }); + + test('then the subcommand error is shown before the option error', () { + expect( + error?.message, + 'tool: Unknown subcommand: "wrong-subcommand"\n' + 'tool: Unknown option: --wrong-option.', + ); + }); + + test('then no command runs', () { + expect(trace, isEmpty); + }); + }, + ); + + group( + 'when an invalid option precedes an unrecognized subcommand', + () { + late UsageException? error; + + setUp(() async { + error = await _captureUsageError( + () => runner.run(['--wrong-option', 'wrong-subcommand']), + ); + }); + + test('then both errors are shown', () { + expect( + error?.message, + 'tool: Unknown option: --wrong-option.\n' + 'tool: Unknown subcommand: "wrong-subcommand"', + ); + }); + + test('then no command runs', () { + expect(trace, isEmpty); + }); + }, + ); + + group( + 'when a misspelled subcommand is followed by an unknown short option', + () { + late UsageException? error; + + setUp(() async { + error = await _captureUsageError( + () => runner.run(['biuld', '-x']), + ); + }); + + test('then the subcommand error is shown before the option error', () { + expect( + error?.message, + 'tool: Unknown subcommand: "biuld" (Did you mean build?)\n' + 'tool: Unknown option: -x.', + ); + }); + }, + ); + group('when the leaf command receives an extra positional argument', () { late UsageException? error; @@ -115,6 +186,58 @@ void main() { }); }); + group('Given a build command that accepts an output option', () { + late List trace; + late TracingRunner runner; + + setUp(() { + trace = []; + runner = TracingRunner( + trace, + usageOutput: noUsageErrorOutput, + subcommands: [ + RecordingCommand( + name: 'build', + description: 'Build the app', + trace: trace, + options: const [ + StringOption(argName: 'output'), + ], + ), + ], + ); + }); + + group( + 'when the subcommand is misspelled and that option is passed', + () { + late UsageException? error; + + setUp(() async { + error = await _captureUsageError( + () => runner.run(['biuld', '--output', 'app']), + ); + }); + + test( + 'then the subcommand error is shown before the option error', + () { + expect( + error?.message, + 'tool: Unknown subcommand: "biuld" (Did you mean build?)\n' + 'tool: Unknown option: --output.\n' + 'tool: Unknown subcommand: "app"', + ); + }, + ); + + test('then no command runs', () { + expect(trace, isEmpty); + }); + }, + ); + }); + group('Given a runner that can run as a leaf', () { late List trace; late TracingRunner runner; diff --git a/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart b/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart index 28bac23..6cddea3 100644 --- a/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart +++ b/packages/config/test/config/arg_parser/scan_and_parse_commands_test.dart @@ -220,6 +220,54 @@ void main() { expect(parse(['nope']).errors, ['Unknown subcommand: "nope"']); }); + test( + 'when an unknown subcommand is followed by an unknown option ' + 'then the subcommand error is recorded first', + () { + expect(parse(['nope', '--missing']).errors, [ + 'Unknown subcommand: "nope"', + 'Unknown option: --missing', + ]); + }, + ); + + test( + 'when an unknown subcommand is followed by an unknown short option ' + 'then both errors are recorded', + () { + expect(parse(['nope', '-q']).errors, [ + 'Unknown subcommand: "nope"', + 'Unknown option: -q', + ]); + }, + ); + + test( + 'when an unknown option is followed by an unknown subcommand ' + 'then the subcommand error is still recorded', + () { + expect(parse(['--missing', 'nope']).errors, [ + 'Unknown option: --missing', + 'Unknown subcommand: "nope"', + ]); + }, + ); + + test( + "when a misspelled nested command is followed by that command's option " + 'then the subcommand error is recorded before the option error', + () { + expect( + childResult(parse(['mid', 'leave', '--output', 'file'])).errors, + [ + 'Unknown subcommand: "leave" (Did you mean leaf?)', + 'Unknown option: --output', + 'Unknown subcommand: "file"', + ], + ); + }, + ); + test('when a nested word is unknown then the error is on that command', () { expect( childResult(parse(['mid', 'nope'])).errors,