Skip to content

[tool] Run config-only build for iOS/macOS native-test #9080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 27 additions & 10 deletions script/tool/lib/src/native_test_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ this command.
}

Future<_PlatformResult> _testIOS(RepositoryPackage plugin, _TestMode mode) {
return _runXcodeTests(plugin, 'iOS', mode,
return _runXcodeTests(plugin, FlutterPlatform.ios, mode,
extraFlags: _iOSDestinationFlags);
}

Future<_PlatformResult> _testMacOS(RepositoryPackage plugin, _TestMode mode) {
return _runXcodeTests(plugin, 'macOS', mode);
return _runXcodeTests(plugin, FlutterPlatform.macos, mode);
}

/// Runs all applicable tests for [plugin], printing status and returning
Expand All @@ -448,7 +448,7 @@ this command.
/// usually at "example/{ios,macos}/Runner.xcworkspace".
Future<_PlatformResult> _runXcodeTests(
RepositoryPackage plugin,
String targetPlatform,
FlutterPlatform targetPlatform,
_TestMode mode, {
List<String> extraFlags = const <String>[],
}) async {
Expand All @@ -459,6 +459,8 @@ this command.
} else if (mode.integrationOnly) {
testTarget = 'RunnerUITests';
}
final String targetPlatformString =
targetPlatform == FlutterPlatform.ios ? 'iOS' : 'macOS';

bool ranUnitTests = false;
// Assume skipped until at least one test has run.
Expand All @@ -472,8 +474,8 @@ this command.
bool exampleHasUnitTests = false;
final String? targetToCheck =
testTarget ?? (mode.unit ? unitTestTarget : null);
final Directory xcodeProject = example.directory
.childDirectory(targetPlatform.toLowerCase())
final Directory xcodeProject = example
.platformDirectory(targetPlatform)
.childDirectory('Runner.xcodeproj');
if (targetToCheck != null) {
final bool? hasTarget =
Expand All @@ -490,14 +492,29 @@ this command.
}
}

_printRunningExampleTestsMessage(example, targetPlatform);
// Ensure that the native project files are configured for a debug build,
// otherwise the Xcode build step will fail due to mode mismatch.
final bool buildSuccess = await runConfigOnlyBuild(
example,
processRunner,
platform,
targetPlatform,
buildDebug: true,
);
if (!buildSuccess) {
printError('Unable to generate debug Xcode project files');
overallResult = RunState.failed;
continue;
}

_printRunningExampleTestsMessage(example, targetPlatformString);
final int exitCode = await _xcode.runXcodeBuild(
example.directory,
targetPlatform,
targetPlatformString,
// Clean before testing to remove cached swiftmodules from previous
// runs, which can cause conflicts.
actions: <String>['clean', 'test'],
workspace: '${targetPlatform.toLowerCase()}/Runner.xcworkspace',
workspace: '${targetPlatformString.toLowerCase()}/Runner.xcworkspace',
scheme: 'Runner',
configuration: 'Debug',
hostPlatform: platform,
Expand All @@ -513,10 +530,10 @@ this command.
const int xcodebuildNoTestExitCode = 66;
switch (exitCode) {
case xcodebuildNoTestExitCode:
_printNoExampleTestsMessage(example, targetPlatform);
_printNoExampleTestsMessage(example, targetPlatformString);
case 0:
printSuccess(
'Successfully ran $targetPlatform xctest for $exampleName');
'Successfully ran $targetPlatformString xctest for $exampleName');
// If this is the first test, assume success until something fails.
if (overallResult == RunState.skipped) {
overallResult = RunState.succeeded;
Expand Down
35 changes: 35 additions & 0 deletions script/tool/test/native_test_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ void main() {
null);
}

// Returns the ProcessCall to expect for generating the native project files
// with a --config-only build on iOS or macOS.
ProcessCall getConfigOnlyDarwinBuildCall(
Directory package, FlutterPlatform platform) {
return ProcessCall(
'flutter',
<String>[
'build',
if (platform == FlutterPlatform.ios) 'ios' else 'macos',
'--debug',
'--config-only',
],
package.path);
}

// Returns the ProcessCall to expect for running the tests in the
// workspace [platform]/Runner.xcworkspace, with the given extra flags.
ProcessCall getRunTestCall(
Expand Down Expand Up @@ -246,6 +261,8 @@ void main() {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos',
extraFlags: <String>['-only-testing:RunnerUITests']),
]));
Expand Down Expand Up @@ -317,6 +334,8 @@ void main() {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'ios'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.ios),
getRunTestCall(pluginExampleDirectory, 'ios',
destination: 'foo_destination'),
]));
Expand Down Expand Up @@ -354,6 +373,8 @@ void main() {
],
null),
getTargetCheckCall(pluginExampleDirectory, 'ios'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.ios),
getRunTestCall(pluginExampleDirectory, 'ios',
destination: 'id=$_simulatorDeviceId'),
]));
Expand Down Expand Up @@ -421,6 +442,8 @@ void main() {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos'),
]));
});
Expand Down Expand Up @@ -1305,6 +1328,8 @@ public class FlutterActivityTest {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos',
extraFlags: <String>['-only-testing:RunnerTests']),
]));
Expand Down Expand Up @@ -1340,6 +1365,8 @@ public class FlutterActivityTest {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos',
extraFlags: <String>['-only-testing:RunnerUITests']),
]));
Expand Down Expand Up @@ -1609,9 +1636,13 @@ public class FlutterActivityTest {
],
androidFolder.path),
getTargetCheckCall(pluginExampleDirectory, 'ios'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.ios),
getRunTestCall(pluginExampleDirectory, 'ios',
destination: 'foo_destination'),
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos'),
]));
});
Expand Down Expand Up @@ -1648,6 +1679,8 @@ public class FlutterActivityTest {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'macos'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.macos),
getRunTestCall(pluginExampleDirectory, 'macos'),
]));
});
Expand Down Expand Up @@ -1684,6 +1717,8 @@ public class FlutterActivityTest {
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
getTargetCheckCall(pluginExampleDirectory, 'ios'),
getConfigOnlyDarwinBuildCall(
pluginExampleDirectory, FlutterPlatform.ios),
getRunTestCall(pluginExampleDirectory, 'ios',
destination: 'foo_destination'),
]));
Expand Down