From fcfd4dcabfccfaf728f30060793cadabe013ffdd Mon Sep 17 00:00:00 2001 From: grahamhar Date: Tue, 26 Mar 2024 18:24:21 +0000 Subject: [PATCH] WIP investigating tag formatting issues --- commitizen/commands/changelog.py | 9 ++++-- tests/commands/test_changelog_command.py | 40 +++++++++++++++++++++++- tests/test_changelog_format_markdown.py | 32 +++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 98e0fc5e8f..c458b56c60 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Callable -from commitizen import changelog, defaults, factory, git, out +from commitizen import bump, changelog, defaults, factory, git, out from commitizen.config import BaseConfig from commitizen.cz.base import MessageBuilderHook, ChangelogReleaseHook @@ -177,8 +177,13 @@ def __call__(self): if self.incremental: changelog_meta = self.changelog_format.get_metadata(self.file_name) if changelog_meta.latest_version: + latest_tag_version: str = bump.normalize_tag( + changelog_meta.latest_version, + tag_format=self.tag_format, + scheme=self.scheme, + ) start_rev = self._find_incremental_rev( - strip_local_version(changelog_meta.latest_version), tags + strip_local_version(latest_tag_version), tags ) if self.rev_range: start_rev, end_rev = changelog.get_oldest_and_newest_rev( diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 9ad5adbf61..157564c0d8 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1540,9 +1540,47 @@ def test_changelog_only_tag_matching_tag_format_included( mocker.patch.object(sys, "argv", testargs) cli.main() wait_for_tag() + create_file_and_commit("feat: some awesome new feature") + # git.tag("0.2.1") + testargs = ["cz", "bump", "--changelog", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + wait_for_tag() + with open(changelog_path) as f: + out = f.read() + assert out.startswith("## 0.3.0custom (2021-06-11)") + assert "## 0.2.0custom (2021-06-11)" in out + assert "## v0.2.0 (2021-06-11)" not in out + assert "## 0.2.0 (2021-06-11)" not in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.freeze_time("2021-06-11") +def test_changelog_only_tag_matching_tag_format_included_prefix( + mocker: MockFixture, + changelog_path: Path, + config_path: Path, +): + with open(config_path, "a", encoding="utf-8") as f: + f.write('\ntag_format = "example-${version}"\n') + create_file_and_commit("feat: new file") + git.tag("v0.2.0") + create_file_and_commit("feat: another new file") + git.tag("0.2.0") + git.tag("random0.2.0") + testargs = ["cz", "bump", "--changelog", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + wait_for_tag() + create_file_and_commit("feat: some awesome new feature") + testargs = ["cz", "bump", "--changelog", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + wait_for_tag() with open(changelog_path) as f: out = f.read() - assert out.startswith("## 0.2.0custom (2021-06-11)") + assert out.startswith("## example-0.3.0 (2021-06-11)") + assert "## example-0.2.0 (2021-06-11)" in out assert "## v0.2.0 (2021-06-11)" not in out assert "## 0.2.0 (2021-06-11)" not in out diff --git a/tests/test_changelog_format_markdown.py b/tests/test_changelog_format_markdown.py index 2e1ee69977..2c2c6768d1 100644 --- a/tests/test_changelog_format_markdown.py +++ b/tests/test_changelog_format_markdown.py @@ -74,6 +74,32 @@ ) +CHANGELOG_E = """ +# Unreleased + +## example-1.0.0 +""" +EXPECTED_E = Metadata( + latest_version="1.0.0", + latest_version_position=3, + unreleased_end=3, + unreleased_start=1, +) + + +CHANGELOG_F = """ +# Unreleased + +## 1.0.0-custom +""" +EXPECTED_F = Metadata( + latest_version="1.0.0", + latest_version_position=3, + unreleased_end=3, + unreleased_start=1, +) + + @pytest.fixture def format(config: BaseConfig) -> Markdown: return Markdown(config) @@ -94,6 +120,10 @@ def format(config: BaseConfig) -> Markdown: ("All notable changes to this project will be documented in this file.", None), ("# Changelog", None), ("### Bug Fixes", None), + ("### 0.1.1custom", "0.1.1"), + ("### 0.1.1-custom", "0.1.1"), + ("### example0.1.1", "0.1.1"), + ("### example-0.1.1", "0.1.1"), ] @@ -127,6 +157,8 @@ def test_parse_title_type_of_line( pytest.param(CHANGELOG_B, EXPECTED_B, id="B"), pytest.param(CHANGELOG_C, EXPECTED_C, id="C"), pytest.param(CHANGELOG_D, EXPECTED_D, id="D"), + pytest.param(CHANGELOG_E, EXPECTED_E, id="E"), + pytest.param(CHANGELOG_F, EXPECTED_F, id="F"), ), ) def test_get_matadata(