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 @@ -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
599598if 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
24from typing import Any
35
46from 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
4351class CommitizenException (Exception ):
4452 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