Skip to content

Commit 83be38a

Browse files
bearomorphismLee-W
authored andcommitted
refactor(cli): early return and improve test coverage
1 parent 9584c49 commit 83be38a

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

commitizen/cli.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,19 +554,20 @@ def commitizen_excepthook(
554554
type, value, traceback, debug=False, no_raise: list[int] | None = None
555555
):
556556
traceback = traceback if isinstance(traceback, TracebackType) else None
557+
if not isinstance(value, CommitizenException):
558+
original_excepthook(type, value, traceback)
559+
return
560+
557561
if not no_raise:
558562
no_raise = []
559-
if isinstance(value, CommitizenException):
560-
if value.message:
561-
value.output_method(value.message)
562-
if debug:
563-
original_excepthook(type, value, traceback)
564-
exit_code = value.exit_code
565-
if exit_code in no_raise:
566-
exit_code = ExitCode.EXPECTED_EXIT
567-
sys.exit(exit_code)
568-
else:
563+
if value.message:
564+
value.output_method(value.message)
565+
if debug:
569566
original_excepthook(type, value, traceback)
567+
exit_code = value.exit_code
568+
if exit_code in no_raise:
569+
exit_code = ExitCode.EXPECTED_EXIT
570+
sys.exit(exit_code)
570571

571572

572573
commitizen_debug_excepthook = partial(commitizen_excepthook, debug=True)

tests/test_cli.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import subprocess
33
import sys
4+
import types
45
from functools import partial
56

67
import pytest
@@ -182,3 +183,59 @@ def test_unknown_args_before_double_dash_raises(mocker: MockFixture):
182183
assert "Invalid commitizen arguments were found before -- separator" in str(
183184
excinfo.value
184185
)
186+
187+
188+
def test_commitizen_excepthook_non_commitizen_exception(mocker: MockFixture):
189+
"""Test that commitizen_excepthook delegates to original_excepthook for non-CommitizenException."""
190+
# Mock the original excepthook
191+
mock_original_excepthook = mocker.Mock()
192+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
193+
194+
# Create a regular exception
195+
test_exception = ValueError("test error")
196+
197+
# Call commitizen_excepthook with the regular exception
198+
cli.commitizen_excepthook(ValueError, test_exception, None)
199+
200+
# Verify original_excepthook was called with correct arguments
201+
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)
202+
203+
204+
def test_commitizen_excepthook_non_commitizen_exception_with_traceback(
205+
mocker: MockFixture,
206+
):
207+
"""Test that commitizen_excepthook handles traceback correctly for non-CommitizenException."""
208+
# Mock the original excepthook
209+
mock_original_excepthook = mocker.Mock()
210+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
211+
212+
# Create a regular exception with a traceback
213+
test_exception = ValueError("test error")
214+
test_traceback = mocker.Mock(spec=types.TracebackType)
215+
216+
# Call commitizen_excepthook with the regular exception and traceback
217+
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)
218+
219+
# Verify original_excepthook was called with correct arguments including traceback
220+
mock_original_excepthook.assert_called_once_with(
221+
ValueError, test_exception, test_traceback
222+
)
223+
224+
225+
def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback(
226+
mocker: MockFixture,
227+
):
228+
"""Test that commitizen_excepthook handles invalid traceback correctly for non-CommitizenException."""
229+
# Mock the original excepthook
230+
mock_original_excepthook = mocker.Mock()
231+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
232+
233+
# Create a regular exception with an invalid traceback
234+
test_exception = ValueError("test error")
235+
test_traceback = mocker.Mock() # Not a TracebackType
236+
237+
# Call commitizen_excepthook with the regular exception and invalid traceback
238+
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)
239+
240+
# Verify original_excepthook was called with None as traceback
241+
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)

0 commit comments

Comments
 (0)