Skip to content

Commit

Permalink
Exit with exit code 1 when there's an exception (#4122)
Browse files Browse the repository at this point in the history
* Exit with exit code when there's exception

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>

* code cov

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>

* cov

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>

* Remove finally block

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>

* Fix indentation

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>

---------

Signed-off-by: Ankita Katiyar <ankitakatiyar2401@gmail.com>
  • Loading branch information
ankatiyar authored Sep 3, 2024
1 parent 7a16e1a commit a10690a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 26 deletions.
11 changes: 1 addition & 10 deletions kedro/framework/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ def main(
project_metadata=self._metadata, command_args=args
)

hook_called = False

try:
super().main(
args=args,
Expand All @@ -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"
Expand Down Expand Up @@ -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]:
Expand Down
16 changes: 0 additions & 16 deletions tests/framework/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand Down

0 comments on commit a10690a

Please sign in to comment.