3
3
// found in the LICENSE file.
4
4
5
5
import 'package:file/file.dart' ;
6
- import 'package:path/path.dart' as p;
7
6
8
7
import 'common/core.dart' ;
9
- import 'common/plugin_command .dart' ;
8
+ import 'common/package_looping_command .dart' ;
10
9
import 'common/plugin_utils.dart' ;
11
10
import 'common/process_runner.dart' ;
12
11
13
12
/// A command to run Dart unit tests for packages.
14
- class TestCommand extends PluginCommand {
13
+ class TestCommand extends PackageLoopingCommand {
15
14
/// Creates an instance of the test command.
16
15
TestCommand (
17
16
Directory packagesDir, {
@@ -20,7 +19,10 @@ class TestCommand extends PluginCommand {
20
19
argParser.addOption (
21
20
kEnableExperiment,
22
21
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.' ,
24
26
);
25
27
}
26
28
@@ -32,72 +34,65 @@ class TestCommand extends PluginCommand {
32
34
'This command requires "flutter" to be in your path.' ;
33
35
34
36
@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
+ }
44
41
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
+ }
46
50
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);
48
54
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
+ }
58
68
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 ;
90
81
}
91
82
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
+ );
100
95
101
- print ( 'All tests are passing!' ) ;
96
+ return exitCode == 0 ;
102
97
}
103
98
}
0 commit comments