-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add print completion-script command #128
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
Open
unicoderbot
wants to merge
4
commits into
main
Choose a base branch
from
vgv-ai-bot/issue-58
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,6 @@ build/ | |
| pubspec.lock | ||
|
|
||
| coverage/ | ||
|
|
||
| # VM service artifacts created by the test runner | ||
| *.dart.vm.json | ||
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
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
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export 'handle_completion_command.dart'; | ||
| export 'install_completion_files_command.dart'; | ||
| export 'print_completion_script_command.dart'; | ||
| export 'uninstall_completion_files_command.dart'; |
48 changes: 48 additions & 0 deletions
48
lib/src/command_runner/commands/print_completion_script_command.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,48 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:args/command_runner.dart'; | ||
| import 'package:cli_completion/cli_completion.dart'; | ||
|
|
||
| /// {@template print_completion_script_command} | ||
| /// A [Command] added by [CompletionCommandRunner] only when auto installation | ||
| /// is disabled that prints the completion script for the current shell to | ||
| /// stdout. | ||
| /// | ||
| /// It allows users to install the completion script manually by piping the | ||
| /// output into their shell configuration file, for example: | ||
| /// ```sh | ||
| /// my_cli completion-script >> ~/.zshrc | ||
| /// ``` | ||
| /// | ||
| /// This mirrors the approach used by other CLIs such as npm and the GitHub CLI. | ||
| /// | ||
| /// Unlike the other completion commands, this command is intentionally left | ||
| /// visible in `--help` (it does not override [hidden]) so that users can | ||
| /// discover it. | ||
| /// {@endtemplate} | ||
| class PrintCompletionScriptCommand<T> extends Command<T> { | ||
| /// {@macro print_completion_script_command} | ||
| PrintCompletionScriptCommand(); | ||
|
|
||
| @override | ||
| String get description { | ||
| return 'Prints the completion script for the current shell to stdout.'; | ||
| } | ||
|
|
||
| /// The string that the user can call to print the completion script. | ||
| static const commandName = 'completion-script'; | ||
|
|
||
| @override | ||
| String get name => commandName; | ||
|
|
||
| @override | ||
| CompletionCommandRunner<T> get runner { | ||
| return super.runner! as CompletionCommandRunner<T>; | ||
| } | ||
|
|
||
| @override | ||
| FutureOr<T>? run() { | ||
| runner.printCompletionScript(); | ||
| return null; | ||
| } | ||
| } | ||
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
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
111 changes: 111 additions & 0 deletions
111
test/src/command_runner/commands/print_completion_script_command_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,111 @@ | ||
| import 'package:cli_completion/cli_completion.dart'; | ||
| import 'package:cli_completion/installer.dart'; | ||
| import 'package:mason_logger/mason_logger.dart'; | ||
| import 'package:mocktail/mocktail.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| class _MockLogger extends Mock implements Logger {} | ||
|
|
||
| class _MockCompletionInstallation extends Mock | ||
| implements CompletionInstallation {} | ||
|
|
||
| class _TestCompletionCommandRunner extends CompletionCommandRunner<int> { | ||
| _TestCompletionCommandRunner() : super('test', 'Test command runner'); | ||
|
|
||
| @override | ||
| bool get enableAutoInstall => false; | ||
|
|
||
| @override | ||
| // Override acceptable for test files | ||
| // ignore: overridden_fields | ||
| final Logger completionLogger = _MockLogger(); | ||
|
|
||
| @override | ||
| // Override acceptable for test files | ||
| // ignore: overridden_fields | ||
| final Logger completionInstallationLogger = _MockLogger(); | ||
|
|
||
| @override | ||
| final CompletionInstallation completionInstallation = | ||
| _MockCompletionInstallation(); | ||
| } | ||
|
|
||
| void main() { | ||
| group('PrintCompletionScriptCommand', () { | ||
| late _TestCompletionCommandRunner commandRunner; | ||
|
|
||
| setUp(() { | ||
| commandRunner = _TestCompletionCommandRunner(); | ||
| }); | ||
|
|
||
| test('can be instantiated', () { | ||
| expect(PrintCompletionScriptCommand<int>(), isNotNull); | ||
| }); | ||
|
|
||
| test('is not hidden', () { | ||
| expect(PrintCompletionScriptCommand<int>().hidden, isFalse); | ||
| }); | ||
|
|
||
| test('description', () { | ||
| expect( | ||
| PrintCompletionScriptCommand<int>().description, | ||
| 'Prints the completion script for the current shell to stdout.', | ||
| ); | ||
| }); | ||
|
|
||
| group('completion-script', () { | ||
| test('prints the completion script to stdout', () async { | ||
| when( | ||
| () => commandRunner.completionInstallation.completionScriptFor( | ||
| commandRunner.executableName, | ||
| ), | ||
| ).thenReturn('some completion script'); | ||
|
|
||
| await commandRunner.run(['completion-script']); | ||
|
|
||
| verify( | ||
| () => commandRunner.completionLogger.info('some completion script'), | ||
| ).called(1); | ||
| }); | ||
|
|
||
| test( | ||
| 'logs a warning when it throws a CompletionInstallationException', | ||
| () async { | ||
| when( | ||
| () => commandRunner.completionInstallation.completionScriptFor( | ||
| commandRunner.executableName, | ||
| ), | ||
| ).thenThrow( | ||
| CompletionInstallationException( | ||
| message: 'oops', | ||
| rootCommand: 'test', | ||
| ), | ||
| ); | ||
|
|
||
| await commandRunner.run(['completion-script']); | ||
|
|
||
| verify( | ||
| () => commandRunner.completionInstallationLogger.warn(any()), | ||
| ).called(1); | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| 'logs an error when an unknown exception happens', | ||
| () async { | ||
| when( | ||
| () => commandRunner.completionInstallation.completionScriptFor( | ||
| commandRunner.executableName, | ||
| ), | ||
| ).thenThrow(Exception('oops')); | ||
|
|
||
| await commandRunner.run(['completion-script']); | ||
|
|
||
| verify( | ||
| () => commandRunner.completionInstallationLogger.err(any()), | ||
| ).called(1); | ||
| }, | ||
| ); | ||
| }); | ||
| }); | ||
| } |
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
Oops, something went wrong.
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.