|
| 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" |
0 commit comments