Skip to content

Commit 37b0e81

Browse files
committed
refactor(ExitCode): add from_str in ExitCode and replace parse_no_raise with it
1 parent 6b4f8b0 commit 37b0e81

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

commitizen/cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ def commitizen_excepthook(
577577
sys.excepthook = commitizen_excepthook
578578

579579

580+
# TODO: unused, deprecate this?
580581
def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
581582
"""Convert the given string to exit codes.
582583
@@ -673,7 +674,11 @@ def main() -> None:
673674
logging.getLogger("commitizen").setLevel(logging.DEBUG)
674675
sys.excepthook = commitizen_debug_excepthook
675676
elif args.no_raise:
676-
no_raise_exit_codes = parse_no_raise(args.no_raise)
677+
no_raise_exit_codes = [
678+
code.value
679+
for s in args.no_raise.split(",")
680+
if (code := ExitCode.from_str(s)) is not None
681+
]
677682
no_raise_debug_excepthook = partial(
678683
commitizen_excepthook, no_raise=no_raise_exit_codes
679684
)

commitizen/exceptions.py

Lines changed: 14 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,16 @@ 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 | None:
46+
try:
47+
if value.isdecimal():
48+
return cls(int(value))
49+
return cls[value.strip()]
50+
except (KeyError, ValueError):
51+
out.warn(f"WARN: no_raise key `{value}` does not exist. Skipping.")
52+
return None
53+
4254

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

tests/test_exceptions.py

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

0 commit comments

Comments
 (0)