File tree Expand file tree Collapse file tree 3 files changed +60
-15
lines changed Expand file tree Collapse file tree 3 files changed +60
-15
lines changed Original file line number Diff line number Diff line change @@ -583,20 +583,19 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
583
583
Receives digits and strings and outputs the parsed integer which
584
584
represents the exit code found in exceptions.
585
585
"""
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
586
+
587
+ def exit_code_from_str_or_skip (s : str ) -> ExitCode | None :
592
588
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
589
+ return ExitCode .from_str (s )
590
+ except (KeyError , ValueError ):
591
+ out .warn (f"WARN: no_raise value `{ s } ` is not a valid exit code. Skipping." )
592
+ return None
593
+
594
+ return [
595
+ code .value
596
+ for s in comma_separated_no_raise .split ("," )
597
+ if (code := exit_code_from_str_or_skip (s )) is not None
598
+ ]
600
599
601
600
602
601
if TYPE_CHECKING :
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,12 @@ 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 :
46
+ if value .isdecimal ():
47
+ return cls (int (value ))
48
+ return cls [value .strip ()]
49
+
42
50
43
51
class CommitizenException (Exception ):
44
52
def __init__ (self , * args : str , ** kwargs : Any ) -> None :
Original file line number Diff line number Diff line change
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 ("\t NO_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 (" " )
You can’t perform that action at this time.
0 commit comments