Skip to content

Commit 999f298

Browse files
authored
Merge branch 'v4-9-0-test' into feat/Add-config-option-for-line-length-warning
2 parents c162290 + e70c0a6 commit 999f298

File tree

5 files changed

+102
-28
lines changed

5 files changed

+102
-28
lines changed

commitizen/cli.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -578,20 +578,19 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:
578578
Receives digits and strings and outputs the parsed integer which
579579
represents the exit code found in exceptions.
580580
"""
581-
no_raise_items: list[str] = comma_separated_no_raise.split(",")
582-
no_raise_codes: list[int] = []
583-
for item in no_raise_items:
584-
if item.isdecimal():
585-
no_raise_codes.append(int(item))
586-
continue
581+
582+
def exit_code_from_str_or_skip(s: str) -> ExitCode | None:
587583
try:
588-
exit_code = ExitCode[item.strip()]
589-
except KeyError:
590-
out.warn(f"WARN: no_raise key `{item}` does not exist. Skipping.")
591-
continue
592-
else:
593-
no_raise_codes.append(exit_code.value)
594-
return no_raise_codes
584+
return ExitCode.from_str(s)
585+
except (KeyError, ValueError):
586+
out.warn(f"WARN: no_raise value `{s}` is not a valid exit code. Skipping.")
587+
return None
588+
589+
return [
590+
code.value
591+
for s in comma_separated_no_raise.split(",")
592+
if (code := exit_code_from_str_or_skip(s)) is not None
593+
]
595594

596595

597596
if TYPE_CHECKING:

commitizen/commands/init.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,20 @@ def _ask_tag(self) -> str:
210210
return latest_tag
211211

212212
def _ask_tag_format(self, latest_tag: str) -> str:
213-
is_correct_format = False
214213
if latest_tag.startswith("v"):
215-
tag_format = r"v$version"
216-
is_correct_format = questionary.confirm(
217-
f'Is "{tag_format}" the correct tag format?', style=self.cz.style
218-
).unsafe_ask()
214+
v_tag_format = r"v$version"
215+
if questionary.confirm(
216+
f'Is "{v_tag_format}" the correct tag format?', style=self.cz.style
217+
).unsafe_ask():
218+
return v_tag_format
219219

220220
default_format = DEFAULT_SETTINGS["tag_format"]
221-
if not is_correct_format:
222-
tag_format = questionary.text(
223-
f'Please enter the correct version format: (default: "{default_format}")',
224-
style=self.cz.style,
225-
).unsafe_ask()
221+
tag_format: str = questionary.text(
222+
f'Please enter the correct version format: (default: "{default_format}")',
223+
style=self.cz.style,
224+
).unsafe_ask()
226225

227-
if not tag_format:
228-
tag_format = default_format
229-
return tag_format
226+
return tag_format or default_format
230227

231228
def _ask_version_provider(self) -> str:
232229
"""Ask for setting: version_provider"""

commitizen/exceptions.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import enum
1+
from __future__ import annotations
2+
3+
from enum import IntEnum
24
from typing import Any
35

46
from 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

4351
class CommitizenException(Exception):
4452
def __init__(self, *args: str, **kwargs: Any) -> None:

tests/commands/test_init_command.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,38 @@ def test_pre_commit_exec_failed(
255255
commands.Init(config)()
256256

257257

258+
class TestAskTagFormat:
259+
def test_confirm_v_tag_format(self, mocker: MockFixture, config):
260+
init = commands.Init(config)
261+
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
262+
263+
result = init._ask_tag_format("v1.0.0")
264+
assert result == r"v$version"
265+
266+
def test_reject_v_tag_format(self, mocker: MockFixture, config):
267+
init = commands.Init(config)
268+
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
269+
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))
270+
271+
result = init._ask_tag_format("v1.0.0")
272+
assert result == "custom-$version"
273+
274+
def test_non_v_tag_format(self, mocker: MockFixture, config):
275+
init = commands.Init(config)
276+
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))
277+
278+
result = init._ask_tag_format("1.0.0")
279+
assert result == "custom-$version"
280+
281+
def test_empty_input_returns_default(self, mocker: MockFixture, config):
282+
init = commands.Init(config)
283+
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
284+
mocker.patch("questionary.text", return_value=FakeQuestion(""))
285+
286+
result = init._ask_tag_format("v1.0.0")
287+
assert result == "$version" # This is the default format from DEFAULT_SETTINGS
288+
289+
258290
@skip_below_py_3_10
259291
def test_init_command_shows_description_when_use_help_option(
260292
mocker: MockFixture, capsys, file_regression

tests/test_exceptions.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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("\tNO_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(" ")

0 commit comments

Comments
 (0)