Skip to content

Commit

Permalink
feat: create command scaffold (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Jan 31, 2021
1 parent 94e211a commit ed5cf77
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [arguments]

Expand All @@ -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 <command>" for more information about a command.
```
Expand Down
4 changes: 3 additions & 1 deletion lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -12,12 +13,13 @@ class VeryGoodCommandRunner extends CommandRunner<int> {
/// {@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;
Expand Down
1 change: 1 addition & 0 deletions lib/src/commands/commands.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'create.dart';
26 changes: 26 additions & 0 deletions lib/src/commands/create.dart
Original file line number Diff line number Diff line change
@@ -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<int> {
/// {@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<int> run() async {
_logger.alert('Created a Very Good App! 🦄');
return ExitCode.success.code;
}
}
8 changes: 4 additions & 4 deletions test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <command> [arguments]\n'
'\n'
Expand All @@ -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 <command>" for more information about a command.'''
];
Expand All @@ -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 <command> [arguments]\n'
'\n'
Expand All @@ -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 <command>" for more information about a command.'''
];
Expand Down
30 changes: 30 additions & 0 deletions test/src/commands/create_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
});
}

0 comments on commit ed5cf77

Please sign in to comment.