33// found in the LICENSE file.
44
55import 'package:file/file.dart' ;
6- import 'package:path/path.dart' as p;
76
87import 'common/core.dart' ;
9- import 'common/plugin_command .dart' ;
8+ import 'common/package_looping_command .dart' ;
109import 'common/plugin_utils.dart' ;
1110import 'common/process_runner.dart' ;
1211
1312/// A command to run Dart unit tests for packages.
14- class TestCommand extends PluginCommand {
13+ class TestCommand extends PackageLoopingCommand {
1514 /// Creates an instance of the test command.
1615 TestCommand (
1716 Directory packagesDir, {
@@ -20,7 +19,10 @@ class TestCommand extends PluginCommand {
2019 argParser.addOption (
2120 kEnableExperiment,
2221 defaultsTo: '' ,
23- help: 'Runs the tests in Dart VM with the given experiments enabled.' ,
22+ help:
23+ 'Runs Dart unit tests in Dart VM with the given experiments enabled. '
24+ 'See https://github.com/dart-lang/sdk/blob/master/docs/process/experimental-flags.md '
25+ 'for details.' ,
2426 );
2527 }
2628
@@ -32,72 +34,65 @@ class TestCommand extends PluginCommand {
3234 'This command requires "flutter" to be in your path.' ;
3335
3436 @override
35- Future <void > run () async {
36- final List <String > failingPackages = < String > [];
37- await for (final Directory packageDir in getPackages ()) {
38- final String packageName =
39- p.relative (packageDir.path, from: packagesDir.path);
40- if (! packageDir.childDirectory ('test' ).existsSync ()) {
41- print ('SKIPPING $packageName - no test subdirectory' );
42- continue ;
43- }
37+ Future <PackageResult > runForPackage (Directory package) async {
38+ if (! package.childDirectory ('test' ).existsSync ()) {
39+ return PackageResult .skip ('No test/ directory.' );
40+ }
4441
45- print ('RUNNING $packageName tests...' );
42+ bool passed;
43+ if (isFlutterPackage (package)) {
44+ passed = await _runFlutterTests (package);
45+ } else {
46+ passed = await _runDartTests (package);
47+ }
48+ return passed ? PackageResult .success () : PackageResult .fail ();
49+ }
4650
47- final String enableExperiment = getStringArg (kEnableExperiment);
51+ /// Runs the Dart tests for a Flutter package, returning true on success.
52+ Future <bool > _runFlutterTests (Directory package) async {
53+ final String experiment = getStringArg (kEnableExperiment);
4854
49- // `flutter test` automatically gets packages. `pub run test` does not. :(
50- int exitCode = 0 ;
51- if (isFlutterPackage (packageDir)) {
52- final List <String > args = < String > [
53- 'test' ,
54- '--color' ,
55- if (enableExperiment.isNotEmpty)
56- '--enable-experiment=$enableExperiment ' ,
57- ];
55+ final int exitCode = await processRunner.runAndStream (
56+ flutterCommand,
57+ < String > [
58+ 'test' ,
59+ '--color' ,
60+ if (experiment.isNotEmpty) '--enable-experiment=$experiment ' ,
61+ // TODO(ditman): Remove this once all plugins are migrated to 'drive'.
62+ if (isWebPlugin (package)) '--platform=chrome' ,
63+ ],
64+ workingDir: package,
65+ );
66+ return exitCode == 0 ;
67+ }
5868
59- if (isWebPlugin (packageDir)) {
60- args.add ('--platform=chrome' );
61- }
62- exitCode = await processRunner.runAndStream (
63- 'flutter' ,
64- args,
65- workingDir: packageDir,
66- );
67- } else {
68- exitCode = await processRunner.runAndStream (
69- 'dart' ,
70- < String > ['pub' , 'get' ],
71- workingDir: packageDir,
72- );
73- if (exitCode == 0 ) {
74- exitCode = await processRunner.runAndStream (
75- 'dart' ,
76- < String > [
77- 'pub' ,
78- 'run' ,
79- if (enableExperiment.isNotEmpty)
80- '--enable-experiment=$enableExperiment ' ,
81- 'test' ,
82- ],
83- workingDir: packageDir,
84- );
85- }
86- }
87- if (exitCode != 0 ) {
88- failingPackages.add (packageName);
89- }
69+ /// Runs the Dart tests for a non-Flutter package, returning true on success.
70+ Future <bool > _runDartTests (Directory package) async {
71+ // Unlike `flutter test`, `pub run test` does not automatically get
72+ // packages
73+ int exitCode = await processRunner.runAndStream (
74+ 'dart' ,
75+ < String > ['pub' , 'get' ],
76+ workingDir: package,
77+ );
78+ if (exitCode != 0 ) {
79+ printError ('Unable to fetch dependencies.' );
80+ return false ;
9081 }
9182
92- print ('\n\n ' );
93- if (failingPackages.isNotEmpty) {
94- print ('Tests for the following packages are failing (see above):' );
95- for (final String package in failingPackages) {
96- print (' * $package ' );
97- }
98- throw ToolExit (1 );
99- }
83+ final String experiment = getStringArg (kEnableExperiment);
84+
85+ exitCode = await processRunner.runAndStream (
86+ 'dart' ,
87+ < String > [
88+ 'pub' ,
89+ 'run' ,
90+ if (experiment.isNotEmpty) '--enable-experiment=$experiment ' ,
91+ 'test' ,
92+ ],
93+ workingDir: package,
94+ );
10095
101- print ( 'All tests are passing!' ) ;
96+ return exitCode == 0 ;
10297 }
10398}
0 commit comments