Skip to content

Commit fa667dc

Browse files
committed
no-mistakes(review): Reject string NONE in bump maps
1 parent 07386e7 commit fa667dc

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

‎commitizen/version_increment.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ def _from_bump_map_value(cls, value: object) -> VersionIncrement:
5858
"""
5959
if value is None:
6060
return cls.NONE
61-
if not isinstance(value, str):
62-
raise ValueError(f"Invalid bump increment: {value!r}")
63-
try:
64-
return cls[value]
65-
except KeyError as exc:
66-
raise ValueError(f"Invalid bump increment: {value!r}") from exc
61+
if value == "PATCH":
62+
return cls.PATCH
63+
if value == "MINOR":
64+
return cls.MINOR
65+
if value == "MAJOR":
66+
return cls.MAJOR
67+
raise ValueError(f"Invalid bump increment: {value!r}")
6768

6869
@classmethod
6970
def from_match(

‎tests/commands/test_bump_command.py‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,11 +1346,11 @@ def test_bump_allow_no_commit_with_invalid_custom_bump_map_raises(
13461346
version = "0.1.0"
13471347
13481348
[tool.commitizen.customize]
1349-
bump_pattern = "^(new|fix|docs)"
1350-
bump_map = { new = "MINORR", fix = "PATCH", docs = "NONE" }
1351-
change_type_map = { new = "Features", fix = "Bugs", docs = "Docs" }
1352-
changelog_pattern = "^(new|fix|docs)"
1353-
commit_parser = '^(?P<change_type>new|fix|docs):\\s(?P<message>.+)$'
1349+
bump_pattern = "^(new|fix)"
1350+
bump_map = { new = "MINORR", fix = "PATCH" }
1351+
change_type_map = { new = "Features", fix = "Bugs" }
1352+
changelog_pattern = "^(new|fix)"
1353+
commit_parser = '^(?P<change_type>new|fix):\\s(?P<message>.+)$'
13541354
schema_pattern = ".*"
13551355
questions = []
13561356
"""
@@ -1369,6 +1369,45 @@ def test_bump_allow_no_commit_with_invalid_custom_bump_map_raises(
13691369
assert git.tag_exist("0.1.2") is False
13701370

13711371

1372+
1373+
def test_bump_allow_no_commit_with_string_none_bump_map_raises(
1374+
tmp_commitizen_project: Path,
1375+
monkeypatch: pytest.MonkeyPatch,
1376+
util: UtilFixture,
1377+
) -> None:
1378+
"""A matched bump-map string NONE must fail instead of falling back to PATCH."""
1379+
monkeypatch.chdir(tmp_commitizen_project)
1380+
(tmp_commitizen_project / "pyproject.toml").write_text(
1381+
dedent(
1382+
"""
1383+
[tool.commitizen]
1384+
name = "cz_customize"
1385+
version = "0.1.0"
1386+
1387+
[tool.commitizen.customize]
1388+
bump_pattern = "^(docs|fix)"
1389+
bump_map = { docs = "NONE", fix = "PATCH" }
1390+
change_type_map = { docs = "Docs", fix = "Bugs" }
1391+
changelog_pattern = "^(docs|fix)"
1392+
commit_parser = '^(?P<change_type>docs|fix):\\s(?P<message>.+)$'
1393+
schema_pattern = ".*"
1394+
questions = []
1395+
"""
1396+
).strip()
1397+
+ "\n",
1398+
encoding="utf-8",
1399+
)
1400+
1401+
util.create_file_and_commit("fix: seed release")
1402+
util.run_cli("bump", "--yes")
1403+
util.create_file_and_commit("docs: update guide")
1404+
1405+
with pytest.raises(ValueError, match="NONE"):
1406+
util.run_cli("bump", "--yes", "--allow-no-commit")
1407+
1408+
assert git.tag_exist("0.1.2") is False
1409+
1410+
13721411
def test_bump_allow_no_commit_with_increment(
13731412
tmp_commitizen_project, monkeypatch, util: UtilFixture, capsys
13741413
):

‎tests/test_version_increment.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ def test_get_highest_by_messages_supports_custom_none_mapping() -> None:
222222

223223

224224

225+
def test_get_highest_by_messages_rejects_string_none_mapping() -> None:
226+
"""The string NONE remains invalid at the bump-map computation boundary."""
227+
with pytest.raises(ValueError, match="NONE"):
228+
VersionIncrement.get_highest_by_messages(
229+
["docs: update guide"],
230+
r"^(docs)",
231+
{"docs": "NONE"},
232+
)
233+
234+
235+
225236
def test_from_message_stops_after_major_match() -> None:
226237
"""A major match on one line shields later invalid lines in that message."""
227238
assert (

0 commit comments

Comments
 (0)