From 0927c3aab183dbdcd4fedb1bbd09159357443429 Mon Sep 17 00:00:00 2001 From: Erick Date: Fri, 14 Jun 2024 15:05:45 -0300 Subject: [PATCH] fix: handling process exit on abort (#2238) --- .../lib/src/shorebird_cli_command_runner.dart | 5 ++- .../shorebird_cli_command_runner_test.dart | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart b/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart index 86558a54..3d4482d4 100644 --- a/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart +++ b/packages/shorebird_cli/lib/src/shorebird_cli_command_runner.dart @@ -16,6 +16,7 @@ import 'package:shorebird_cli/src/shorebird_env.dart'; import 'package:shorebird_cli/src/shorebird_flutter.dart'; import 'package:shorebird_cli/src/shorebird_process.dart'; import 'package:shorebird_cli/src/shorebird_version.dart'; +import 'package:shorebird_cli/src/third_party/flutter_tools/lib/flutter_tools.dart'; import 'package:shorebird_cli/src/version.dart'; const executableName = 'shorebird'; @@ -197,6 +198,8 @@ Engine • revision ${shorebirdEnv.shorebirdEngineRevision}'''); } else { try { exitCode = await super.runCommand(topLevelResults); + } on ProcessExit catch (error) { + exitCode = error.exitCode; } catch (error, stackTrace) { logger ..err('$error') @@ -205,7 +208,7 @@ Engine • revision ${shorebirdEnv.shorebirdEngineRevision}'''); } } - if (exitCode == ExitCode.software.code && logger.level != Level.verbose) { + if (exitCode != ExitCode.success.code && logger.level != Level.verbose) { final fileAnIssue = link( uri: Uri.parse( 'https://github.com/shorebirdtech/shorebird/issues/new/choose', diff --git a/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart b/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart index f9680505..c800e4c4 100644 --- a/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart +++ b/packages/shorebird_cli/test/src/shorebird_cli_command_runner_test.dart @@ -8,9 +8,11 @@ import 'package:scoped_deps/scoped_deps.dart'; import 'package:shorebird_cli/src/logger.dart' hide logger; import 'package:shorebird_cli/src/platform.dart'; import 'package:shorebird_cli/src/shorebird_cli_command_runner.dart'; +import 'package:shorebird_cli/src/shorebird_command.dart'; import 'package:shorebird_cli/src/shorebird_env.dart'; import 'package:shorebird_cli/src/shorebird_flutter.dart'; import 'package:shorebird_cli/src/shorebird_version.dart'; +import 'package:shorebird_cli/src/third_party/flutter_tools/lib/flutter_tools.dart'; import 'package:shorebird_cli/src/version.dart'; import 'package:test/test.dart'; @@ -68,6 +70,31 @@ void main() { commandRunner = runWithOverrides(ShorebirdCliCommandRunner.new); }); + group('handles ProcessExit', () { + test('does nothing when exit code is 0', () async { + commandRunner.addCommand(_TestCommand(ExitCode.success)); + final result = await runWithOverrides( + () => commandRunner.run(['test']), + ); + expect(result, equals(ExitCode.success.code)); + }); + + test('exits with the correct code', () async { + commandRunner.addCommand(_TestCommand(ExitCode.unavailable)); + final result = await runWithOverrides( + () => commandRunner.run(['test']), + ); + expect(result, equals(ExitCode.unavailable.code)); + verify( + () => logger.info( + any( + that: contains('''If you aren't sure why this command failed'''), + ), + ), + ).called(1); + }); + }); + test('handles FormatException', () async { const exception = FormatException('oops!'); var isFirstInvocation = true; @@ -386,3 +413,20 @@ Engine • revision $shorebirdEngineRevision''', }); }); } + +class _TestCommand extends ShorebirdCommand { + _TestCommand(this.exitCode); + + final ExitCode exitCode; + + @override + String get name => 'test'; + + @override + String get description => 'Test command'; + + @override + Future run() async { + throw ProcessExit(exitCode.code); + } +}