11import inspect
22import os
3+ import re
34import shutil
45from typing import List , Optional
56
67import pytest
78from pytest_mock import MockFixture
89
910from commitizen import cmd , exceptions , git
11+ from commitizen .tags import make_tag_pattern
1012from tests .utils import FakeCommand , create_file_and_commit
1113
1214
@@ -28,7 +30,7 @@ def test_get_tags(mocker: MockFixture):
2830 )
2931 mocker .patch ("commitizen.cmd.run" , return_value = FakeCommand (out = tag_str ))
3032
31- git_tags = git .get_tags ()
33+ git_tags = git .get_tags (pattern = re . compile ( r"v[0-9\.]+" ) )
3234 latest_git_tag = git_tags [0 ]
3335 assert latest_git_tag .rev == "333"
3436 assert latest_git_tag .name == "v1.0.0"
@@ -37,7 +39,60 @@ def test_get_tags(mocker: MockFixture):
3739 mocker .patch (
3840 "commitizen.cmd.run" , return_value = FakeCommand (out = "" , err = "No tag available" )
3941 )
40- assert git .get_tags () == []
42+ assert git .get_tags (pattern = re .compile (r"v[0-9\.]+" )) == []
43+
44+
45+ @pytest .mark .parametrize (
46+ "pattern, expected_tags" ,
47+ [
48+ pytest .param (
49+ make_tag_pattern (tag_format = "$version" ),
50+ [], # No versions with normal 1.2.3 pattern
51+ id = "default-tag-format" ,
52+ ),
53+ pytest .param (
54+ make_tag_pattern (tag_format = "$major-$minor-$patch$prerelease" ),
55+ ["1-0-0" , "1-0-0alpha2" ],
56+ id = "tag-format-with-hyphens" ,
57+ ),
58+ pytest .param (
59+ r"[0-9]+\-[0-9]+\-[0-9]+" ,
60+ ["1-0-0" ],
61+ id = "tag-regex-with-hyphens-that-excludes-alpha" ,
62+ ),
63+ pytest .param (
64+ make_tag_pattern (tag_format = "v$version" ),
65+ ["v0.5.0" , "v0.0.1-pre" ],
66+ id = "tag-format-with-v-prefix" ,
67+ ),
68+ pytest .param (
69+ make_tag_pattern (tag_format = "custom-prefix-$version" ),
70+ ["custom-prefix-0.0.1" ],
71+ id = "tag-format-with-custom-prefix" ,
72+ ),
73+ pytest .param (
74+ ".*" ,
75+ ["1-0-0" , "1-0-0alpha2" , "v0.5.0" , "v0.0.1-pre" , "custom-prefix-0.0.1" ],
76+ id = "custom-tag-regex-to-include-all-tags" ,
77+ ),
78+ ],
79+ )
80+ def test_get_tags_filtering (
81+ mocker : MockFixture , pattern : str , expected_tags : List [str ]
82+ ):
83+ tag_str = (
84+ "1-0-0---inner_delimiter---333---inner_delimiter---2020-01-20---inner_delimiter---\n "
85+ "1-0-0alpha2---inner_delimiter---333---inner_delimiter---2020-01-20---inner_delimiter---\n "
86+ "v0.5.0---inner_delimiter---222---inner_delimiter---2020-01-17---inner_delimiter---\n "
87+ "v0.0.1-pre---inner_delimiter---111---inner_delimiter---2020-01-17---inner_delimiter---\n "
88+ "custom-prefix-0.0.1---inner_delimiter---111---inner_delimiter---2020-01-17---inner_delimiter---\n "
89+ "custom-non-release-tag"
90+ )
91+ mocker .patch ("commitizen.cmd.run" , return_value = FakeCommand (out = tag_str ))
92+
93+ git_tags = git .get_tags (pattern = re .compile (pattern , flags = re .VERBOSE ))
94+ actual_name_list = [t .name for t in git_tags ]
95+ assert actual_name_list == expected_tags
4196
4297
4398def test_get_tag_names (mocker : MockFixture ):
0 commit comments