diff --git a/commitizen/tags.py b/commitizen/tags.py index 5724bb257..2b9a4b091 100644 --- a/commitizen/tags.py +++ b/commitizen/tags.py @@ -113,6 +113,10 @@ def _format_regex(self, tag_pattern: str, star: bool = False) -> str: format_regex = format_regex.replace(pattern, regex) return format_regex + def _version_tag_error(self, tag: str) -> str: + """Format the error message for an invalid version tag""" + return f"Invalid version tag: '{tag}' does not match any configured tag format" + def is_version_tag(self, tag: str | GitTag, warn: bool = False) -> bool: """ True if a given tag is a legit version tag. @@ -120,9 +124,9 @@ def is_version_tag(self, tag: str | GitTag, warn: bool = False) -> bool: if `warn` is `True`, it will print a warning message if the tag is not a version tag. """ tag = tag.name if isinstance(tag, GitTag) else tag - is_legit = any(regex.match(tag) for regex in self.version_regexes) + is_legit = any(regex.fullmatch(tag) for regex in self.version_regexes) if warn and not is_legit and not self.is_ignored_tag(tag): - out.warn(f"InvalidVersion {tag} doesn't match any configured tag format") + out.warn(self._version_tag_error(tag)) return is_legit def is_ignored_tag(self, tag: str | GitTag) -> bool: @@ -146,9 +150,7 @@ def extract_version(self, tag: GitTag) -> Version: m for regex in self.version_regexes if (m := regex.fullmatch(tag.name)) ) if not (m := next(candidates, None)): - raise InvalidVersion( - f"Invalid version tag: '{tag.name}' does not match any configured tag format" - ) + raise InvalidVersion(self._version_tag_error(tag.name)) if "version" in m.groupdict(): return self.scheme(m.group("version")) diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index f3c1ba923..e15539d8a 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1656,3 +1656,35 @@ def test_bump_detect_legacy_tags_from_scm( cli.main() assert git.tag_exist("v0.4.3") + + +def test_bump_warn_but_dont_fail_on_invalid_tags( + tmp_commitizen_project: py.path.local, + mocker: MockFixture, + capsys: pytest.CaptureFixture, +): + project_root = Path(tmp_commitizen_project) + tmp_commitizen_cfg_file = project_root / "pyproject.toml" + tmp_commitizen_cfg_file.write_text( + "\n".join( + [ + "[tool.commitizen]", + 'version_provider = "scm"', + 'version_scheme = "pep440"', + ] + ), + ) + create_file_and_commit("feat: new file") + create_tag("0.4.2") + create_file_and_commit("feat: new file") + create_tag("0.4.3.deadbeaf") + create_file_and_commit("feat: new file") + + testargs = ["cz", "bump", "--increment", "patch", "--changelog"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + _, err = capsys.readouterr() + + assert err.count("Invalid version tag: '0.4.3.deadbeaf'") == 1 + assert git.tag_exist("0.4.3") diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 12a2e04b4..0eb29cdb0 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1788,13 +1788,13 @@ def test_changelog_ignored_tags( out = open(changelog_path).read() _, err = capsys.readouterr() assert "## ignore-0.1.0" not in out - assert "InvalidVersion ignore-0.1.0" not in err + assert "Invalid version tag: 'ignore-0.1.0'" not in err assert "## ignored" not in out - assert "InvalidVersion ignored" not in err + assert "Invalid version tag: 'ignored'" not in err assert "## not-ignored" not in out - assert "InvalidVersion not-ignored" in err + assert "Invalid version tag: 'not-ignored'" in err assert "## v0.3.0" in out - assert "InvalidVersion v0.3.0" not in err + assert "Invalid version tag: 'v0.3.0'" not in err def test_changelog_template_extra_quotes( diff --git a/tests/test_changelog.py b/tests/test_changelog.py index 67ba273b5..067adb4a9 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -1635,7 +1635,7 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture): } captured = capsys.readouterr() - assert captured.err.count("InvalidVersion") == 2 + assert captured.err.count("Invalid version tag:") == 2 assert captured.err.count("not-a-version") == 2