Skip to content

fix(ExitCode): add from_str in ExitCode and replace parse_no_raise with it, warn if the error code is not in range #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v4-9-0-test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import argcomplete
from decli import cli

from commitizen import commands, config, out, version_schemes
from commitizen import commands, config, version_schemes
from commitizen.exceptions import (
CommitizenException,
ExitCode,
Expand Down Expand Up @@ -583,20 +583,11 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
Receives digits and strings and outputs the parsed integer which
represents the exit code found in exceptions.
"""
no_raise_items: list[str] = comma_separated_no_raise.split(",")
no_raise_codes: list[int] = []
for item in no_raise_items:
if item.isdecimal():
no_raise_codes.append(int(item))
continue
try:
exit_code = ExitCode[item.strip()]
except KeyError:
out.warn(f"WARN: no_raise key `{item}` does not exist. Skipping.")
continue
else:
no_raise_codes.append(exit_code.value)
return no_raise_codes
return [
code.value
for s in comma_separated_no_raise.split(",")
if (code := ExitCode.from_str(s)) is not None
]


if TYPE_CHECKING:
Expand Down
23 changes: 21 additions & 2 deletions commitizen/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import enum
from __future__ import annotations

from enum import IntEnum
from typing import Any

from commitizen import out


class ExitCode(enum.IntEnum):
class ExitCode(IntEnum):
EXPECTED_EXIT = 0
NO_COMMITIZEN_FOUND = 1
NOT_A_GIT_PROJECT = 2
Expand Down Expand Up @@ -39,6 +41,23 @@ class ExitCode(enum.IntEnum):
CONFIG_FILE_IS_EMPTY = 31
COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32

@classmethod
def from_str(cls, value: str) -> ExitCode | None:
if value.isdecimal():
try:
return cls(int(value))
except ValueError:
out.warn(
f"WARN: no_raise value `{value}` is not a valid exit code. Skipping."
)
return None

try:
return cls[value.strip()]
except KeyError:
out.warn(f"WARN: no_raise key `{value}` does not exist. Skipping.")
return None


class CommitizenException(Exception):
def __init__(self, *args: str, **kwargs: Any) -> None:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from commitizen.exceptions import ExitCode


def test_from_str_with_decimal():
"""Test from_str with decimal values."""
assert ExitCode.from_str("0") == ExitCode.EXPECTED_EXIT
assert ExitCode.from_str("1") == ExitCode.NO_COMMITIZEN_FOUND
assert ExitCode.from_str("32") == ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED


def test_from_str_with_enum_name():
"""Test from_str with enum names."""
assert ExitCode.from_str("EXPECTED_EXIT") == ExitCode.EXPECTED_EXIT
assert ExitCode.from_str("NO_COMMITIZEN_FOUND") == ExitCode.NO_COMMITIZEN_FOUND
assert (
ExitCode.from_str("COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED")
== ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED
)


def test_from_str_with_whitespace():
"""Test from_str with whitespace in enum names."""
assert ExitCode.from_str(" EXPECTED_EXIT ") == ExitCode.EXPECTED_EXIT
assert ExitCode.from_str("\tNO_COMMITIZEN_FOUND\t") == ExitCode.NO_COMMITIZEN_FOUND


def test_from_str_with_invalid_values():
"""Test from_str with invalid values."""
assert ExitCode.from_str("invalid_name") is None
assert ExitCode.from_str("999") is None # Out of range decimal
assert ExitCode.from_str("") is None
assert ExitCode.from_str(" ") is None