File tree Expand file tree Collapse file tree 3 files changed +52
-3
lines changed Expand file tree Collapse file tree 3 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -577,6 +577,7 @@ def commitizen_excepthook(
577
577
sys .excepthook = commitizen_excepthook
578
578
579
579
580
+ # TODO: unused, deprecate this?
580
581
def parse_no_raise (comma_separated_no_raise : str ) -> list [int ]:
581
582
"""Convert the given string to exit codes.
582
583
@@ -673,7 +674,11 @@ def main() -> None:
673
674
logging .getLogger ("commitizen" ).setLevel (logging .DEBUG )
674
675
sys .excepthook = commitizen_debug_excepthook
675
676
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
+ ]
677
682
no_raise_debug_excepthook = partial (
678
683
commitizen_excepthook , no_raise = no_raise_exit_codes
679
684
)
Original file line number Diff line number Diff line change 1
- import enum
1
+ from __future__ import annotations
2
+
3
+ from enum import IntEnum
2
4
from typing import Any
3
5
4
6
from commitizen import out
5
7
6
8
7
- class ExitCode (enum . IntEnum ):
9
+ class ExitCode (IntEnum ):
8
10
EXPECTED_EXIT = 0
9
11
NO_COMMITIZEN_FOUND = 1
10
12
NOT_A_GIT_PROJECT = 2
@@ -39,6 +41,16 @@ class ExitCode(enum.IntEnum):
39
41
CONFIG_FILE_IS_EMPTY = 31
40
42
COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32
41
43
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
+
42
54
43
55
class CommitizenException (Exception ):
44
56
def __init__ (self , * args : str , ** kwargs : Any ) -> None :
Original file line number Diff line number Diff line change
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 ("\t NO_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
You can’t perform that action at this time.
0 commit comments