From 80b53d7cc6c37cd061b57de08cb5365e0c95ab48 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar Date: Wed, 28 Aug 2024 15:57:21 +0100 Subject: [PATCH 1/5] Exit with exit code when there's exception Signed-off-by: Ankita Katiyar --- kedro/framework/cli/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kedro/framework/cli/cli.py b/kedro/framework/cli/cli.py index 2ef5ee66dc..59790c7419 100644 --- a/kedro/framework/cli/cli.py +++ b/kedro/framework/cli/cli.py @@ -210,7 +210,8 @@ def main( project_metadata=self._metadata, command_args=args, exit_code=1 ) hook_called = True - raise + traceback.print_exception() + sys.exit(1) finally: if not hook_called: self._cli_hook_manager.hook.after_command_run( From 373cd11d40114930f6107e1cb43010454bba4020 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar Date: Wed, 28 Aug 2024 16:12:03 +0100 Subject: [PATCH 2/5] code cov Signed-off-by: Ankita Katiyar --- tests/framework/cli/test_cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/framework/cli/test_cli.py b/tests/framework/cli/test_cli.py index 9aa2606587..89dc554c01 100644 --- a/tests/framework/cli/test_cli.py +++ b/tests/framework/cli/test_cli.py @@ -506,7 +506,7 @@ 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") + # @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,7 +522,7 @@ def test_main_hook_exception_handling(self, fake_metadata): assert result.exit_code == 1 - @patch("sys.exit") + # @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() From 8554d71712b8b39b9554d0c4ed3fd2fcbf5319a7 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar Date: Thu, 29 Aug 2024 12:44:15 +0100 Subject: [PATCH 3/5] cov Signed-off-by: Ankita Katiyar --- kedro/framework/cli/cli.py | 2 +- tests/framework/cli/test_cli.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/kedro/framework/cli/cli.py b/kedro/framework/cli/cli.py index 59790c7419..b9a053830e 100644 --- a/kedro/framework/cli/cli.py +++ b/kedro/framework/cli/cli.py @@ -210,7 +210,7 @@ def main( project_metadata=self._metadata, command_args=args, exit_code=1 ) hook_called = True - traceback.print_exception() + traceback.print_exc() sys.exit(1) finally: if not hook_called: diff --git a/tests/framework/cli/test_cli.py b/tests/framework/cli/test_cli.py index 89dc554c01..5767142a49 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,7 +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() From 893c8b172bb5b33ff7a2e029def80da42d535f34 Mon Sep 17 00:00:00 2001 From: Ankita Katiyar Date: Fri, 30 Aug 2024 16:16:59 +0100 Subject: [PATCH 4/5] Remove finally block Signed-off-by: Ankita Katiyar --- kedro/framework/cli/cli.py | 9 --------- tests/framework/cli/test_cli.py | 14 -------------- 2 files changed, 23 deletions(-) diff --git a/kedro/framework/cli/cli.py b/kedro/framework/cli/cli.py index b9a053830e..3b11b5e646 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" @@ -209,14 +206,8 @@ def main( self._cli_hook_manager.hook.after_command_run( project_metadata=self._metadata, command_args=args, exit_code=1 ) - hook_called = True traceback.print_exc() sys.exit(1) - 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 5767142a49..e243ef73e1 100644 --- a/tests/framework/cli/test_cli.py +++ b/tests/framework/cli/test_cli.py @@ -521,20 +521,6 @@ def test_main_hook_exception_handling(self, fake_metadata): assert result.exit_code == 1 - 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: From c7779347758e4a4cc0d49aab5594839b749f9b0e Mon Sep 17 00:00:00 2001 From: Ankita Katiyar Date: Mon, 2 Sep 2024 15:55:23 +0100 Subject: [PATCH 5/5] Fix indentation Signed-off-by: Ankita Katiyar --- kedro/framework/cli/cli.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kedro/framework/cli/cli.py b/kedro/framework/cli/cli.py index 3b11b5e646..b22fa70d00 100644 --- a/kedro/framework/cli/cli.py +++ b/kedro/framework/cli/cli.py @@ -201,13 +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 ) - traceback.print_exc() - sys.exit(1) + raise @property def global_groups(self) -> Sequence[click.MultiCommand]: