| 1 |
from commitizen.git import GitTag |
| 2 |
from commitizen.tags import TagRules |
| 3 |
|
| 4 |
|
| 5 |
def _git_tag(name: str) -> GitTag: |
| 6 |
return GitTag(name, "rev", "2024-01-01") |
| 7 |
|
| 8 |
|
| 9 |
def test_find_tag_for_partial_version_returns_latest_match(): |
| 10 |
tags = [ |
| 11 |
_git_tag("1.2.0"), |
| 12 |
_git_tag("1.2.2"), |
| 13 |
_git_tag("1.2.1"), |
| 14 |
_git_tag("1.3.0"), |
| 15 |
] |
| 16 |
|
| 17 |
rules = TagRules() |
| 18 |
|
| 19 |
found = rules.find_tag_for(tags, "1.2") |
| 20 |
|
| 21 |
assert found is not None |
| 22 |
assert found.name == "1.2.2" |
| 23 |
|
| 24 |
|
| 25 |
def test_find_tag_for_full_version_remains_exact(): |
| 26 |
tags = [ |
| 27 |
_git_tag("1.2.0"), |
| 28 |
_git_tag("1.2.2"), |
| 29 |
_git_tag("1.2.1"), |
| 30 |
] |
| 31 |
|
| 32 |
rules = TagRules() |
| 33 |
|
| 34 |
found = rules.find_tag_for(tags, "1.2.1") |
| 35 |
|
| 36 |
assert found is not None |
| 37 |
assert found.name == "1.2.1" |
| 38 |
|
| 39 |
|
| 40 |
def test_find_tag_for_partial_version_with_prereleases_prefers_latest_version(): |
| 41 |
tags = [ |
| 42 |
_git_tag("1.2.0b1"), |
| 43 |
_git_tag("1.2.0"), |
| 44 |
_git_tag("1.2.1b1"), |
| 45 |
] |
| 46 |
|
| 47 |
rules = TagRules() |
| 48 |
|
| 49 |
found = rules.find_tag_for(tags, "1.2") |
| 50 |
|
| 51 |
assert found is not None |
| 52 |
# 1.2.1b1 > 1.2.0 so it should be selected |
| 53 |
assert found.name == "1.2.1b1" |
| 54 |
|
| 55 |
|
| 56 |
def test_find_tag_for_partial_version_respects_tag_format(): |
| 57 |
tags = [ |
| 58 |
_git_tag("v1.2.0"), |
| 59 |
_git_tag("v1.2.1"), |
| 60 |
_git_tag("v1.3.0"), |
| 61 |
] |
| 62 |
|
| 63 |
rules = TagRules(tag_format="v$version") |
| 64 |
|
| 65 |
found = rules.find_tag_for(tags, "1.2") |
| 66 |
|
| 67 |
assert found is not None |
| 68 |
assert found.name == "v1.2.1" |
| 69 |
|
| 70 |
found = rules.find_tag_for(tags, "1") |
| 71 |
|
| 72 |
assert found is not None |
| 73 |
assert found.name == "v1.3.0" |
| 74 |
|
| 75 |
|
| 76 |
def test_find_tag_for_partial_version_returns_none_when_no_match(): |
| 77 |
tags = [ |
| 78 |
_git_tag("2.0.0"), |
| 79 |
_git_tag("2.1.0"), |
| 80 |
] |
| 81 |
|
| 82 |
rules = TagRules() |
| 83 |
|
| 84 |
found = rules.find_tag_for(tags, "1.2") |
| 85 |
|
| 86 |
assert found is None |
| 87 |
|
| 88 |
|
| 89 |
def test_find_tag_for_partial_version_ignores_invalid_tags(): |
| 90 |
tags = [ |
| 91 |
_git_tag("not-a-version"), |
| 92 |
_git_tag("1.2.0"), |
| 93 |
_git_tag("1.2.1"), |
| 94 |
] |
| 95 |
|
| 96 |
rules = TagRules() |
| 97 |
|
| 98 |
found = rules.find_tag_for(tags, "1.2") |
| 99 |
|
| 100 |
assert found is not None |
| 101 |
assert found.name == "1.2.1" |
| 102 |
|
| 103 |
|
| 104 |
def test_is_version_tag_accepts_semver2_prerelease_in_custom_tag_format(): |
| 105 |
"""Regression test for #1614: a SemVer2-style prerelease segment such as |
| 106 |
``rc.0`` (with a literal dot) must be recognised when it appears at the |
| 107 |
position of ``${prerelease}`` in a custom ``tag_format``. Before the |
| 108 |
prerelease regex was widened from ``\\w+\\d+`` to ``\\w+(?:\\.\\w+)*``, |
| 109 |
the tag commitizen itself just created emitted "Invalid version tag" |
| 110 |
warnings on the next changelog/bump. |
| 111 |
""" |
| 112 |
from commitizen.version_schemes import get_version_scheme |
| 113 |
|
| 114 |
scheme = get_version_scheme({"version_scheme": "semver2"}) |
| 115 |
rules = TagRules( |
| 116 |
scheme=scheme, |
| 117 |
tag_format="${major}.${minor}-${patch}${prerelease}", |
| 118 |
) |
| 119 |
|
| 120 |
assert rules.is_version_tag("0.0-2rc.0") is True |
| 121 |
# Plain releases (no prerelease) are still accepted. |
| 122 |
assert rules.is_version_tag("0.0-2") is True |
| 123 |
# Multi-segment SemVer2 prereleases too. |
| 124 |
assert rules.is_version_tag("0.0-2alpha.beta.1") is True |
| 125 |
|
| 126 |
# And ``extract_version`` round-trips the prerelease portion. |
| 127 |
extracted = rules.extract_version(_git_tag("0.0-2rc.0")) |
| 128 |
assert str(extracted) == "0.0.2-rc.0" |
| 129 |
|
| 130 |
|
| 131 |
def test_is_version_tag_accepts_dotless_devrelease_in_custom_tag_format(): |
| 132 |
"""Regression test for #1614: ``${devrelease}`` accepts both ``dev1`` |
| 133 |
and ``.dev1`` suffixes when a custom ``tag_format`` splits release and dev |
| 134 |
portions explicitly. |
| 135 |
""" |
| 136 |
rules = TagRules(tag_format="version-${major}.${minor}.${patch}${devrelease}") |
| 137 |
|
| 138 |
assert rules.is_version_tag("version-1.2.3.dev1") is True |
| 139 |
assert rules.is_version_tag("version-1.2.3dev1") is True |
| 140 |
|
| 141 |
extracted = rules.extract_version(_git_tag("version-1.2.3dev1")) |
| 142 |
assert str(extracted) == "1.2.3.dev1" |