mirror of
https://github.com/flutter/packages.git
synced 2025-06-18 04:33:52 +08:00
[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
This commit is contained in:
@ -6,17 +6,19 @@ import 'package:file/file.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
import 'common/core.dart';
|
||||
import 'common/plugin_command.dart';
|
||||
import 'common/package_looping_command.dart';
|
||||
import 'common/process_runner.dart';
|
||||
|
||||
/// A command to run the Java tests of Android plugins.
|
||||
class JavaTestCommand extends PluginCommand {
|
||||
class JavaTestCommand extends PackageLoopingCommand {
|
||||
/// Creates an instance of the test runner.
|
||||
JavaTestCommand(
|
||||
Directory packagesDir, {
|
||||
ProcessRunner processRunner = const ProcessRunner(),
|
||||
}) : super(packagesDir, processRunner: processRunner);
|
||||
|
||||
static const String _gradleWrapper = 'gradlew';
|
||||
|
||||
@override
|
||||
final String name = 'java-test';
|
||||
|
||||
@ -25,12 +27,10 @@ class JavaTestCommand extends PluginCommand {
|
||||
'Building the apks of the example apps is required before executing this'
|
||||
'command.';
|
||||
|
||||
static const String _gradleWrapper = 'gradlew';
|
||||
|
||||
@override
|
||||
Future<void> run() async {
|
||||
final Stream<Directory> examplesWithTests = getExamples().where(
|
||||
(Directory d) =>
|
||||
Future<List<String>> runForPackage(Directory package) async {
|
||||
final Iterable<Directory> examplesWithTests = getExamplesForPlugin(package)
|
||||
.where((Directory d) =>
|
||||
isFlutterPackage(d) &&
|
||||
(d
|
||||
.childDirectory('android')
|
||||
@ -44,18 +44,17 @@ class JavaTestCommand extends PluginCommand {
|
||||
.childDirectory('test')
|
||||
.existsSync()));
|
||||
|
||||
final List<String> failingPackages = <String>[];
|
||||
final List<String> missingFlutterBuild = <String>[];
|
||||
await for (final Directory example in examplesWithTests) {
|
||||
final String packageName =
|
||||
p.relative(example.path, from: packagesDir.path);
|
||||
print('\nRUNNING JAVA TESTS for $packageName');
|
||||
final List<String> errors = <String>[];
|
||||
for (final Directory example in examplesWithTests) {
|
||||
final String exampleName = p.relative(example.path, from: package.path);
|
||||
print('\nRUNNING JAVA TESTS for $exampleName');
|
||||
|
||||
final Directory androidDirectory = example.childDirectory('android');
|
||||
if (!androidDirectory.childFile(_gradleWrapper).existsSync()) {
|
||||
print('ERROR: Run "flutter build apk" on example app of $packageName'
|
||||
printError('ERROR: Run "flutter build apk" on $exampleName, or run '
|
||||
'this tool\'s "build-examples --apk" command, '
|
||||
'before executing tests.');
|
||||
missingFlutterBuild.add(packageName);
|
||||
errors.add('$exampleName has not been built.');
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -64,31 +63,9 @@ class JavaTestCommand extends PluginCommand {
|
||||
<String>['testDebugUnitTest', '--info'],
|
||||
workingDir: androidDirectory);
|
||||
if (exitCode != 0) {
|
||||
failingPackages.add(packageName);
|
||||
errors.add('$exampleName tests failed.');
|
||||
}
|
||||
}
|
||||
|
||||
print('\n\n');
|
||||
if (failingPackages.isNotEmpty) {
|
||||
print(
|
||||
'The Java tests for the following packages are failing (see above for'
|
||||
'details):');
|
||||
for (final String package in failingPackages) {
|
||||
print(' * $package');
|
||||
}
|
||||
}
|
||||
if (missingFlutterBuild.isNotEmpty) {
|
||||
print('Run "pub global run flutter_plugin_tools build-examples --apk" on'
|
||||
'the following packages before executing tests again:');
|
||||
for (final String package in missingFlutterBuild) {
|
||||
print(' * $package');
|
||||
}
|
||||
}
|
||||
|
||||
if (failingPackages.isNotEmpty || missingFlutterBuild.isNotEmpty) {
|
||||
throw ToolExit(1);
|
||||
}
|
||||
|
||||
print('All Java tests successful!');
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user