Unverified Commit ca5411e3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Fix type error hidden by implicit downcasts (#38102)

parent 2003432c
...@@ -90,7 +90,12 @@ class SimControl { ...@@ -90,7 +90,12 @@ class SimControl {
return <String, Map<String, dynamic>>{}; return <String, Map<String, dynamic>>{};
} }
try { try {
return json.decode(results.stdout)[section.name]; final Object decodeResult = json.decode(results.stdout?.toString())[section.name];
if (decodeResult is Map<String, dynamic>) {
return decodeResult;
}
printError('simctl returned unexpected JSON response: ${results.stdout}');
return <String, dynamic>{};
} on FormatException { } on FormatException {
// We failed to parse the simctl output, or it returned junk. // We failed to parse the simctl output, or it returned junk.
// One known message is "Install Started" isn't valid JSON but is // One known message is "Install Started" isn't valid JSON but is
...@@ -107,9 +112,11 @@ class SimControl { ...@@ -107,9 +112,11 @@ class SimControl {
final Map<String, dynamic> devicesSection = await _list(SimControlListSection.devices); final Map<String, dynamic> devicesSection = await _list(SimControlListSection.devices);
for (String deviceCategory in devicesSection.keys) { for (String deviceCategory in devicesSection.keys) {
final List<dynamic> devicesData = devicesSection[deviceCategory]; final Object devicesData = devicesSection[deviceCategory];
for (Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) { if (devicesData != null && devicesData is List<dynamic>) {
devices.add(SimDevice(deviceCategory, data)); for (Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) {
devices.add(SimDevice(deviceCategory, data));
}
} }
} }
...@@ -220,10 +227,10 @@ class SimDevice { ...@@ -220,10 +227,10 @@ class SimDevice {
final String category; final String category;
final Map<String, dynamic> data; final Map<String, dynamic> data;
String get state => data['state']; String get state => data['state']?.toString();
String get availability => data['availability']; String get availability => data['availability']?.toString();
String get name => data['name']; String get name => data['name']?.toString();
String get udid => data['udid']; String get udid => data['udid']?.toString();
bool get isBooted => state == 'Booted'; bool get isBooted => state == 'Booted';
} }
...@@ -267,7 +274,7 @@ class IOSSimulator extends Device { ...@@ -267,7 +274,7 @@ class IOSSimulator extends Device {
Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false; Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false;
@override @override
Future<bool> installApp(ApplicationPackage app) async { Future<bool> installApp(covariant IOSApp app) async {
try { try {
final IOSApp iosApp = app; final IOSApp iosApp = app;
await SimControl.instance.install(id, iosApp.simulatorBundlePath); await SimControl.instance.install(id, iosApp.simulatorBundlePath);
...@@ -316,7 +323,7 @@ class IOSSimulator extends Device { ...@@ -316,7 +323,7 @@ class IOSSimulator extends Device {
@override @override
Future<LaunchResult> startApp( Future<LaunchResult> startApp(
ApplicationPackage package, { covariant IOSApp package, {
String mainPath, String mainPath,
String route, String route,
DebuggingOptions debuggingOptions, DebuggingOptions debuggingOptions,
...@@ -325,7 +332,7 @@ class IOSSimulator extends Device { ...@@ -325,7 +332,7 @@ class IOSSimulator extends Device {
bool usesTerminalUi = true, bool usesTerminalUi = true,
bool ipv6 = false, bool ipv6 = false,
}) async { }) async {
if (!prebuiltApplication) { if (!prebuiltApplication && package is BuildableIOSApp) {
printTrace('Building ${package.name} for $id.'); printTrace('Building ${package.name} for $id.');
try { try {
...@@ -371,8 +378,7 @@ class IOSSimulator extends Device { ...@@ -371,8 +378,7 @@ class IOSSimulator extends Device {
// which should always yield the correct value and does not require // which should always yield the correct value and does not require
// parsing the xcodeproj or configuration files. // parsing the xcodeproj or configuration files.
// See https://github.com/flutter/flutter/issues/31037 for more information. // See https://github.com/flutter/flutter/issues/31037 for more information.
final IOSApp iosApp = package; final String plistPath = fs.path.join(package.simulatorBundlePath, 'Info.plist');
final String plistPath = fs.path.join(iosApp.simulatorBundlePath, 'Info.plist');
final String bundleIdentifier = iosWorkflow.getPlistValueFromFile(plistPath, kCFBundleIdentifierKey); final String bundleIdentifier = iosWorkflow.getPlistValueFromFile(plistPath, kCFBundleIdentifierKey);
await SimControl.instance.launch(id, bundleIdentifier, args); await SimControl.instance.launch(id, bundleIdentifier, args);
...@@ -400,7 +406,7 @@ class IOSSimulator extends Device { ...@@ -400,7 +406,7 @@ class IOSSimulator extends Device {
} }
} }
Future<void> _setupUpdatedApplicationBundle(ApplicationPackage app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async { Future<void> _setupUpdatedApplicationBundle(covariant BuildableIOSApp app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async {
await _sideloadUpdatedAssetsForInstalledApplicationBundle(app, buildInfo, mainPath); await _sideloadUpdatedAssetsForInstalledApplicationBundle(app, buildInfo, mainPath);
// Step 1: Build the Xcode project. // Step 1: Build the Xcode project.
...@@ -422,8 +428,7 @@ class IOSSimulator extends Device { ...@@ -422,8 +428,7 @@ class IOSSimulator extends Device {
throwToolExit('Could not build the application for the simulator.'); throwToolExit('Could not build the application for the simulator.');
// Step 2: Assert that the Xcode project was successfully built. // Step 2: Assert that the Xcode project was successfully built.
final IOSApp iosApp = app; final Directory bundle = fs.directory(app.simulatorBundlePath);
final Directory bundle = fs.directory(iosApp.simulatorBundlePath);
final bool bundleExists = bundle.existsSync(); final bool bundleExists = bundle.existsSync();
if (!bundleExists) if (!bundleExists)
throwToolExit('Could not find the built application bundle at ${bundle.path}.'); throwToolExit('Could not find the built application bundle at ${bundle.path}.');
...@@ -463,11 +468,11 @@ class IOSSimulator extends Device { ...@@ -463,11 +468,11 @@ class IOSSimulator extends Device {
Future<int> get sdkMajorVersion async { Future<int> get sdkMajorVersion async {
final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion); final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion);
return int.parse(sdkMatch?.group(2) ?? 11); return int.parse(sdkMatch?.group(2) ?? '11');
} }
@override @override
DeviceLogReader getLogReader({ ApplicationPackage app }) { DeviceLogReader getLogReader({ covariant IOSApp app }) {
assert(app is IOSApp); assert(app is IOSApp);
_logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{}; _logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{};
return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app)); return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app));
......
...@@ -438,6 +438,12 @@ void main() { ...@@ -438,6 +438,12 @@ void main() {
ProcessManager: () => mockProcessManager, ProcessManager: () => mockProcessManager,
SimControl: () => simControl, SimControl: () => simControl,
}); });
testUsingContext('sdkMajorVersion defaults to 11 when sdkNameAndVersion is junk', () async {
final IOSSimulator iosSimulatorA = IOSSimulator('x', name: 'Testo', simulatorCategory: 'NaN');
expect(await iosSimulatorA.sdkMajorVersion, 11);
});
}); });
group('startApp', () { group('startApp', () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment