Skip to content

Commit fcfd4dc

Browse files
committed
WIP investigating tag formatting issues
1 parent c89218b commit fcfd4dc

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

commitizen/commands/changelog.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pathlib import Path
77
from typing import Callable
88

9-
from commitizen import changelog, defaults, factory, git, out
9+
from commitizen import bump, changelog, defaults, factory, git, out
1010

1111
from commitizen.config import BaseConfig
1212
from commitizen.cz.base import MessageBuilderHook, ChangelogReleaseHook
@@ -177,8 +177,13 @@ def __call__(self):
177177
if self.incremental:
178178
changelog_meta = self.changelog_format.get_metadata(self.file_name)
179179
if changelog_meta.latest_version:
180+
latest_tag_version: str = bump.normalize_tag(
181+
changelog_meta.latest_version,
182+
tag_format=self.tag_format,
183+
scheme=self.scheme,
184+
)
180185
start_rev = self._find_incremental_rev(
181-
strip_local_version(changelog_meta.latest_version), tags
186+
strip_local_version(latest_tag_version), tags
182187
)
183188
if self.rev_range:
184189
start_rev, end_rev = changelog.get_oldest_and_newest_rev(

tests/commands/test_changelog_command.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1540,9 +1540,47 @@ def test_changelog_only_tag_matching_tag_format_included(
15401540
mocker.patch.object(sys, "argv", testargs)
15411541
cli.main()
15421542
wait_for_tag()
1543+
create_file_and_commit("feat: some awesome new feature")
1544+
# git.tag("0.2.1")
1545+
testargs = ["cz", "bump", "--changelog", "--yes"]
1546+
mocker.patch.object(sys, "argv", testargs)
1547+
cli.main()
1548+
wait_for_tag()
1549+
with open(changelog_path) as f:
1550+
out = f.read()
1551+
assert out.startswith("## 0.3.0custom (2021-06-11)")
1552+
assert "## 0.2.0custom (2021-06-11)" in out
1553+
assert "## v0.2.0 (2021-06-11)" not in out
1554+
assert "## 0.2.0 (2021-06-11)" not in out
1555+
1556+
1557+
@pytest.mark.usefixtures("tmp_commitizen_project")
1558+
@pytest.mark.freeze_time("2021-06-11")
1559+
def test_changelog_only_tag_matching_tag_format_included_prefix(
1560+
mocker: MockFixture,
1561+
changelog_path: Path,
1562+
config_path: Path,
1563+
):
1564+
with open(config_path, "a", encoding="utf-8") as f:
1565+
f.write('\ntag_format = "example-${version}"\n')
1566+
create_file_and_commit("feat: new file")
1567+
git.tag("v0.2.0")
1568+
create_file_and_commit("feat: another new file")
1569+
git.tag("0.2.0")
1570+
git.tag("random0.2.0")
1571+
testargs = ["cz", "bump", "--changelog", "--yes"]
1572+
mocker.patch.object(sys, "argv", testargs)
1573+
cli.main()
1574+
wait_for_tag()
1575+
create_file_and_commit("feat: some awesome new feature")
1576+
testargs = ["cz", "bump", "--changelog", "--yes"]
1577+
mocker.patch.object(sys, "argv", testargs)
1578+
cli.main()
1579+
wait_for_tag()
15431580
with open(changelog_path) as f:
15441581
out = f.read()
1545-
assert out.startswith("## 0.2.0custom (2021-06-11)")
1582+
assert out.startswith("## example-0.3.0 (2021-06-11)")
1583+
assert "## example-0.2.0 (2021-06-11)" in out
15461584
assert "## v0.2.0 (2021-06-11)" not in out
15471585
assert "## 0.2.0 (2021-06-11)" not in out
15481586

tests/test_changelog_format_markdown.py

+32
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@
7474
)
7575

7676

77+
CHANGELOG_E = """
78+
# Unreleased
79+
80+
## example-1.0.0
81+
"""
82+
EXPECTED_E = Metadata(
83+
latest_version="1.0.0",
84+
latest_version_position=3,
85+
unreleased_end=3,
86+
unreleased_start=1,
87+
)
88+
89+
90+
CHANGELOG_F = """
91+
# Unreleased
92+
93+
## 1.0.0-custom
94+
"""
95+
EXPECTED_F = Metadata(
96+
latest_version="1.0.0",
97+
latest_version_position=3,
98+
unreleased_end=3,
99+
unreleased_start=1,
100+
)
101+
102+
77103
@pytest.fixture
78104
def format(config: BaseConfig) -> Markdown:
79105
return Markdown(config)
@@ -94,6 +120,10 @@ def format(config: BaseConfig) -> Markdown:
94120
("All notable changes to this project will be documented in this file.", None),
95121
("# Changelog", None),
96122
("### Bug Fixes", None),
123+
("### 0.1.1custom", "0.1.1"),
124+
("### 0.1.1-custom", "0.1.1"),
125+
("### example0.1.1", "0.1.1"),
126+
("### example-0.1.1", "0.1.1"),
97127
]
98128

99129

@@ -127,6 +157,8 @@ def test_parse_title_type_of_line(
127157
pytest.param(CHANGELOG_B, EXPECTED_B, id="B"),
128158
pytest.param(CHANGELOG_C, EXPECTED_C, id="C"),
129159
pytest.param(CHANGELOG_D, EXPECTED_D, id="D"),
160+
pytest.param(CHANGELOG_E, EXPECTED_E, id="E"),
161+
pytest.param(CHANGELOG_F, EXPECTED_F, id="F"),
130162
),
131163
)
132164
def test_get_matadata(

0 commit comments

Comments
 (0)