Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit with exit code 1 when there's an exception #4122

Merged
merged 7 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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