Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[tool] Target specific Android unit tests
Instead of running all test targets in the transitive dependency tree
when running Android native unit tests via `gradlew`, specifically
target the example app and the containing plugin. This avoids running
tests from other packages (such as `flutter_plugin_android_lifecycle`).

Fixes flutter/flutter#85057
  • Loading branch information
stuartmorgan-g committed May 10, 2023
commit eb683946671842807af71d0011e4cf908a10085f
5 changes: 3 additions & 2 deletions script/tool/lib/src/common/gradle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ class GradleProject {

/// Runs a `gradlew` command with the given parameters.
Future<int> runCommand(
String target, {
String task, {
List<String> additionalTasks = const <String>[],
List<String> arguments = const <String>[],
}) {
return processRunner.runAndStream(
gradleWrapper.path,
<String>[target, ...arguments],
<String>[task, ...additionalTasks, ...arguments],
workingDir: androidDirectory,
);
}
Expand Down
11 changes: 10 additions & 1 deletion script/tool/lib/src/native_test_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ this command.
}

final Iterable<RepositoryPackage> examples = plugin.getExamples();
final String pluginName = plugin.directory.basename;

bool ranUnitTests = false;
bool ranAnyTests = false;
Expand Down Expand Up @@ -317,7 +318,15 @@ this command.

if (runUnitTests) {
print('Running unit tests...');
final int exitCode = await project.runCommand('testDebugUnitTest');
const String taskName = 'testDebugUnitTest';
// Target the unit tests in the app and plugin specifically, to avoid
// transitively running tests in dependencies. If unit tests have
// already run in an earlier example, only run any app-level unit tests.
final List<String> pluginTestTask = <String>[
if (!ranUnitTests) '$pluginName:$taskName'
];
final int exitCode = await project.runCommand('app:$taskName',
additionalTasks: pluginTestTask);
if (exitCode != 0) {
printError('$exampleName unit tests failed.');
failed = true;
Expand Down
77 changes: 70 additions & 7 deletions script/tool/test/native_test_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,10 @@ void main() {
orderedEquals(<ProcessCall>[
ProcessCall(
androidFolder.childFile('gradlew').path,
const <String>['testDebugUnitTest'],
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder.path,
),
]),
Expand Down Expand Up @@ -470,13 +473,62 @@ void main() {
orderedEquals(<ProcessCall>[
ProcessCall(
androidFolder.childFile('gradlew').path,
const <String>['testDebugUnitTest'],
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder.path,
),
]),
);
});

test('only runs plugin-level unit tests once', () async {
final RepositoryPackage plugin = createFakePlugin(
'plugin',
packagesDir,
platformSupport: <String, PlatformDetails>{
platformAndroid: const PlatformDetails(PlatformSupport.inline)
},
examples: <String>['example1', 'example2'],
extraFiles: <String>[
'example/example1/android/gradlew',
'example/example1/android/app/src/test/example_test.java',
'example/example2/android/gradlew',
'example/example2/android/app/src/test/example_test.java',
],
);

await runCapturingPrint(runner, <String>['native-test', '--android']);

final List<RepositoryPackage> examples = plugin.getExamples().toList();
final Directory androidFolder1 =
examples[0].platformDirectory(FlutterPlatform.android);
final Directory androidFolder2 =
examples[1].platformDirectory(FlutterPlatform.android);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
androidFolder1.childFile('gradlew').path,
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder1.path,
),
ProcessCall(
androidFolder2.childFile('gradlew').path,
const <String>[
'app:testDebugUnitTest',
],
androidFolder2.path,
),
]),
);
});

test('runs Java integration tests', () async {
final RepositoryPackage plugin = createFakePlugin(
'plugin',
Expand Down Expand Up @@ -629,7 +681,10 @@ public class FlutterActivityTest {
orderedEquals(<ProcessCall>[
ProcessCall(
androidFolder.childFile('gradlew').path,
const <String>['testDebugUnitTest'],
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder.path,
),
ProcessCall(
Expand Down Expand Up @@ -708,7 +763,10 @@ public class FlutterActivityTest {
orderedEquals(<ProcessCall>[
ProcessCall(
androidFolder.childFile('gradlew').path,
const <String>['testDebugUnitTest'],
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder.path,
),
]),
Expand Down Expand Up @@ -854,7 +912,7 @@ public class FlutterActivityTest {
processRunner.mockProcessesForExecutable[gradlewPath] =
<FakeProcessInfo>[
FakeProcessInfo(
MockProcess(), <String>['testDebugUnitTest']), // unit passes
MockProcess(), <String>['app:testDebugUnitTest']), // unit passes
FakeProcessInfo(MockProcess(exitCode: 1),
<String>['app:connectedAndroidTest']), // integration fails
];
Expand Down Expand Up @@ -1395,8 +1453,13 @@ public class FlutterActivityTest {
expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(androidFolder.childFile('gradlew').path,
const <String>['testDebugUnitTest'], androidFolder.path),
ProcessCall(
androidFolder.childFile('gradlew').path,
const <String>[
'app:testDebugUnitTest',
'plugin:testDebugUnitTest',
],
androidFolder.path),
getTargetCheckCall(pluginExampleDirectory, 'ios'),
getRunTestCall(pluginExampleDirectory, 'ios',
destination: 'foo_destination'),
Expand Down