Catch FormatException parsing XCDevice._getAllDevices (#90967)

This commit is contained in:
Jenn Magder 2021-10-08 13:08:04 -07:00 committed by GitHub
parent b1126d8749
commit 8930e9b892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -112,9 +112,16 @@ class XCDevice {
throwOnError: true,
);
if (result.exitCode == 0) {
final List<dynamic> listResults = json.decode(result.stdout) as List<dynamic>;
_cachedListResults = listResults;
return listResults;
final String listOutput = result.stdout;
try {
final List<dynamic> listResults = json.decode(listOutput) as List<dynamic>;
_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) {

View File

@ -680,6 +680,19 @@ void main() {
}, overrides: <Type, Generator>{
Platform: () => macPlatform,
});
testUsingContext('handles bad output',() async {
fakeProcessManager.addCommand(const FakeCommand(
command: <String>['xcrun', 'xcdevice', 'list', '--timeout', '2'],
stdout: 'Something bad happened, not JSON',
));
final List<IOSDevice> devices = await xcdevice.getAvailableIOSDevices();
expect(devices, isEmpty);
expect(logger.errorText, contains('xcdevice returned non-JSON response'));
}, overrides: <Type, Generator>{
Platform: () => macPlatform,
});
});
group('diagnostics', () {