From 8930e9b89218b6aec80d283c111f72d00a4ac456 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Fri, 8 Oct 2021 13:08:04 -0700 Subject: [PATCH] Catch FormatException parsing XCDevice._getAllDevices (#90967) --- packages/flutter_tools/lib/src/macos/xcdevice.dart | 13 ++++++++++--- .../test/general.shard/macos/xcode_test.dart | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/macos/xcdevice.dart b/packages/flutter_tools/lib/src/macos/xcdevice.dart index 884f3a78d97..00f8c67b3cc 100644 --- a/packages/flutter_tools/lib/src/macos/xcdevice.dart +++ b/packages/flutter_tools/lib/src/macos/xcdevice.dart @@ -112,9 +112,16 @@ class XCDevice { throwOnError: true, ); if (result.exitCode == 0) { - final List listResults = json.decode(result.stdout) as List; - _cachedListResults = listResults; - return listResults; + final String listOutput = result.stdout; + try { + final List listResults = json.decode(listOutput) as List; + _cachedListResults = listResults; + return listResults; + } on FormatException { + // xcdevice logs errors and crashes to stdout. + _logger.printError('xcdevice returned non-JSON response: $listOutput'); + return null; + } } _logger.printTrace('xcdevice returned an error:\n${result.stderr}'); } on ProcessException catch (exception) { diff --git a/packages/flutter_tools/test/general.shard/macos/xcode_test.dart b/packages/flutter_tools/test/general.shard/macos/xcode_test.dart index 4b6506690da..430ac0709af 100644 --- a/packages/flutter_tools/test/general.shard/macos/xcode_test.dart +++ b/packages/flutter_tools/test/general.shard/macos/xcode_test.dart @@ -680,6 +680,19 @@ void main() { }, overrides: { Platform: () => macPlatform, }); + + testUsingContext('handles bad output',() async { + fakeProcessManager.addCommand(const FakeCommand( + command: ['xcrun', 'xcdevice', 'list', '--timeout', '2'], + stdout: 'Something bad happened, not JSON', + )); + + final List devices = await xcdevice.getAvailableIOSDevices(); + expect(devices, isEmpty); + expect(logger.errorText, contains('xcdevice returned non-JSON response')); + }, overrides: { + Platform: () => macPlatform, + }); }); group('diagnostics', () {