From ed5cf77900fe1f2363cf74ac87eb98dfec36e4de Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Sun, 31 Jan 2021 14:04:27 -0600 Subject: [PATCH] feat: create command scaffold (#5) --- README.md | 4 ++-- lib/src/command_runner.dart | 4 +++- lib/src/commands/commands.dart | 1 + lib/src/commands/create.dart | 26 ++++++++++++++++++++++++++ test/src/command_runner_test.dart | 8 ++++---- test/src/commands/create_test.dart | 30 ++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 lib/src/commands/commands.dart create mode 100644 lib/src/commands/create.dart create mode 100644 test/src/commands/create_test.dart diff --git a/README.md b/README.md index e0d27a58..39eced2c 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Create a new very good flutter application in seconds. See the complete list of commands and usage information. ```sh -🦄 A Very Good Commandline Interface +🦄 A Very Good Command Line Interface Usage: very_good [arguments] @@ -35,7 +35,7 @@ Global options: --version Print the current version. Available commands: - help Display help information for very_good. + create Creates a new very good flutter application in seconds. Run "very_good help " for more information about a command. ``` diff --git a/lib/src/command_runner.dart b/lib/src/command_runner.dart index 4385d786..b4238d59 100644 --- a/lib/src/command_runner.dart +++ b/lib/src/command_runner.dart @@ -2,6 +2,7 @@ import 'package:args/args.dart'; import 'package:args/command_runner.dart'; import 'package:io/io.dart'; import 'package:mason/mason.dart'; +import 'package:very_good_cli/src/commands/commands.dart'; import 'version.dart'; @@ -12,12 +13,13 @@ class VeryGoodCommandRunner extends CommandRunner { /// {@macro very_good_command_runner} VeryGoodCommandRunner({Logger logger}) : _logger = logger ?? Logger(), - super('very_good', '🦄 A Very Good Commandline Interface') { + super('very_good', '🦄 A Very Good Command Line Interface') { argParser.addFlag( 'version', negatable: false, help: 'Print the current version.', ); + addCommand(CreateCommand()); } final Logger _logger; diff --git a/lib/src/commands/commands.dart b/lib/src/commands/commands.dart new file mode 100644 index 00000000..7555cbba --- /dev/null +++ b/lib/src/commands/commands.dart @@ -0,0 +1 @@ +export 'create.dart'; diff --git a/lib/src/commands/create.dart b/lib/src/commands/create.dart new file mode 100644 index 00000000..671ce8db --- /dev/null +++ b/lib/src/commands/create.dart @@ -0,0 +1,26 @@ +import 'package:args/command_runner.dart'; +import 'package:io/io.dart'; +import 'package:mason/mason.dart'; + +/// {@template create_command} +/// `very_good create` command creates a new very good flutter app. +/// {@endtemplate} +class CreateCommand extends Command { + /// {@macro create_command} + CreateCommand({Logger logger}) : _logger = logger ?? Logger(); + + final Logger _logger; + + @override + final String description = + 'Creates a new very good flutter application in seconds.'; + + @override + final String name = 'create'; + + @override + Future run() async { + _logger.alert('Created a Very Good App! 🦄'); + return ExitCode.success.code; + } +} diff --git a/test/src/command_runner_test.dart b/test/src/command_runner_test.dart index b3c850c7..db476b09 100644 --- a/test/src/command_runner_test.dart +++ b/test/src/command_runner_test.dart @@ -70,7 +70,7 @@ void main() { test('handles no command', overridePrint(() async { const expectedPrintLogs = [ - '🦄 A Very Good Commandline Interface\n' + '🦄 A Very Good Command Line Interface\n' '\n' 'Usage: very_good [arguments]\n' '\n' @@ -79,7 +79,7 @@ void main() { ' --version Print the current version.\n' '\n' 'Available commands:\n' - ' help Display help information for very_good.\n' + ''' create Creates a new very good flutter application in seconds.\n''' '\n' '''Run "very_good help " for more information about a command.''' ]; @@ -91,7 +91,7 @@ void main() { group('--help', () { test('outputs usage', overridePrint(() async { const expectedPrintLogs = [ - '🦄 A Very Good Commandline Interface\n' + '🦄 A Very Good Command Line Interface\n' '\n' 'Usage: very_good [arguments]\n' '\n' @@ -100,7 +100,7 @@ void main() { ' --version Print the current version.\n' '\n' 'Available commands:\n' - ' help Display help information for very_good.\n' + ''' create Creates a new very good flutter application in seconds.\n''' '\n' '''Run "very_good help " for more information about a command.''' ]; diff --git a/test/src/commands/create_test.dart b/test/src/commands/create_test.dart new file mode 100644 index 00000000..632d0845 --- /dev/null +++ b/test/src/commands/create_test.dart @@ -0,0 +1,30 @@ +import 'package:io/io.dart'; +import 'package:mason/mason.dart'; +import 'package:mockito/mockito.dart'; +import 'package:test/test.dart'; +import 'package:very_good_cli/src/commands/create.dart'; + +class MockLogger extends Mock implements Logger {} + +void main() { + group('Create', () { + Logger logger; + CreateCommand command; + + setUp(() { + logger = MockLogger(); + command = CreateCommand(logger: logger); + }); + + test('can be instantiated without an explicit Logger instance', () { + final command = CreateCommand(); + expect(command, isNotNull); + }); + + test('completes successfully with correct output', () async { + final result = await command.run(); + expect(result, equals(ExitCode.success.code)); + verify(logger.alert('Created a Very Good App! 🦄')).called(1); + }); + }); +}