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 12
12
import argcomplete
13
13
from decli import cli
14
14
15
- from commitizen import commands , config , out , version_schemes
15
+ from commitizen import commands , config , version_schemes
16
16
from commitizen .exceptions import (
17
17
CommitizenException ,
18
18
ExitCode ,
@@ -577,26 +577,18 @@ 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
583
584
Receives digits and strings and outputs the parsed integer which
584
585
represents the exit code found in exceptions.
585
586
"""
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
+ ]
600
592
601
593
602
594
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,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