From a10690a77531ac662dba00c1f36d62ade5798534 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar <110245118+ankatiyar@users.noreply.github.com> Date: Tue, 3 Sep 2024 09:58:37 +0100 Subject: [PATCH] Exit with exit code 1 when there's an exception (#4122) * Exit with exit code when there's exception Signed-off-by: Ankita Katiyar * code cov Signed-off-by: Ankita Katiyar * cov Signed-off-by: Ankita Katiyar * Remove finally block Signed-off-by: Ankita Katiyar * Fix indentation Signed-off-by: Ankita Katiyar --------- Signed-off-by: Ankita Katiyar --- kedro/framework/cli/cli.py | 11 +---------- tests/framework/cli/test_cli.py | 16 ---------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/kedro/framework/cli/cli.py b/kedro/framework/cli/cli.py index 2ef5ee66dc..b22fa70d00 100644 --- a/kedro/framework/cli/cli.py +++ b/kedro/framework/cli/cli.py @@ -156,8 +156,6 @@ def main( project_metadata=self._metadata, command_args=args ) - hook_called = False - try: super().main( args=args, @@ -173,7 +171,6 @@ def main( self._cli_hook_manager.hook.after_command_run( project_metadata=self._metadata, command_args=args, exit_code=exc.code ) - hook_called = True # When CLI is run outside of a project, project_groups are not registered catch_exception = "click.exceptions.UsageError: No such command" @@ -204,18 +201,12 @@ def main( ) click.echo(message) click.echo(hint) - sys.exit(exc.code) + sys.exit(exc.code) except Exception: self._cli_hook_manager.hook.after_command_run( project_metadata=self._metadata, command_args=args, exit_code=1 ) - hook_called = True raise - finally: - if not hook_called: - self._cli_hook_manager.hook.after_command_run( - project_metadata=self._metadata, command_args=args, exit_code=0 - ) @property def global_groups(self) -> Sequence[click.MultiCommand]: diff --git a/tests/framework/cli/test_cli.py b/tests/framework/cli/test_cli.py index 9aa2606587..e243ef73e1 100644 --- a/tests/framework/cli/test_cli.py +++ b/tests/framework/cli/test_cli.py @@ -506,7 +506,6 @@ def test_kedro_cli_with_project(self, mocker, fake_metadata): assert "Global commands from Kedro" in result.output assert "Project specific commands from Kedro" in result.output - @patch("sys.exit") def test_main_hook_exception_handling(self, fake_metadata): kedro_cli = KedroCLI(fake_metadata.project_path) kedro_cli._cli_hook_manager.hook.after_command_run = MagicMock() @@ -522,21 +521,6 @@ def test_main_hook_exception_handling(self, fake_metadata): assert result.exit_code == 1 - @patch("sys.exit") - def test_main_hook_finally_block(self, fake_metadata): - kedro_cli = KedroCLI(fake_metadata.project_path) - kedro_cli._cli_hook_manager.hook.after_command_run = MagicMock() - - # No exception is raised, so it should go to the finally block and call the hook - with patch.object(click.CommandCollection, "main"): - result = CliRunner().invoke(kedro_cli, []) - - kedro_cli._cli_hook_manager.hook.after_command_run.assert_called_once_with( - project_metadata=kedro_cli._metadata, command_args=[], exit_code=0 - ) - - assert result.exit_code == 0 - @mark.usefixtures("chdir_to_dummy_project") class TestRunCommand: