Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b16697c

Browse files
[flutter_plugin_tools] Migrate 'test' to new base command (#4133)
Migrates `test` to the new package looping base command. Refactors the bulk of the logic to helpers for easier understanding of the flow. Part of flutter/flutter#83413
1 parent 1626b10 commit b16697c

File tree

2 files changed

+66
-68
lines changed

2 files changed

+66
-68
lines changed

script/tool/lib/src/test_command.dart

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
// found in the LICENSE file.
44

55
import 'package:file/file.dart';
6-
import 'package:path/path.dart' as p;
76

87
import 'common/core.dart';
9-
import 'common/plugin_command.dart';
8+
import 'common/package_looping_command.dart';
109
import 'common/plugin_utils.dart';
1110
import '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
}

script/tool/test/test_command_test.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ void main() {
7373
expect(
7474
output,
7575
containsAllInOrder(<Matcher>[
76-
contains('Tests for the following packages are failing'),
77-
contains(' * plugin1'),
76+
contains('The following packages had errors:'),
77+
contains(' plugin1'),
7878
]));
7979
});
8080

@@ -137,7 +137,9 @@ void main() {
137137
expect(
138138
output,
139139
containsAllInOrder(<Matcher>[
140-
contains('Tests for the following packages are failing'),
140+
contains('Unable to fetch dependencies'),
141+
contains('The following packages had errors:'),
142+
contains(' a_package'),
141143
]));
142144
});
143145

@@ -160,7 +162,8 @@ void main() {
160162
expect(
161163
output,
162164
containsAllInOrder(<Matcher>[
163-
contains('Tests for the following packages are failing'),
165+
contains('The following packages had errors:'),
166+
contains(' a_package'),
164167
]));
165168
});
166169

0 commit comments

Comments
 (0)