Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e85cae

Browse filesBrowse files
committed
refactor(cli): early return and improve test coverage
1 parent a8094ae commit 5e85cae
Copy full SHA for 5e85cae

File tree

2 files changed

+100
-10
lines changed
Filter options

2 files changed

+100
-10
lines changed

‎commitizen/cli.py

Copy file name to clipboardExpand all lines: commitizen/cli.py
+11-10Lines 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

Copy file name to clipboardExpand all lines: tests/test_cli.py
+89Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import argparse
12
import os
23
import subprocess
34
import sys
5+
import types
46
from functools import partial
57

68
import pytest
@@ -182,3 +184,90 @@ def test_unknown_args_before_double_dash_raises(mocker: MockFixture):
182184
assert "Invalid commitizen arguments were found before -- separator" in str(
183185
excinfo.value
184186
)
187+
188+
189+
def test_parse_kwargs_non_string_input():
190+
"""Test that ParseKwargs returns early when given non-string input."""
191+
parser = argparse.ArgumentParser()
192+
namespace = argparse.Namespace()
193+
action = cli.ParseKwargs(option_strings=["-k"], dest="kwargs")
194+
195+
# Test with None
196+
action(parser, namespace, None)
197+
assert not hasattr(namespace, "kwargs")
198+
199+
# Test with list
200+
action(parser, namespace, ["key=value"])
201+
assert not hasattr(namespace, "kwargs")
202+
203+
# Test with dict
204+
action(parser, namespace, {"key": "value"})
205+
assert not hasattr(namespace, "kwargs")
206+
207+
208+
def test_parse_kwargs_valid_string_input():
209+
"""Test that ParseKwargs correctly processes valid string input."""
210+
parser = argparse.ArgumentParser()
211+
namespace = argparse.Namespace()
212+
action = cli.ParseKwargs(option_strings=["-k"], dest="kwargs")
213+
214+
# Test with valid key=value string
215+
action(parser, namespace, "key=value")
216+
assert hasattr(namespace, "kwargs")
217+
assert namespace.kwargs == {"key": "value"}
218+
219+
220+
def test_commitizen_excepthook_non_commitizen_exception(mocker: MockFixture):
221+
"""Test that commitizen_excepthook delegates to original_excepthook for non-CommitizenException."""
222+
# Mock the original excepthook
223+
mock_original_excepthook = mocker.Mock()
224+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
225+
226+
# Create a regular exception
227+
test_exception = ValueError("test error")
228+
229+
# Call commitizen_excepthook with the regular exception
230+
cli.commitizen_excepthook(ValueError, test_exception, None)
231+
232+
# Verify original_excepthook was called with correct arguments
233+
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)
234+
235+
236+
def test_commitizen_excepthook_non_commitizen_exception_with_traceback(
237+
mocker: MockFixture,
238+
):
239+
"""Test that commitizen_excepthook handles traceback correctly for non-CommitizenException."""
240+
# Mock the original excepthook
241+
mock_original_excepthook = mocker.Mock()
242+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
243+
244+
# Create a regular exception with a traceback
245+
test_exception = ValueError("test error")
246+
test_traceback = mocker.Mock(spec=types.TracebackType)
247+
248+
# Call commitizen_excepthook with the regular exception and traceback
249+
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)
250+
251+
# Verify original_excepthook was called with correct arguments including traceback
252+
mock_original_excepthook.assert_called_once_with(
253+
ValueError, test_exception, test_traceback
254+
)
255+
256+
257+
def test_commitizen_excepthook_non_commitizen_exception_with_invalid_traceback(
258+
mocker: MockFixture,
259+
):
260+
"""Test that commitizen_excepthook handles invalid traceback correctly for non-CommitizenException."""
261+
# Mock the original excepthook
262+
mock_original_excepthook = mocker.Mock()
263+
mocker.patch("commitizen.cli.original_excepthook", mock_original_excepthook)
264+
265+
# Create a regular exception with an invalid traceback
266+
test_exception = ValueError("test error")
267+
test_traceback = mocker.Mock() # Not a TracebackType
268+
269+
# Call commitizen_excepthook with the regular exception and invalid traceback
270+
cli.commitizen_excepthook(ValueError, test_exception, test_traceback)
271+
272+
# Verify original_excepthook was called with None as traceback
273+
mock_original_excepthook.assert_called_once_with(ValueError, test_exception, None)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.