Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2631253

Browse files
bearomorphismLee-W
authored andcommitted
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 07e78bc commit 2631253

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
@@ -580,20 +580,19 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
580580
Receives digits and strings and outputs the parsed integer which
581581
represents the exit code found in exceptions.
582582
"""
583-
no_raise_items: list[str] = comma_separated_no_raise.split(",")
584-
no_raise_codes: list[int] = []
585-
for item in no_raise_items:
586-
if item.isdecimal():
587-
no_raise_codes.append(int(item))
588-
continue
583+
584+
def exit_code_from_str_or_skip(s: str) -> ExitCode | None:
589585
try:
590-
exit_code = ExitCode[item.strip()]
591-
except KeyError:
592-
out.warn(f"WARN: no_raise key `{item}` does not exist. Skipping.")
593-
continue
594-
else:
595-
no_raise_codes.append(exit_code.value)
596-
return no_raise_codes
586+
return ExitCode.from_str(s)
587+
except (KeyError, ValueError):
588+
out.warn(f"WARN: no_raise value `{s}` is not a valid exit code. Skipping.")
589+
return None
590+
591+
return [
592+
code.value
593+
for s in comma_separated_no_raise.split(",")
594+
if (code := exit_code_from_str_or_skip(s)) is not None
595+
]
597596

598597

599598
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)

AltStyle によって変換されたページ (->オリジナル) /