Skip to content

refactor(cli): early return and improve test coverage #1449

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

Merged
merged 1 commit into from
May 26, 2025
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
21 changes: 11 additions & 10 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,19 +554,20 @@ def commitizen_excepthook(
type, value, traceback, debug=False, no_raise: list[int] | None = None
):
traceback = traceback if isinstance(traceback, TracebackType) else None
if not isinstance(value, CommitizenException):
original_excepthook(type, value, traceback)
return

if not no_raise:
no_raise = []
if isinstance(value, CommitizenException):
if value.message:
value.output_method(value.message)
if debug:
original_excepthook(type, value, traceback)
exit_code = value.exit_code
if exit_code in no_raise:
exit_code = ExitCode.EXPECTED_EXIT
sys.exit(exit_code)
else:
if value.message:
value.output_method(value.message)
if debug:
original_excepthook(type, value, traceback)
exit_code = value.exit_code
if exit_code in no_raise:
exit_code = ExitCode.EXPECTED_EXIT
sys.exit(exit_code)


commitizen_debug_excepthook = partial(commitizen_excepthook, debug=True)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import subprocess
import sys
import types
from functools import partial

import pytest
Expand Down Expand Up @@ -182,3 +183,59 @@ def test_unknown_args_before_double_dash_raises(mocker: MockFixture):
assert "Invalid commitizen arguments were found before -- separator" in str(
excinfo.value
)


def test_commitizen_excepthook_non_commitizen_exception(mocker: MockFixture):
"""Test that commitizen_excepthook delegates to original_excepthook for non-CommitizenException."""
# Mock the original excepthook
mock_original_excepthook = mocker.Mock()
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)

# Create a regular exception
test_exception = ValueError("test error")

# Call commitizen_excepthook with the regular exception
cli.commitizen_excepthook(ValueError, test_exception, None)

# Verify original_excepthook was called with correct arguments
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)


def test_commitizen_excepthook_non_commitizen_exception_with_traceback(
mocker: MockFixture,
):
"""Test that commitizen_excepthook handles traceback correctly for non-CommitizenException."""
# Mock the original excepthook
mock_original_excepthook = mocker.Mock()
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)

# Create a regular exception with a traceback
test_exception = ValueError("test error")
test_traceback = mocker.Mock(spec=types.TracebackType)

# Call commitizen_excepthook with the regular exception and traceback
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)

# Verify original_excepthook was called with correct arguments including traceback
mock_original_excepthook.assert_called_once_with(
ValueError, test_exception, test_traceback
)


def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback(
mocker: MockFixture,
):
"""Test that commitizen_excepthook handles invalid traceback correctly for non-CommitizenException."""
# Mock the original excepthook
mock_original_excepthook = mocker.Mock()
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)

# Create a regular exception with an invalid traceback
test_exception = ValueError("test error")
test_traceback = mocker.Mock() # Not a TracebackType

# Call commitizen_excepthook with the regular exception and invalid traceback
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)

# Verify original_excepthook was called with None as traceback
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)