|
| 1 | +// ignore_for_file: no_adjacent_strings_in_list |
| 2 | +import 'dart:async'; |
| 3 | + |
| 4 | +import 'package:args/command_runner.dart'; |
| 5 | +import 'package:io/io.dart'; |
| 6 | +import 'package:mason/mason.dart'; |
| 7 | +import 'package:mockito/mockito.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | +import 'package:very_good_cli/src/command_runner.dart'; |
| 10 | +import 'package:very_good_cli/src/version.dart'; |
| 11 | + |
| 12 | +class MockLogger extends Mock implements Logger {} |
| 13 | + |
| 14 | +void main() { |
| 15 | + group('VeryGoodCommandRunner', () { |
| 16 | + List<String> printLogs; |
| 17 | + Logger logger; |
| 18 | + VeryGoodCommandRunner commandRunner; |
| 19 | + |
| 20 | + void Function() overridePrint(void Function() fn) { |
| 21 | + return () { |
| 22 | + final spec = ZoneSpecification(print: (_, __, ___, String msg) { |
| 23 | + printLogs.add(msg); |
| 24 | + }); |
| 25 | + return Zone.current.fork(specification: spec).run<void>(fn); |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + setUp(() { |
| 30 | + printLogs = []; |
| 31 | + logger = MockLogger(); |
| 32 | + commandRunner = VeryGoodCommandRunner(logger: logger); |
| 33 | + }); |
| 34 | + |
| 35 | + test('can be instantiated without an explicit logger instance', () { |
| 36 | + final commandRunner = VeryGoodCommandRunner(); |
| 37 | + expect(commandRunner, isNotNull); |
| 38 | + }); |
| 39 | + |
| 40 | + group('run', () { |
| 41 | + test('handles FormatException', () async { |
| 42 | + const exception = FormatException('oops!'); |
| 43 | + var isFirstInvocation = true; |
| 44 | + when(logger.info(any)).thenAnswer((_) { |
| 45 | + if (isFirstInvocation) { |
| 46 | + isFirstInvocation = false; |
| 47 | + throw exception; |
| 48 | + } |
| 49 | + }); |
| 50 | + final result = await commandRunner.run(['--version']); |
| 51 | + expect(result, equals(ExitCode.usage.code)); |
| 52 | + verify(logger.err(exception.message)).called(1); |
| 53 | + verify(logger.info(commandRunner.usage)).called(1); |
| 54 | + }); |
| 55 | + |
| 56 | + test('handles UsageException', () async { |
| 57 | + final exception = UsageException('oops!', commandRunner.usage); |
| 58 | + var isFirstInvocation = true; |
| 59 | + when(logger.info(any)).thenAnswer((_) { |
| 60 | + if (isFirstInvocation) { |
| 61 | + isFirstInvocation = false; |
| 62 | + throw exception; |
| 63 | + } |
| 64 | + }); |
| 65 | + final result = await commandRunner.run(['--version']); |
| 66 | + expect(result, equals(ExitCode.usage.code)); |
| 67 | + verify(logger.err(exception.message)).called(1); |
| 68 | + verify(logger.info(commandRunner.usage)).called(1); |
| 69 | + }); |
| 70 | + |
| 71 | + test('handles no command', overridePrint(() async { |
| 72 | + const expectedPrintLogs = [ |
| 73 | + '🦄 A Very Good Commandline Interface\n' |
| 74 | + '\n' |
| 75 | + 'Usage: very_good <command> [arguments]\n' |
| 76 | + '\n' |
| 77 | + 'Global options:\n' |
| 78 | + '-h, --help Print this usage information.\n' |
| 79 | + ' --version Print the current version.\n' |
| 80 | + '\n' |
| 81 | + 'Available commands:\n' |
| 82 | + ' help Display help information for very_good.\n' |
| 83 | + '\n' |
| 84 | + '''Run "very_good help <command>" for more information about a command.''' |
| 85 | + ]; |
| 86 | + final result = await commandRunner.run([]); |
| 87 | + expect(printLogs, equals(expectedPrintLogs)); |
| 88 | + expect(result, equals(ExitCode.success.code)); |
| 89 | + })); |
| 90 | + |
| 91 | + group('--help', () { |
| 92 | + test('outputs usage', overridePrint(() async { |
| 93 | + const expectedPrintLogs = [ |
| 94 | + '🦄 A Very Good Commandline Interface\n' |
| 95 | + '\n' |
| 96 | + 'Usage: very_good <command> [arguments]\n' |
| 97 | + '\n' |
| 98 | + 'Global options:\n' |
| 99 | + '-h, --help Print this usage information.\n' |
| 100 | + ' --version Print the current version.\n' |
| 101 | + '\n' |
| 102 | + 'Available commands:\n' |
| 103 | + ' help Display help information for very_good.\n' |
| 104 | + '\n' |
| 105 | + '''Run "very_good help <command>" for more information about a command.''' |
| 106 | + ]; |
| 107 | + final result = await commandRunner.run(['--help']); |
| 108 | + expect(printLogs, equals(expectedPrintLogs)); |
| 109 | + expect(result, equals(ExitCode.success.code)); |
| 110 | + |
| 111 | + printLogs.clear(); |
| 112 | + |
| 113 | + final resultAbbr = await commandRunner.run(['-h']); |
| 114 | + expect(printLogs, equals(expectedPrintLogs)); |
| 115 | + expect(resultAbbr, equals(ExitCode.success.code)); |
| 116 | + })); |
| 117 | + }); |
| 118 | + |
| 119 | + group('--version', () { |
| 120 | + test('outputs current version', () async { |
| 121 | + final result = await commandRunner.run(['--version']); |
| 122 | + expect(result, equals(ExitCode.success.code)); |
| 123 | + verify(logger.info('very_good version: $packageVersion')); |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +} |
0 commit comments