diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index ecf9bf52c7..2d5db2b4fe 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -46,11 +46,12 @@ class IOSSimulatorUtils { /// Returns [IOSSimulatorUtils] active in the current app context (i.e. zone). static IOSSimulatorUtils get instance => context.get(); - List getAttachedDevices() { + Future> getAttachedDevices() async { if (!xcode.isInstalledAndMeetsVersionCheck) return []; - return SimControl.instance.getConnectedDevices().map((SimDevice device) { + final List connected = await SimControl.instance.getConnectedDevices(); + return connected.map((SimDevice device) { return IOSSimulator(device.udid, name: device.name, simulatorCategory: device.category); }).toList(); } @@ -63,7 +64,7 @@ class SimControl { /// Runs `simctl list --json` and returns the JSON of the corresponding /// [section]. - Map _list(SimControlListSection section) { + Future> _list(SimControlListSection section) async { // Sample output from `simctl list --json`: // // { @@ -83,20 +84,27 @@ class SimControl { final List command = [_xcrunPath, 'simctl', 'list', '--json', section.name]; printTrace(command.join(' ')); - final ProcessResult results = processManager.runSync(command); + final ProcessResult results = await processManager.run(command); if (results.exitCode != 0) { printError('Error executing simctl: ${results.exitCode}\n${results.stderr}'); return >{}; } - - return json.decode(results.stdout)[section.name]; + try { + return json.decode(results.stdout)[section.name]; + } 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 + // returned sometimes. + printError('simctl returned non-JSON response: ${results.stdout}'); + return {}; + } } /// Returns a list of all available devices, both potential and connected. - List getDevices() { + Future> getDevices() async { final List devices = []; - final Map devicesSection = _list(SimControlListSection.devices); + final Map devicesSection = await _list(SimControlListSection.devices); for (String deviceCategory in devicesSection.keys) { final List devicesData = devicesSection[deviceCategory]; @@ -109,8 +117,9 @@ class SimControl { } /// Returns all the connected simulator devices. - List getConnectedDevices() { - return getDevices().where((SimDevice device) => device.isBooted).toList(); + Future> getConnectedDevices() async { + final List simDevices = await getDevices(); + return simDevices.where((SimDevice device) => device.isBooted).toList(); } Future isInstalled(String deviceId, String appId) { diff --git a/packages/flutter_tools/test/general.shard/ios/simulators_test.dart b/packages/flutter_tools/test/general.shard/ios/simulators_test.dart index 59aca13ac5..bafc54cd41 100644 --- a/packages/flutter_tools/test/general.shard/ios/simulators_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/simulators_test.dart @@ -390,14 +390,15 @@ void main() { setUp(() { mockProcessManager = MockProcessManager(); - when(mockProcessManager.runSync(any)) - .thenReturn(ProcessResult(mockPid, 0, validSimControlOutput, '')); + when(mockProcessManager.run(any)).thenAnswer((Invocation _) async { + return ProcessResult(mockPid, 0, validSimControlOutput, ''); + }); simControl = SimControl(); }); - testUsingContext('getDevices succeeds', () { - final List devices = simControl.getDevices(); + testUsingContext('getDevices succeeds', () async { + final List devices = await simControl.getDevices(); final SimDevice watch = devices[0]; expect(watch.category, 'watchOS 4.3'); @@ -426,6 +427,17 @@ void main() { ProcessManager: () => mockProcessManager, SimControl: () => simControl, }); + + testUsingContext('getDevices handles bad simctl output', () async { + when(mockProcessManager.run(any)) + .thenAnswer((Invocation _) async => ProcessResult(mockPid, 0, 'Install Started', '')); + final List devices = await simControl.getDevices(); + + expect(devices, isEmpty); + }, overrides: { + ProcessManager: () => mockProcessManager, + SimControl: () => simControl, + }); }); group('startApp', () { diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index 2cafbcdad1..996c2100b2 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -77,7 +77,7 @@ void testUsingContext( HttpClient: () => MockHttpClient(), IOSSimulatorUtils: () { final MockIOSSimulatorUtils mock = MockIOSSimulatorUtils(); - when(mock.getAttachedDevices()).thenReturn([]); + when(mock.getAttachedDevices()).thenAnswer((Invocation _) async => []); return mock; }, OutputPreferences: () => OutputPreferences(showColor: false), @@ -227,7 +227,7 @@ class FakeDoctor extends Doctor { class MockSimControl extends Mock implements SimControl { MockSimControl() { - when(getConnectedDevices()).thenReturn([]); + when(getConnectedDevices()).thenAnswer((Invocation _) async => []); } }