-
Notifications
You must be signed in to change notification settings - Fork 3
fix(cli_tools): Integration of help -h with MessageOutput
#92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
491be41
fix(cli_tools): Integration of `help -h` with `MessageOutput`
ab842e9
test(cli_tools): Behavior of `help -h` with `MessageOutput`
d3bb0cf
refactor(#92): Better error message if a test fails
60d5090
refactor(#92): Better readability
9b10c86
refactor(#92): Better robustness
f0f3719
refactor(#92): Clean Code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
packages/cli_tools/test/better_command_runner/help_command_usage_text_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import 'dart:async' show ZoneSpecification, runZoned; | ||
|
|
||
| import 'package:args/command_runner.dart' show CommandRunner, UsageException; | ||
| import 'package:cli_tools/better_command_runner.dart' | ||
| show BetterCommandRunner, MessageOutput; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() => _runTests(); | ||
|
|
||
| const _exeName = 'mock'; | ||
| const _exeDescription = 'A mock command to test `help -h` with MessageOutput.'; | ||
| const _moMockText = 'SUCCESS: `MessageOutput.usageLogger`'; | ||
| const _moExceptionText = 'SUCCESS: `MessageOutput.usageExceptionLogger`'; | ||
| const _invalidOption = '-z'; | ||
|
|
||
| CommandRunner _buildUpstreamRunner() => CommandRunner( | ||
| _exeName, | ||
| _exeDescription, | ||
| ); | ||
|
|
||
| BetterCommandRunner _buildBetterRunner() => BetterCommandRunner( | ||
| _exeName, | ||
| _exeDescription, | ||
| messageOutput: MessageOutput( | ||
| usageLogger: (final usage) { | ||
| print(_moMockText); | ||
| print(usage); | ||
| }, | ||
| usageExceptionLogger: (final exception) { | ||
| print(_moExceptionText); | ||
| print(exception.message); | ||
| print(exception.usage); | ||
| }, | ||
| ), | ||
| ); | ||
|
|
||
| void _runTests() { | ||
| group('Given a BetterCommandRunner with custom MessageOutput', () { | ||
| for (final args in [ | ||
| ['help', '-h'], | ||
| ['help', 'help'], | ||
| ['help', '-h', 'help'], | ||
| ['help', '-h', 'help', '-h'], | ||
| ['help', '-h', 'help', '-h', 'help'], | ||
| ['help', '-h', 'help', '-h', 'help', '-h'], | ||
| ]) { | ||
| _testsForValidHelpUsageRequests(args); | ||
| } | ||
| for (final args in [ | ||
| ['help', '-h', _invalidOption], | ||
| ['help', 'help', _invalidOption], | ||
| ['help', _invalidOption, 'help'], | ||
| ['help', 'help', '-h', _invalidOption], | ||
| ['help', _invalidOption, 'help', '-h', _invalidOption], | ||
| ]) { | ||
| _testsForInvalidHelpUsageRequests(args); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void _testsForValidHelpUsageRequests(final List<String> args) { | ||
| group('when $args is received', () { | ||
| final betterRunnerOutput = StringBuffer(); | ||
| final upstreamRunnerOutput = StringBuffer(); | ||
| late final Object? betterRunnerExitCode; | ||
| late final Object? upstreamRunnerExitCode; | ||
| setUpAll(() async { | ||
| betterRunnerExitCode = await runZoned( | ||
| () async => await _buildBetterRunner().run(args), | ||
| zoneSpecification: ZoneSpecification( | ||
| print: (final _, final __, final ___, final String line) { | ||
| betterRunnerOutput.writeln(line); | ||
| }), | ||
| ); | ||
| upstreamRunnerExitCode = await runZoned( | ||
| () async => await _buildUpstreamRunner().run(args), | ||
| zoneSpecification: ZoneSpecification( | ||
| print: (final _, final __, final ___, final String line) { | ||
| upstreamRunnerOutput.writeln(line); | ||
| }), | ||
| ); | ||
| }); | ||
| test('then MessageOutput is not bypassed', () { | ||
| expect( | ||
| betterRunnerOutput.toString(), | ||
| contains(_moMockText), | ||
| ); | ||
| }); | ||
| test('then it can subsume upstream HelpCommand output', () { | ||
| expect( | ||
| betterRunnerOutput.toString(), | ||
| stringContainsInOrder([ | ||
| _moMockText, | ||
| upstreamRunnerOutput.toString(), | ||
| ]), | ||
| ); | ||
| }); | ||
| test('then Exit Code (null) matches that of upstream HelpCommand', () { | ||
| expect(betterRunnerExitCode, equals(null)); | ||
| expect(betterRunnerExitCode, equals(upstreamRunnerExitCode)); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| void _testsForInvalidHelpUsageRequests(final List<String> args) { | ||
| group('when $args is received', () { | ||
| final betterRunnerOutput = StringBuffer(); | ||
| final upstreamRunnerOutput = StringBuffer(); | ||
| late final UsageException? betterRunnerException; | ||
| late final UsageException? upstreamRunnerException; | ||
| setUpAll(() async { | ||
| try { | ||
| await runZoned( | ||
| () async => await _buildBetterRunner().run(args), | ||
| zoneSpecification: ZoneSpecification( | ||
| print: (final _, final __, final ___, final String line) { | ||
| betterRunnerOutput.writeln(line); | ||
| }, | ||
| ), | ||
| ); | ||
| } on UsageException catch (e) { | ||
| betterRunnerException = e; | ||
| } | ||
| try { | ||
| await runZoned( | ||
| () async => await _buildUpstreamRunner().run(args), | ||
| zoneSpecification: ZoneSpecification( | ||
| print: (final _, final __, final ___, final String line) { | ||
| upstreamRunnerOutput.writeln(line); | ||
| }, | ||
| ), | ||
| ); | ||
| } on UsageException catch (e) { | ||
| upstreamRunnerException = e; | ||
| } | ||
| }); | ||
| test('then it throws UsageException exactly like CommandRunner', () { | ||
| expect(betterRunnerException, isA<UsageException>()); | ||
| expect(upstreamRunnerException, isA<UsageException>()); | ||
| expect( | ||
| betterRunnerException!.message, | ||
| equals(upstreamRunnerException!.message), | ||
| ); | ||
| expect( | ||
| betterRunnerException!.usage, | ||
| equals(upstreamRunnerException!.usage), | ||
| ); | ||
| }); | ||
| test('then MessageOutput is not bypassed', () { | ||
| expect( | ||
| betterRunnerOutput.toString(), | ||
| stringContainsInOrder( | ||
| [ | ||
| _moExceptionText, | ||
| betterRunnerException!.message, | ||
| betterRunnerException!.usage, | ||
| ], | ||
| ), | ||
| ); | ||
| }); | ||
| test('then it can subsume upstream HelpCommand output', () { | ||
| expect( | ||
| betterRunnerOutput.toString(), | ||
| contains(upstreamRunnerOutput.toString()), | ||
| ); | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.