8000 fix(cli): raise exception instead of skipping an invalid error code · commitizen-tools/commitizen@213fc52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 213fc52

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

File tree

3 files changed

+19
-23
lines changed
  • tests
  • 3 files changed

    +19
    -23
    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_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)
    0