Skip to content

Commit 7d31f42

Browse files
committed
fix(ExitCode): add from_str in ExitCode and replace parse_no_raise with it
Warn if a given decimal string is not in range
1 parent 6b4f8b0 commit 7d31f42

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

commitizen/cli.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -583,20 +583,19 @@ 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-
no_raise_items: list[str] = comma_separated_no_raise.split(",")
587-
no_raise_codes: list[int] = []
588-
for item in no_raise_items:
589-
if item.isdecimal():
590-
no_raise_codes.append(int(item))
591-
continue
586+
587+
def exit_code_from_str_or_skip(s: str) -> ExitCode | None:
592588
try:
593-
exit_code = ExitCode[item.strip()]
594-
except KeyError:
595-
out.warn(f"WARN: no_raise key `{item}` does not exist. Skipping.")
596-
continue
597-
else:
598-
no_raise_codes.append(exit_code.value)
599-
return no_raise_codes
589+
return ExitCode.from_str(s)
590+
except (KeyError, ValueError):
591+
out.warn(f"WARN: no_raise value `{s}` is not a valid exit code. Skipping.")
592+
return None
593+
594+
return [
595+
code.value
596+
for s in comma_separated_no_raise.split(",")
597+
if (code := exit_code_from_str_or_skip(s)) is not None
598+
]
600599

601600

602601
if TYPE_CHECKING:

commitizen/exceptions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import enum
1+
from __future__ import annotations
2+
3+
from enum import IntEnum
24
from typing import Any
35

46
from commitizen import out
57

68

7-
class ExitCode(enum.IntEnum):
9+
class ExitCode(IntEnum):
810
EXPECTED_EXIT = 0
911
NO_COMMITIZEN_FOUND = 1
1012
NOT_A_GIT_PROJECT = 2
@@ -39,6 +41,12 @@ class ExitCode(enum.IntEnum):
3941
CONFIG_FILE_IS_EMPTY = 31
4042
COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32
4143

44+
@classmethod
45+
def from_str(cls, value: str) -> ExitCode:
46+
if value.isdecimal():
47+
return cls(int(value))
48+
return cls[value.strip()]
49+
4250

4351
class CommitizenException(Exception):
4452
def __init__(self, *args: str, **kwargs: Any) -> None:

tests/test_exceptions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from commitizen.exceptions import ExitCode
4+
5+
6+
def test_from_str_with_decimal():
7+
"""Test from_str with decimal values."""
8+
assert ExitCode.from_str("0") == ExitCode.EXPECTED_EXIT
9+
assert ExitCode.from_str("1") == ExitCode.NO_COMMITIZEN_FOUND
10+
assert ExitCode.from_str("32") == ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED
11+
12+
13+
def test_from_str_with_enum_name():
14+
"""Test from_str with enum names."""
15+
assert ExitCode.from_str("EXPECTED_EXIT") == ExitCode.EXPECTED_EXIT
16+
assert ExitCode.from_str("NO_COMMITIZEN_FOUND") == ExitCode.NO_COMMITIZEN_FOUND
17+
assert (
18+
ExitCode.from_str("COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED")
19+
== ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED
20+
)
21+
22+
23+
def test_from_str_with_whitespace():
24+
"""Test from_str with whitespace in enum names."""
25+
assert ExitCode.from_str(" EXPECTED_EXIT ") == ExitCode.EXPECTED_EXIT
26+
assert ExitCode.from_str("\tNO_COMMITIZEN_FOUND\t") == ExitCode.NO_COMMITIZEN_FOUND
27+
28+
29+
def test_from_str_with_invalid_values():
30+
"""Test from_str with invalid values."""
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)