diff --git a/README.md b/README.md index cb65e84f4..fc6bc4308 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,10 @@ Run "very_good help" to see global options. Run tests in a Dart or Flutter project. ```sh -# Run tests in the current directory +# Run all tests very_good test -# Run tests in ./some/other/directory +# Run only tests in ./some/other/directory very_good test ./some/other/directory # Run tests recursively diff --git a/lib/src/cli/flutter_cli.dart b/lib/src/cli/flutter_cli.dart index ca7157403..9b20a2ccc 100644 --- a/lib/src/cli/flutter_cli.dart +++ b/lib/src/cli/flutter_cli.dart @@ -107,6 +107,7 @@ class Flutter { bool recursive = false, bool collectCoverage = false, double? minCoverage, + List? arguments, void Function(String)? stdout, void Function(String)? stderr, }) async { @@ -120,10 +121,11 @@ class Flutter { await _runCommand( cmd: (cwd) { void noop(String? _) {} - stdout?.call('Running "flutter test" in $cwd...\n'); + stdout?.call('Running "flutter test" in ${p.canonicalize(cwd)}...\n'); return _flutterTest( cwd: cwd, collectCoverage: collectCoverage, + arguments: arguments, stdout: stdout ?? noop, stderr: stderr ?? noop, ); @@ -172,6 +174,7 @@ Future _runCommand({ Future _flutterTest({ String cwd = '.', bool collectCoverage = false, + List? arguments, required void Function(String) stdout, required void Function(String) stderr, }) { @@ -208,6 +211,7 @@ Future _flutterTest({ workingDirectory: cwd, arguments: [ if (collectCoverage) '--coverage', + ...?arguments, ], runInShell: true, ).listen( diff --git a/lib/src/commands/test.dart b/lib/src/commands/test.dart index 0b4f24c82..c00fed406 100644 --- a/lib/src/commands/test.dart +++ b/lib/src/commands/test.dart @@ -46,13 +46,8 @@ class TestCommand extends Command { @override Future run() async { - if (_argResults.rest.length > 1) { - throw UsageException('Too many arguments', usage); - } - final recursive = _argResults['recursive'] as bool; - final target = _argResults.rest.length == 1 ? _argResults.rest[0] : '.'; - final targetPath = path.normalize(Directory(target).absolute.path); + final targetPath = path.normalize(Directory.current.absolute.path); final collectCoverage = _argResults['coverage'] as bool; final minCoverage = double.tryParse( _argResults['min-coverage'] as String? ?? '', @@ -61,15 +56,19 @@ class TestCommand extends Command { if (isFlutterInstalled) { try { await Flutter.test( - cwd: targetPath, recursive: recursive, stdout: _logger.write, stderr: _logger.err, collectCoverage: collectCoverage, minCoverage: minCoverage, + arguments: argResults?.rest, ); } on PubspecNotFound catch (_) { - _logger.err('Could not find a pubspec.yaml in $targetPath'); + _logger.err( + ''' +Could not find a pubspec.yaml in $targetPath. +This command should be run from the root of your Flutter project.''', + ); return ExitCode.noInput.code; } on MinCoverageNotMet catch (e) { _logger.err( diff --git a/test/src/commands/test_test.dart b/test/src/commands/test_test.dart index 89d9a5e75..d67bd176f 100644 --- a/test/src/commands/test_test.dart +++ b/test/src/commands/test_test.dart @@ -42,6 +42,12 @@ dev_dependencies: void main() { group('test', () { + final cwd = Directory.current; + + setUp(() { + Directory.current = cwd; + }); + test( 'help', withRunner((commandRunner, logger, printLogs) async { @@ -57,23 +63,13 @@ void main() { }), ); - test( - 'throws usage exception ' - 'when too many arguments are provided', - withRunner((commandRunner, logger, printLogs) async { - final result = await commandRunner.run( - ['test', 'arg1', 'arg2'], - ); - expect(result, equals(ExitCode.usage.code)); - }), - ); - test( 'throws pubspec not found exception ' 'when no pubspec.yaml exists', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); - final result = await commandRunner.run(['test', directory.path]); + Directory.current = directory.path; + final result = await commandRunner.run(['test']); expect(result, equals(ExitCode.noInput.code)); verify(() { logger.err(any(that: contains('Could not find a pubspec.yaml in'))); @@ -86,7 +82,8 @@ void main() { 'when no pubspec.yaml exists (recursive)', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); - final result = await commandRunner.run(['test', '-r', directory.path]); + Directory.current = directory.path; + final result = await commandRunner.run(['test', '-r']); expect(result, equals(ExitCode.noInput.code)); verify(() { logger.err(any(that: contains('Could not find a pubspec.yaml in'))); @@ -99,8 +96,9 @@ void main() { withRunner( (commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; File(path.join(directory.path, 'pubspec.yaml')).writeAsStringSync(''); - final result = await commandRunner.run(['test', directory.path]); + final result = await commandRunner.run(['test']); expect(result, equals(ExitCode.unavailable.code)); }, ), @@ -111,6 +109,7 @@ void main() { 'when pubspec.yaml and tests exist', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; final testDirectory = Directory(path.join(directory.path, 'test')) ..createSync(); File( @@ -119,7 +118,7 @@ void main() { File( path.join(testDirectory.path, 'example_test.dart'), ).writeAsStringSync(testContent); - final result = await commandRunner.run(['test', directory.path]); + final result = await commandRunner.run(['test']); expect(result, equals(ExitCode.success.code)); verify(() { logger.write( @@ -136,6 +135,7 @@ void main() { 'completes normally --coverage', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; final testDirectory = Directory(path.join(directory.path, 'test')) ..createSync(); File( @@ -144,9 +144,7 @@ void main() { File( path.join(testDirectory.path, 'example_test.dart'), ).writeAsStringSync(testContent); - final result = await commandRunner.run( - ['test', '--coverage', directory.path], - ); + final result = await commandRunner.run(['test', '--coverage']); expect(result, equals(ExitCode.success.code)); verify(() { logger.write( @@ -163,6 +161,7 @@ void main() { 'completes normally --coverage --min-coverage 0', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; final testDirectory = Directory(path.join(directory.path, 'test')) ..createSync(); File( @@ -172,7 +171,7 @@ void main() { path.join(testDirectory.path, 'example_test.dart'), ).writeAsStringSync(testContent); final result = await commandRunner.run( - ['test', '--coverage', '--min-coverage', '0', directory.path], + ['test', '--coverage', '--min-coverage', '0'], ); expect(result, equals(ExitCode.success.code)); verify(() { @@ -190,6 +189,7 @@ void main() { 'fails when coverage not met --coverage --min-coverage 100', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; final testDirectory = Directory(path.join(directory.path, 'test')) ..createSync(); File( @@ -199,7 +199,7 @@ void main() { path.join(testDirectory.path, 'example_test.dart'), ).writeAsStringSync(testContent); final result = await commandRunner.run( - ['test', '--coverage', '--min-coverage', '100', directory.path], + ['test', '--coverage', '--min-coverage', '100'], ); expect(result, equals(ExitCode.unavailable.code)); verify(() { @@ -221,6 +221,7 @@ void main() { 'when pubspec.yaml and tests exist (recursive)', withRunner((commandRunner, logger, printLogs) async { final directory = Directory.systemTemp.createTempSync(); + Directory.current = directory.path; final testDirectoryA = Directory( path.join(directory.path, 'example_a', 'test'), )..createSync(recursive: true); @@ -240,9 +241,7 @@ void main() { path.join(directory.path, 'example_b', 'pubspec.yaml'), ).writeAsStringSync(pubspecContent('example_b')); - final result = await commandRunner.run( - ['test', '--recursive', directory.path], - ); + final result = await commandRunner.run(['test', '--recursive']); expect(result, equals(ExitCode.success.code)); verify(() { logger.write(