From 410af14fd8396b067c4cd6c545bb9c1042a81089 Mon Sep 17 00:00:00 2001 From: Victoria Ashworth <15619084+vashworth@users.noreply.github.com> Date: Tue, 12 Dec 2023 14:05:58 -0600 Subject: [PATCH] Select simulator runtime for tests based on Xcode's preferred runtime build (#139919) When creating a simulator for a test, select the runtime based on the selected Xcode's preferred build. This is to prevent it from using a runtime greater than its greatest supported version. Fixes https://github.com/flutter/flutter/issues/139917. Example test with both iOS 16 and 17 available: https://chromium-swarm.appspot.com/task?id=6672aca184395a10 --- dev/devicelab/lib/framework/ios.dart | 45 ++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/dev/devicelab/lib/framework/ios.dart b/dev/devicelab/lib/framework/ios.dart index c663c4422d..ae47a65768 100644 --- a/dev/devicelab/lib/framework/ios.dart +++ b/dev/devicelab/lib/framework/ios.dart @@ -56,8 +56,6 @@ Future testWithNewIOSSimulator( SimulatorFunction testFunction, { String deviceTypeId = 'com.apple.CoreSimulator.SimDeviceType.iPhone-11', }) async { - // Xcode 11.4 simctl create makes the runtime argument optional, and defaults to latest. - // TODO(jmagman): Remove runtime parsing when devicelab upgrades to Xcode 11.4 https://github.com/flutter/flutter/issues/54889 final String availableRuntimes = await eval( 'xcrun', [ @@ -68,11 +66,48 @@ Future testWithNewIOSSimulator( workingDirectory: flutterDirectory.path, ); + final String runtimesForSelectedXcode = await eval( + 'xcrun', + [ + 'simctl', + 'runtime', + 'match', + 'list', + '--json', + ], + workingDirectory: flutterDirectory.path, + ); + + // Get the preferred runtime build for the selected Xcode version. Preferred + // means the runtime was either bundled with Xcode, exactly matched your SDK + // version, or it's indicated a better match for your SDK. + final Map decodeResult = json.decode(runtimesForSelectedXcode) as Map; + final String? iosKey = decodeResult.keys + .where((String key) => key.contains('iphoneos')) + .firstOrNull; + final Object? iosDetails = decodeResult[iosKey]; + String? runtimeBuildForSelectedXcode; + if (iosDetails != null && iosDetails is Map) { + final Object? preferredBuild = iosDetails['preferredBuild']; + if (preferredBuild is String) { + runtimeBuildForSelectedXcode = preferredBuild; + } + } + String? iOSSimRuntime; final RegExp iOSRuntimePattern = RegExp(r'iOS .*\) - (.*)'); + // [availableRuntimes] may include runtime versions greater than the selected + // Xcode's greatest supported version. Use [runtimeBuildForSelectedXcode] when + // possible to pick which runtime to use. + // For example, iOS 17 (released with Xcode 15) may be available even if the + // selected Xcode version is 14. for (final String runtime in LineSplitter.split(availableRuntimes)) { + if (runtimeBuildForSelectedXcode != null && + !runtime.contains(runtimeBuildForSelectedXcode)) { + continue; + } // These seem to be in order, so allow matching multiple lines so it grabs // the last (hopefully latest) one. final RegExpMatch? iOSRuntimeMatch = iOSRuntimePattern.firstMatch(runtime); @@ -82,7 +117,11 @@ Future testWithNewIOSSimulator( } } if (iOSSimRuntime == null) { - throw 'No iOS simulator runtime found. Available runtimes:\n$availableRuntimes'; + if (runtimeBuildForSelectedXcode != null) { + throw 'iOS simulator runtime $runtimeBuildForSelectedXcode not found. Available runtimes:\n$availableRuntimes'; + } else { + throw 'No iOS simulator runtime found. Available runtimes:\n$availableRuntimes'; + } } final String deviceId = await eval(