diff --git a/commitizen/changelog_formats/base.py b/commitizen/changelog_formats/base.py index dcddb5d7e0..87747f96f5 100644 --- a/commitizen/changelog_formats/base.py +++ b/commitizen/changelog_formats/base.py @@ -47,10 +47,11 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: meta = Metadata() unreleased_level: int | None = None for index, line in enumerate(file): - line = line.strip().lower() + # Keep the original case: `tag_format` can contain uppercase characters. + line = line.strip() unreleased: int | None = None - if "unreleased" in line: + if "unreleased" in line.lower(): unreleased = self.parse_title_level(line) # Try to find beginning and end lines of the unreleased block if unreleased: @@ -84,7 +85,7 @@ def get_latest_full_release_from_file(self, file: IO[Any]) -> IncrementalMergeIn latest_version_index: int | None = None for index, line in enumerate(file): latest_version_index = index - line = line.strip().lower() + line = line.strip() parsed_version = self.parse_version_from_title(line) if ( diff --git a/commitizen/changelog_formats/restructuredtext.py b/commitizen/changelog_formats/restructuredtext.py index e8ce411e80..5b4380c333 100644 --- a/commitizen/changelog_formats/restructuredtext.py +++ b/commitizen/changelog_formats/restructuredtext.py @@ -24,7 +24,8 @@ def get_metadata_from_file(self, file: IO[str]) -> Metadata: out_metadata = Metadata() unreleased_title_kind: str | tuple[str, str] | None = None is_overlined_title = False - lines = [line.strip().lower() for line in file.readlines()] + # Keep the original case: `tag_format` can contain uppercase characters. + lines = [line.strip() for line in file.readlines()] for index, (first, second, third) in enumerate( zip_longest(lines, lines[1:], lines[2:], fillvalue="") @@ -44,7 +45,7 @@ def get_metadata_from_file(self, file: IO[str]) -> Metadata: if not title: continue - if "unreleased" in title: + if "unreleased" in title.lower(): unreleased_title_kind = kind out_metadata.unreleased_start = index continue diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 8922b256f9..d6a955cc9c 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1461,6 +1461,34 @@ def test_changelog_only_tag_matching_tag_format_included_suffix_sep( 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_incremental_with_uppercase_tag_format( + changelog_path: Path, + config_path: Path, + util: UtilFixture, +): + """An uppercase character in `tag_format` must not break the incremental changelog. + + Before the fix, the existing changelog was read in lower case, so the latest + release title did not match `tag_format` and all versions were written again. + """ + # Arrange + with config_path.open("a", encoding="utf-8") as f: + f.write('\ntag_format = "Release-${version}"\n') + util.create_file_and_commit("feat: new file") + util.run_cli("bump", "--changelog", "--yes") + util.create_file_and_commit("fix: some fix") + + # Act + util.run_cli("bump", "--changelog", "--yes") + + # Assert + out = changelog_path.read_text() + assert out.startswith("## Release-0.2.1 (2021-06-11)") + assert out.count("## Release-0.2.0 (2021-06-11)") == 1 + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_legacy_tags( changelog_path: Path, diff --git a/tests/test_changelog_format_asciidoc.py b/tests/test_changelog_format_asciidoc.py index a56a05ba08..03a4d4d67c 100644 --- a/tests/test_changelog_format_asciidoc.py +++ b/tests/test_changelog_format_asciidoc.py @@ -197,6 +197,7 @@ def test_get_latest_full_release_no_file(format: AsciiDoc): "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_markdown.py b/tests/test_changelog_format_markdown.py index 1b75ed1f7f..0cb758cd5a 100644 --- a/tests/test_changelog_format_markdown.py +++ b/tests/test_changelog_format_markdown.py @@ -198,6 +198,7 @@ def test_get_metadata( "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_restructuredtext.py b/tests/test_changelog_format_restructuredtext.py index 00f7740737..2da44182d4 100644 --- a/tests/test_changelog_format_restructuredtext.py +++ b/tests/test_changelog_format_restructuredtext.py @@ -368,6 +368,7 @@ def test_is_overlined_title(text: str, expected: bool): "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_textile.py b/tests/test_changelog_format_textile.py index 481f903a7a..8eada39542 100644 --- a/tests/test_changelog_format_textile.py +++ b/tests/test_changelog_format_textile.py @@ -186,6 +186,7 @@ def test_get_metadata( "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], )