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

Commit a02b8f2

Browse files
[flutter_plugin_tools] Migrate java-test to new base command (#4105)
Switches `java-test` to the new base command that handles the boilerplate of looping over target packages. Includes test improvements: - Adds failure tests; previously no failure cases were covered. - Captures output so test output isn't spammed with command output. Part of flutter/flutter#83413
1 parent 8af8001 commit a02b8f2

File tree

2 files changed

+84
-41
lines changed

2 files changed

+84
-41
lines changed

script/tool/lib/src/java_test_command.dart

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ import 'package:file/file.dart';
66
import 'package:path/path.dart' as p;
77

88
import 'common/core.dart';
9-
import 'common/plugin_command.dart';
9+
import 'common/package_looping_command.dart';
1010
import 'common/process_runner.dart';
1111

1212
/// A command to run the Java tests of Android plugins.
13-
class JavaTestCommand extends PluginCommand {
13+
class JavaTestCommand extends PackageLoopingCommand {
1414
/// Creates an instance of the test runner.
1515
JavaTestCommand(
1616
Directory packagesDir, {
1717
ProcessRunner processRunner = const ProcessRunner(),
1818
}) : super(packagesDir, processRunner: processRunner);
1919

20+
static const String _gradleWrapper = 'gradlew';
21+
2022
@override
2123
final String name = 'java-test';
2224

@@ -25,12 +27,10 @@ class JavaTestCommand extends PluginCommand {
2527
'Building the apks of the example apps is required before executing this'
2628
'command.';
2729

28-
static const String _gradleWrapper = 'gradlew';
29-
3030
@override
31-
Future<void> run() async {
32-
final Stream<Directory> examplesWithTests = getExamples().where(
33-
(Directory d) =>
31+
Future<List<String>> runForPackage(Directory package) async {
32+
final Iterable<Directory> examplesWithTests = getExamplesForPlugin(package)
33+
.where((Directory d) =>
3434
isFlutterPackage(d) &&
3535
(d
3636
.childDirectory('android')
@@ -44,18 +44,17 @@ class JavaTestCommand extends PluginCommand {
4444
.childDirectory('test')
4545
.existsSync()));
4646

47-
final List<String> failingPackages = <String>[];
48-
final List<String> missingFlutterBuild = <String>[];
49-
await for (final Directory example in examplesWithTests) {
50-
final String packageName =
51-
p.relative(example.path, from: packagesDir.path);
52-
print('\nRUNNING JAVA TESTS for $packageName');
47+
final List<String> errors = <String>[];
48+
for (final Directory example in examplesWithTests) {
49+
final String exampleName = p.relative(example.path, from: package.path);
50+
print('\nRUNNING JAVA TESTS for $exampleName');
5351

5452
final Directory androidDirectory = example.childDirectory('android');
5553
if (!androidDirectory.childFile(_gradleWrapper).existsSync()) {
56-
print('ERROR: Run "flutter build apk" on example app of $packageName'
54+
printError('ERROR: Run "flutter build apk" on $exampleName, or run '
55+
'this tool\'s "build-examples --apk" command, '
5756
'before executing tests.');
58-
missingFlutterBuild.add(packageName);
57+
errors.add('$exampleName has not been built.');
5958
continue;
6059
}
6160

@@ -64,31 +63,9 @@ class JavaTestCommand extends PluginCommand {
6463
<String>['testDebugUnitTest', '--info'],
6564
workingDir: androidDirectory);
6665
if (exitCode != 0) {
67-
failingPackages.add(packageName);
68-
}
69-
}
70-
71-
print('\n\n');
72-
if (failingPackages.isNotEmpty) {
73-
print(
74-
'The Java tests for the following packages are failing (see above for'
75-
'details):');
76-
for (final String package in failingPackages) {
77-
print(' * $package');
78-
}
79-
}
80-
if (missingFlutterBuild.isNotEmpty) {
81-
print('Run "pub global run flutter_plugin_tools build-examples --apk" on'
82-
'the following packages before executing tests again:');
83-
for (final String package in missingFlutterBuild) {
84-
print(' * $package');
66+
errors.add('$exampleName tests failed.');
8567
}
8668
}
87-
88-
if (failingPackages.isNotEmpty || missingFlutterBuild.isNotEmpty) {
89-
throw ToolExit(1);
90-
}
91-
92-
print('All Java tests successful!');
69+
return errors;
9370
}
9471
}

script/tool/test/java_test_command_test.dart

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:flutter_plugin_tools/src/java_test_command.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:test/test.dart';
1313

14+
import 'mocks.dart';
1415
import 'util.dart';
1516

1617
void main() {
@@ -45,7 +46,7 @@ void main() {
4546
],
4647
);
4748

48-
await runner.run(<String>['java-test']);
49+
await runCapturingPrint(runner, <String>['java-test']);
4950

5051
expect(
5152
processRunner.recordedCalls,
@@ -72,7 +73,7 @@ void main() {
7273
],
7374
);
7475

75-
await runner.run(<String>['java-test']);
76+
await runCapturingPrint(runner, <String>['java-test']);
7677

7778
expect(
7879
processRunner.recordedCalls,
@@ -85,5 +86,70 @@ void main() {
8586
]),
8687
);
8788
});
89+
90+
test('fails when the app needs to be built', () async {
91+
createFakePlugin(
92+
'plugin1',
93+
packagesDir,
94+
platformSupport: <String, PlatformSupport>{
95+
kPlatformAndroid: PlatformSupport.inline
96+
},
97+
extraFiles: <String>[
98+
'example/android/app/src/test/example_test.java',
99+
],
100+
);
101+
102+
Error? commandError;
103+
final List<String> output = await runCapturingPrint(
104+
runner, <String>['java-test'], errorHandler: (Error e) {
105+
commandError = e;
106+
});
107+
108+
expect(commandError, isA<ToolExit>());
109+
110+
expect(
111+
output,
112+
containsAllInOrder(<Matcher>[
113+
contains('ERROR: Run "flutter build apk" on example'),
114+
contains('plugin1:\n'
115+
' example has not been built.')
116+
]),
117+
);
118+
});
119+
120+
test('fails when a test fails', () async {
121+
createFakePlugin(
122+
'plugin1',
123+
packagesDir,
124+
platformSupport: <String, PlatformSupport>{
125+
kPlatformAndroid: PlatformSupport.inline
126+
},
127+
extraFiles: <String>[
128+
'example/android/gradlew',
129+
'example/android/app/src/test/example_test.java',
130+
],
131+
);
132+
133+
// Simulate failure from `gradlew`.
134+
final MockProcess mockDriveProcess = MockProcess();
135+
mockDriveProcess.exitCodeCompleter.complete(1);
136+
processRunner.processToReturn = mockDriveProcess;
137+
138+
Error? commandError;
139+
final List<String> output = await runCapturingPrint(
140+
runner, <String>['java-test'], errorHandler: (Error e) {
141+
commandError = e;
142+
});
143+
144+
expect(commandError, isA<ToolExit>());
145+
146+
expect(
147+
output,
148+
containsAllInOrder(<Matcher>[
149+
contains('plugin1:\n'
150+
' example tests failed.')
151+
]),
152+
);
153+
});
88154
});
89155
}

0 commit comments

Comments
 (0)