Skip to content

Commit

Permalink
Fix type error hidden by implicit downcasts (flutter#38102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams authored Aug 12, 2019
1 parent 2003432 commit ca5411e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/flutter_tools/lib/src/ios/simulators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ class SimControl {
return <String, Map<String, dynamic>>{};
}
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 {
// We failed to parse the simctl output, or it returned junk.
// One known message is "Install Started" isn't valid JSON but is
Expand All @@ -107,9 +112,11 @@ class SimControl {
final Map<String, dynamic> devicesSection = await _list(SimControlListSection.devices);

for (String deviceCategory in devicesSection.keys) {
final List<dynamic> devicesData = devicesSection[deviceCategory];
for (Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) {
devices.add(SimDevice(deviceCategory, data));
final Object devicesData = devicesSection[deviceCategory];
if (devicesData != null && devicesData is List<dynamic>) {
for (Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) {
devices.add(SimDevice(deviceCategory, data));
}
}
}

Expand Down Expand Up @@ -220,10 +227,10 @@ class SimDevice {
final String category;
final Map<String, dynamic> data;

String get state => data['state'];
String get availability => data['availability'];
String get name => data['name'];
String get udid => data['udid'];
String get state => data['state']?.toString();
String get availability => data['availability']?.toString();
String get name => data['name']?.toString();
String get udid => data['udid']?.toString();

bool get isBooted => state == 'Booted';
}
Expand Down Expand Up @@ -267,7 +274,7 @@ class IOSSimulator extends Device {
Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false;

@override
Future<bool> installApp(ApplicationPackage app) async {
Future<bool> installApp(covariant IOSApp app) async {
try {
final IOSApp iosApp = app;
await SimControl.instance.install(id, iosApp.simulatorBundlePath);
Expand Down Expand Up @@ -316,7 +323,7 @@ class IOSSimulator extends Device {

@override
Future<LaunchResult> startApp(
ApplicationPackage package, {
covariant IOSApp package, {
String mainPath,
String route,
DebuggingOptions debuggingOptions,
Expand All @@ -325,7 +332,7 @@ class IOSSimulator extends Device {
bool usesTerminalUi = true,
bool ipv6 = false,
}) async {
if (!prebuiltApplication) {
if (!prebuiltApplication && package is BuildableIOSApp) {
printTrace('Building ${package.name} for $id.');

try {
Expand Down Expand Up @@ -371,8 +378,7 @@ class IOSSimulator extends Device {
// which should always yield the correct value and does not require
// parsing the xcodeproj or configuration files.
// See https://github.com/flutter/flutter/issues/31037 for more information.
final IOSApp iosApp = package;
final String plistPath = fs.path.join(iosApp.simulatorBundlePath, 'Info.plist');
final String plistPath = fs.path.join(package.simulatorBundlePath, 'Info.plist');
final String bundleIdentifier = iosWorkflow.getPlistValueFromFile(plistPath, kCFBundleIdentifierKey);

await SimControl.instance.launch(id, bundleIdentifier, args);
Expand Down Expand Up @@ -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);

// Step 1: Build the Xcode project.
Expand All @@ -422,8 +428,7 @@ class IOSSimulator extends Device {
throwToolExit('Could not build the application for the simulator.');

// Step 2: Assert that the Xcode project was successfully built.
final IOSApp iosApp = app;
final Directory bundle = fs.directory(iosApp.simulatorBundlePath);
final Directory bundle = fs.directory(app.simulatorBundlePath);
final bool bundleExists = bundle.existsSync();
if (!bundleExists)
throwToolExit('Could not find the built application bundle at ${bundle.path}.');
Expand Down Expand Up @@ -463,11 +468,11 @@ class IOSSimulator extends Device {

Future<int> get sdkMajorVersion async {
final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion);
return int.parse(sdkMatch?.group(2) ?? 11);
return int.parse(sdkMatch?.group(2) ?? '11');
}

@override
DeviceLogReader getLogReader({ ApplicationPackage app }) {
DeviceLogReader getLogReader({ covariant IOSApp app }) {
assert(app is IOSApp);
_logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{};
return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ void main() {
ProcessManager: () => mockProcessManager,
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', () {
Expand Down

0 comments on commit ca5411e

Please sign in to comment.