From 47762b6fd7e70e66ee758262f53f913a4b4a4785 Mon Sep 17 00:00:00 2001 From: Santiago Fraire Date: Thu, 14 Mar 2024 11:11:35 +0100 Subject: [PATCH] fix(changelog): include latest change when dry run and incremental Closes #1024 --- commitizen/commands/changelog.py | 12 ++++++--- tests/commands/test_bump_command.py | 33 ++++++++++++++++++++++++ tests/commands/test_changelog_command.py | 6 ++++- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 5d710ed5d0..6b7a63ba9e 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -133,10 +133,6 @@ def write_changelog( if changelog_hook: changelog_out = changelog_hook(changelog_out, partial_changelog) - if self.dry_run: - out.write(changelog_out) - raise DryRunExit() - changelog_file.write(changelog_out) def export_template(self): @@ -221,6 +217,14 @@ def __call__(self): ) changelog_out = changelog_out.lstrip("\n") + # Dry_run is executed here to avoid checking and reading the files + if self.dry_run: + changelog_hook: Callable | None = self.cz.changelog_hook + if changelog_hook: + changelog_out = changelog_hook(changelog_out, "") + out.write(changelog_out) + raise DryRunExit() + lines = [] if self.incremental and os.path.isfile(self.file_name): with open(self.file_name, encoding=self.encoding) as changelog_file: diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index c9e73649f4..6cf8a8d00c 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -1406,3 +1406,36 @@ def test_bump_template_extra_quotes( changelog = project_root / any_changelog_format.default_changelog_file assert changelog.read_text() == "no-quote - single quotes - double quotes" + + +def test_bump_changelog_contains_increment_only(mocker, tmp_commitizen_project, capsys): + """Issue 1024""" + # Initialize commitizen up to v1.0.0 + project_root = Path(tmp_commitizen_project) + tmp_commitizen_cfg_file = project_root / "pyproject.toml" + tmp_commitizen_cfg_file.write_text( + "[tool.commitizen]\n" 'version="1.0.0"\n' "update_changelog_on_bump = true\n" + ) + tmp_changelog_file = project_root / "CHANGELOG.md" + tmp_changelog_file.write_text("## v1.0.0") + create_file_and_commit("feat(user): new file") + create_tag("v1.0.0") + + # Add a commit and bump to v2.0.0 + create_file_and_commit("feat(user)!: new file") + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + _ = capsys.readouterr() + + # Add a commit and create the incremental changelog to v3.0.0 + # it should only include v3 changes + create_file_and_commit("feat(next)!: next version") + testargs = ["cz", "bump", "--yes", "--files-only", "--changelog-to-stdout"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(ExpectedExit): + cli.main() + out, _ = capsys.readouterr() + + assert "3.0.0" in out + assert "2.0.0" not in out diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 971d04cce5..c8072831e8 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -294,11 +294,15 @@ def test_changelog_hook(mocker: MockFixture, config: BaseConfig, dry_run: bool): changelog() except DryRunExit: pass + full_changelog = ( "## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n" ) + partial_changelog = full_changelog + if dry_run: + partial_changelog = "" - changelog_hook_mock.assert_called_with(full_changelog, full_changelog) + changelog_hook_mock.assert_called_with(full_changelog, partial_changelog) @pytest.mark.usefixtures("tmp_commitizen_project")