Skip to content

Commit 6dae1e3

Browse files
committed
fix(cli): raise exception instead of skipping an invalid error code
1 parent b0ffb9a commit 6dae1e3

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

commitizen/cli.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,12 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
583583
Receives digits and strings and outputs the parsed integer which
584584
represents the exit code found in exceptions.
585585
"""
586-
return [
587-
code.value
588-
for s in comma_separated_no_raise.split(",")
589-
if (code := ExitCode.from_str(s)) is not None
590-
]
586+
try:
587+
return [ExitCode.from_str(s).value for s in comma_separated_no_raise.split(",")]
588+
except (KeyError, ValueError) as e:
589+
raise InvalidCommandArgumentError(
590+
f"Invalid no_raise value `{comma_separated_no_raise}`. "
591+
) from e
591592

592593

593594
if TYPE_CHECKING:

commitizen/exceptions.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,10 @@ class ExitCode(IntEnum):
4242
COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32
4343

4444
@classmethod
45-
def from_str(cls, value: str) -> ExitCode | None:
45+
def from_str(cls, value: str) -> ExitCode:
4646
if value.isdecimal():
47-
try:
48-
return cls(int(value))
49-
except ValueError:
50-
out.warn(
51-
f"WARN: no_raise value `{value}` is not a valid exit code. Skipping."
52-
)
53-
return None
54-
55-
try:
56-
return cls[value.strip()]
57-
except KeyError:
58-
out.warn(f"WARN: no_raise key `{value}` does not exist. Skipping.")
59-
return None
47+
return cls(int(value))
48+
return cls[value.strip()]
6049

6150

6251
class CommitizenException(Exception):

tests/test_cli.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,13 @@ def test_parse_no_raise_mix_integer_error_code():
161161
assert result == [1, 2, 3, 4]
162162

163163

164-
def test_parse_no_raise_mix_invalid_arg_is_skipped():
164+
def test_parse_no_raise_mix_invalid_arg_with_error():
165165
input_str = "NO_COMMITIZEN_FOUND,2,nothing,4"
166-
result = cli.parse_no_raise(input_str)
167-
assert result == [1, 2, 4]
166+
with pytest.raises(InvalidCommandArgumentError) as excinfo:
167+
cli.parse_no_raise(input_str)
168+
assert "Invalid no_raise value `NO_COMMITIZEN_FOUND,2,nothing,4`" in str(
169+
excinfo.value
170+
)
168171

169172

170173
def test_unknown_args_raises(mocker: MockFixture):

tests/test_exceptions.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from commitizen.exceptions import ExitCode
24

35

@@ -26,7 +28,11 @@ def test_from_str_with_whitespace():
2628

2729
def test_from_str_with_invalid_values():
2830
"""Test from_str with invalid values."""
29-
assert ExitCode.from_str("invalid_name") is None
30-
assert ExitCode.from_str("999") is None # Out of range decimal
31-
assert ExitCode.from_str("") is None
32-
assert ExitCode.from_str(" ") is None
31+
with pytest.raises(KeyError):
32+
ExitCode.from_str("invalid_name")
33+
with pytest.raises(ValueError):
34+
ExitCode.from_str("999") # Out of range decimal
35+
with pytest.raises(KeyError):
36+
ExitCode.from_str("")
37+
with pytest.raises(KeyError):
38+
ExitCode.from_str(" ")

0 commit comments

Comments
 (0)