Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 46c5ecc

Browse files
fix(bump): don't fail if an invalid version tag is present (fix #1410) (#1418)
1 parent 2cb2daf commit 46c5ecc

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

‎commitizen/tags.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,20 @@ def _format_regex(self, tag_pattern: str, star: bool = False) -> str:
113113
format_regex = format_regex.replace(pattern, regex)
114114
return format_regex
115115

116+
def _version_tag_error(self, tag: str) -> str:
117+
"""Format the error message for an invalid version tag"""
118+
return f"Invalid version tag: '{tag}' does not match any configured tag format"
119+
116120
def is_version_tag(self, tag: str | GitTag, warn: bool = False) -> bool:
117121
"""
118122
True if a given tag is a legit version tag.
119123
120124
if `warn` is `True`, it will print a warning message if the tag is not a version tag.
121125
"""
122126
tag = tag.name if isinstance(tag, GitTag) else tag
123-
is_legit = any(regex.match(tag) for regex in self.version_regexes)
127+
is_legit = any(regex.fullmatch(tag) for regex in self.version_regexes)
124128
if warn and not is_legit and not self.is_ignored_tag(tag):
125-
out.warn(f"InvalidVersion {tag} doesn't match any configured tag format")
129+
out.warn(self._version_tag_error(tag))
126130
return is_legit
127131

128132
def is_ignored_tag(self, tag: str | GitTag) -> bool:
@@ -146,9 +150,7 @@ def extract_version(self, tag: GitTag) -> Version:
146150
m for regex in self.version_regexes if (m := regex.fullmatch(tag.name))
147151
)
148152
if not (m := next(candidates, None)):
149-
raise InvalidVersion(
150-
f"Invalid version tag: '{tag.name}' does not match any configured tag format"
151-
)
153+
raise InvalidVersion(self._version_tag_error(tag.name))
152154
if "version" in m.groupdict():
153155
return self.scheme(m.group("version"))
154156

‎tests/commands/test_bump_command.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,3 +1656,35 @@ def test_bump_detect_legacy_tags_from_scm(
16561656
cli.main()
16571657

16581658
assert git.tag_exist("v0.4.3")
1659+
1660+
1661+
def test_bump_warn_but_dont_fail_on_invalid_tags(
1662+
tmp_commitizen_project: py.path.local,
1663+
mocker: MockFixture,
1664+
capsys: pytest.CaptureFixture,
1665+
):
1666+
project_root = Path(tmp_commitizen_project)
1667+
tmp_commitizen_cfg_file = project_root / "pyproject.toml"
1668+
tmp_commitizen_cfg_file.write_text(
1669+
"\n".join(
1670+
[
1671+
"[tool.commitizen]",
1672+
'version_provider = "scm"',
1673+
'version_scheme = "pep440"',
1674+
]
1675+
),
1676+
)
1677+
create_file_and_commit("feat: new file")
1678+
create_tag("0.4.2")
1679+
create_file_and_commit("feat: new file")
1680+
create_tag("0.4.3.deadbeaf")
1681+
create_file_and_commit("feat: new file")
1682+
1683+
testargs = ["cz", "bump", "--increment", "patch", "--changelog"]
1684+
mocker.patch.object(sys, "argv", testargs)
1685+
cli.main()
1686+
1687+
_, err = capsys.readouterr()
1688+
1689+
assert err.count("Invalid version tag: '0.4.3.deadbeaf'") == 1
1690+
assert git.tag_exist("0.4.3")

‎tests/commands/test_changelog_command.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,13 +1788,13 @@ def test_changelog_ignored_tags(
17881788
out = open(changelog_path).read()
17891789
_, err = capsys.readouterr()
17901790
assert "## ignore-0.1.0" not in out
1791-
assert "InvalidVersion ignore-0.1.0" not in err
1791+
assert "Invalid version tag: 'ignore-0.1.0'" not in err
17921792
assert "## ignored" not in out
1793-
assert "InvalidVersion ignored" not in err
1793+
assert "Invalid version tag: 'ignored'" not in err
17941794
assert "## not-ignored" not in out
1795-
assert "InvalidVersion not-ignored" in err
1795+
assert "Invalid version tag: 'not-ignored'" in err
17961796
assert "## v0.3.0" in out
1797-
assert "InvalidVersion v0.3.0" not in err
1797+
assert "Invalid version tag: 'v0.3.0'" not in err
17981798

17991799

18001800
def test_changelog_template_extra_quotes(

‎tests/test_changelog.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture):
16351635
}
16361636

16371637
captured = capsys.readouterr()
1638-
assert captured.err.count("InvalidVersion") == 2
1638+
assert captured.err.count("Invalid version tag:") == 2
16391639
assert captured.err.count("not-a-version") == 2
16401640

16411641

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /