File tree Expand file tree Collapse file tree 3 files changed +53
-17
lines changed Expand file tree Collapse file tree 3 files changed +53
-17
lines changed Original file line number Diff line number Diff line change 1212import argcomplete
1313from decli import cli
1414
15- from commitizen import commands , config , out , version_schemes
15+ from commitizen import commands , config , version_schemes
1616from commitizen .exceptions import (
1717 CommitizenException ,
1818 ExitCode ,
@@ -577,26 +577,18 @@ def commitizen_excepthook(
577577sys .excepthook = commitizen_excepthook
578578
579579
580+ # TODO: unused, deprecate this?
580581def parse_no_raise (comma_separated_no_raise : str ) -> list [int ]:
581582 """Convert the given string to exit codes.
582583
583584 Receives digits and strings and outputs the parsed integer which
584585 represents the exit code found in exceptions.
585586 """
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
587+ return [
588+ code .value
589+ for s in comma_separated_no_raise .split ("," )
590+ if (code := ExitCode .from_str (s )) is not None
591+ ]
600592
601593
602594if 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,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
4355class CommitizenException (Exception ):
4456 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