@@ -6,12 +6,35 @@ import 'package:path/path.dart' as path;
66import 'package:universal_io/io.dart' ;
77import 'package:very_good_cli/src/cli/cli.dart' ;
88
9+ /// Signature for the [Flutter.installed] method.
10+ typedef FlutterInstalledCommand = Future <bool > Function ();
11+
12+ /// Signature for the [Flutter.test] method.
13+ typedef FlutterTestCommand = Future <void > Function ({
14+ String cwd,
15+ bool recursive,
16+ bool collectCoverage,
17+ bool optimizePerformance,
18+ double ? minCoverage,
19+ String ? excludeFromCoverage,
20+ List <String >? arguments,
21+ void Function ([String ? ]) Function (String message)? progress,
22+ void Function (String )? stdout,
23+ void Function (String )? stderr,
24+ });
25+
926/// {@template test_command}
1027/// `very_good test` command for running tests.
1128/// {@endtemplate}
1229class TestCommand extends Command <int > {
1330 /// {@macro test_command}
14- TestCommand ({Logger ? logger}) : _logger = logger ?? Logger () {
31+ TestCommand ({
32+ Logger ? logger,
33+ FlutterInstalledCommand ? flutterInstalled,
34+ FlutterTestCommand ? flutterTest,
35+ }) : _logger = logger ?? Logger (),
36+ _flutterInstalled = flutterInstalled ?? Flutter .installed,
37+ _flutterTest = flutterTest ?? Flutter .test {
1538 argParser
1639 ..addFlag (
1740 'recursive' ,
@@ -41,6 +64,8 @@ class TestCommand extends Command<int> {
4164 }
4265
4366 final Logger _logger;
67+ final FlutterInstalledCommand _flutterInstalled;
68+ final FlutterTestCommand _flutterTest;
4469
4570 @override
4671 String get description => 'Run tests in a Dart or Flutter project.' ;
@@ -56,21 +81,33 @@ class TestCommand extends Command<int> {
5681
5782 @override
5883 Future <int > run () async {
59- final recursive = _argResults['recursive' ] as bool ;
6084 final targetPath = path.normalize (Directory .current.absolute.path);
85+ final pubspec = File (path.join (targetPath, 'pubspec.yaml' ));
86+
87+ if (! pubspec.existsSync ()) {
88+ _logger.err (
89+ '''
90+ Could not find a pubspec.yaml in $targetPath .
91+ This command should be run from the root of your Flutter project.''' ,
92+ );
93+ return ExitCode .noInput.code;
94+ }
95+
96+ final recursive = _argResults['recursive' ] as bool ;
6197 final collectCoverage = _argResults['coverage' ] as bool ;
6298 final minCoverage = double .tryParse (
6399 _argResults['min-coverage' ] as String ? ?? '' ,
64100 );
65101 final excludeTags = _argResults['exclude-tags' ] as String ? ;
66- final isFlutterInstalled = await Flutter .installed ();
67-
102+ final isFlutterInstalled = await _flutterInstalled ();
68103 final excludeFromCoverage = _argResults['exclude-coverage' ] as String ? ;
69104
70105 if (isFlutterInstalled) {
71106 try {
72- await Flutter .test (
107+ await _flutterTest (
108+ optimizePerformance: _argResults.rest.isEmpty,
73109 recursive: recursive,
110+ progress: _logger.progress,
74111 stdout: _logger.write,
75112 stderr: _logger.err,
76113 collectCoverage: collectCoverage,
@@ -81,13 +118,6 @@ class TestCommand extends Command<int> {
81118 ..._argResults.rest,
82119 ],
83120 );
84- } on PubspecNotFound catch (_) {
85- _logger.err (
86- '''
87- Could not find a pubspec.yaml in $targetPath .
88- This command should be run from the root of your Flutter project.''' ,
89- );
90- return ExitCode .noInput.code;
91121 } on MinCoverageNotMet catch (e) {
92122 _logger.err (
93123 '''Expected coverage >= ${minCoverage !.toStringAsFixed (2 )}% but actual is ${e .coverage .toStringAsFixed (2 )}%.''' ,
0 commit comments