From 069dc9c72d94459c3b31402d7e5f36b9674c0c03 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Mon, 27 Apr 2020 15:04:02 -0700 Subject: [PATCH] [flutter_tools] catch ProcessException and throw ToolExit during upgrade (#55759) --- .../lib/src/commands/upgrade.dart | 19 ++++++++-------- .../permeable/upgrade_test.dart | 22 ++++++++++++++++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/upgrade.dart b/packages/flutter_tools/lib/src/commands/upgrade.dart index 00fa3e9f2c..21d156936b 100644 --- a/packages/flutter_tools/lib/src/commands/upgrade.dart +++ b/packages/flutter_tools/lib/src/commands/upgrade.dart @@ -137,7 +137,7 @@ class UpgradeCommandRunner { } recordState(flutterVersion); await upgradeChannel(flutterVersion); - await attemptReset(flutterVersion, upstreamRevision); + await attemptReset(upstreamRevision); if (!testFlow) { await flutterUpgradeContinue(); } @@ -243,14 +243,15 @@ class UpgradeCommandRunner { /// This is a reset instead of fast forward because if we are on a release /// branch with cherry picks, there may not be a direct fast-forward route /// to the next release. - Future attemptReset(FlutterVersion oldFlutterVersion, String newRevision) async { - final RunResult result = await processUtils.run( - ['git', 'reset', '--hard', newRevision], - throwOnError: true, - workingDirectory: workingDirectory, - ); - if (result.exitCode != 0) { - throwToolExit(null, exitCode: result.exitCode); + Future attemptReset(String newRevision) async { + try { + await processUtils.run( + ['git', 'reset', '--hard', newRevision], + throwOnError: true, + workingDirectory: workingDirectory, + ); + } on ProcessException catch (e) { + throwToolExit(e.message, exitCode: e.errorCode); } } diff --git a/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart b/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart index 4068dce21a..633ff276d7 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart @@ -186,6 +186,26 @@ void main() { Platform: () => fakePlatform, }); + testUsingContext('git exception during attemptReset throwsToolExit', () async { + const String revision = 'abc123'; + const String errorMessage = 'fatal: Could not parse object ´$revision´'; + when(processManager.run( + ['git', 'reset', '--hard', revision] + )).thenThrow(const ProcessException( + 'git', + ['reset', '--hard', revision], + errorMessage, + )); + + expect( + () async => await realCommandRunner.attemptReset(revision), + throwsToolExit(message: errorMessage), + ); + }, overrides: { + ProcessManager: () => processManager, + Platform: () => fakePlatform, + }); + testUsingContext('flutterUpgradeContinue passes env variables to child process', () async { await realCommandRunner.flutterUpgradeContinue(); @@ -315,7 +335,7 @@ class FakeUpgradeCommandRunner extends UpgradeCommandRunner { Future upgradeChannel(FlutterVersion flutterVersion) async {} @override - Future attemptReset(FlutterVersion flutterVersion, String newRevision) async => alreadyUpToDate; + Future attemptReset(String newRevision) async {} @override Future precacheArtifacts() async {}