Skip to content

Commit

Permalink
fix(changelog): include latest change when dry run and incremental
Browse files Browse the repository at this point in the history
Closes #1024
  • Loading branch information
woile committed Mar 14, 2024
1 parent cbcfa7f commit 3c9988a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
12 changes: 8 additions & 4 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
33 changes: 33 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3c9988a

Please sign in to comment.