From 071d4eb2c8cb20f2f28c0740046ecfae69e5c7ae Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Tue, 17 Mar 2020 12:55:57 -0700 Subject: [PATCH] explicitly catch ArgumentError, and add tests (#52757) --- .../flutter_tools/lib/src/base/process.dart | 6 ++++ .../test/general.shard/base/process_test.dart | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index eddf2c5e4fe..3ceff0cdff5 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -542,6 +542,9 @@ class _DefaultProcessUtils implements ProcessUtils { } on Exception catch (error) { _logger.printTrace('$cli failed with $error'); return false; + } on ArgumentError catch (error) { + _logger.printTrace('$cli failed with $error'); + return false; } } @@ -556,6 +559,9 @@ class _DefaultProcessUtils implements ProcessUtils { } on Exception catch (error) { _logger.printTrace('$cli failed with $error'); return false; + } on ArgumentError catch (error) { + _logger.printTrace('$cli failed with $error'); + return false; } } diff --git a/packages/flutter_tools/test/general.shard/base/process_test.dart b/packages/flutter_tools/test/general.shard/base/process_test.dart index f8ed20a2c42..7d5b17a5f2e 100644 --- a/packages/flutter_tools/test/general.shard/base/process_test.dart +++ b/packages/flutter_tools/test/general.shard/base/process_test.dart @@ -360,6 +360,20 @@ void main() { ); expect(processUtils.exitsHappySync(['boohoo']), isFalse); }); + + testWithoutContext('catches Exception and returns false', () { + when(mockProcessManager.runSync(['boohoo'])).thenThrow( + const ProcessException('Process failed', []), + ); + expect(processUtils.exitsHappySync(['boohoo']), isFalse); + }); + + testWithoutContext('catches ArgumentError and returns false', () { + when(mockProcessManager.runSync(['nonesuch'])).thenThrow( + ArgumentError('Invalid argument(s): Cannot find executable for nonesuch') + ); + expect(processUtils.exitsHappySync(['nonesuch']), isFalse); + }); }); group('exitsHappy', () { @@ -387,6 +401,20 @@ void main() { }); expect(await processUtils.exitsHappy(['boohoo']), isFalse); }); + + testWithoutContext('catches Exception and returns false', () async { + when(mockProcessManager.run(['boohoo'])).thenThrow( + const ProcessException('Process failed', []), + ); + expect(await processUtils.exitsHappy(['boohoo']), isFalse); + }); + + testWithoutContext('catches ArgumentError and returns false', () async { + when(mockProcessManager.run(['nonesuch'])).thenThrow( + ArgumentError('Invalid argument(s): Cannot find executable for nonesuch'), + ); + expect(await processUtils.exitsHappy(['nonesuch']), isFalse); + }); }); }