Skip to content

Commit 85d5313

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 85d5313

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

commitizen/cli.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import argcomplete
1313
from decli import cli
1414

15-
from commitizen import commands, config, out, version_schemes
15+
from commitizen import commands, config, version_schemes
1616
from commitizen.exceptions import (
1717
CommitizenException,
1818
ExitCode,
@@ -583,20 +583,11 @@ 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
592-
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
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+
]
600591

601592

602593
if TYPE_CHECKING:

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy