Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions packages/config/test/command/command_line_run_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -115,6 +186,58 @@ void main() {
});
});

group('Given a build command that accepts an output option', () {
late List<String> 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<String> trace;
late TracingRunner runner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading