Skip to content

Commit 3c9988a

Browse files
committed
fix(changelog): include latest change when dry run and incremental
Closes #1024
1 parent cbcfa7f commit 3c9988a

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

commitizen/commands/changelog.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,6 @@ def write_changelog(
133133
if changelog_hook:
134134
changelog_out = changelog_hook(changelog_out, partial_changelog)
135135

136-
if self.dry_run:
137-
out.write(changelog_out)
138-
raise DryRunExit()
139-
140136
changelog_file.write(changelog_out)
141137

142138
def export_template(self):
@@ -221,6 +217,14 @@ def __call__(self):
221217
)
222218
changelog_out = changelog_out.lstrip("\n")
223219

220+
# Dry_run is executed here to avoid checking and reading the files
221+
if self.dry_run:
222+
changelog_hook: Callable | None = self.cz.changelog_hook
223+
if changelog_hook:
224+
changelog_out = changelog_hook(changelog_out, "")
225+
out.write(changelog_out)
226+
raise DryRunExit()
227+
224228
lines = []
225229
if self.incremental and os.path.isfile(self.file_name):
226230
with open(self.file_name, encoding=self.encoding) as changelog_file:

tests/commands/test_bump_command.py

+33
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,36 @@ def test_bump_template_extra_quotes(
14061406

14071407
changelog = project_root / any_changelog_format.default_changelog_file
14081408
assert changelog.read_text() == "no-quote - single quotes - double quotes"
1409+
1410+
1411+
def test_bump_changelog_contains_increment_only(mocker, tmp_commitizen_project, capsys):
1412+
"""Issue 1024"""
1413+
# Initialize commitizen up to v1.0.0
1414+
project_root = Path(tmp_commitizen_project)
1415+
tmp_commitizen_cfg_file = project_root / "pyproject.toml"
1416+
tmp_commitizen_cfg_file.write_text(
1417+
"[tool.commitizen]\n" 'version="1.0.0"\n' "update_changelog_on_bump = true\n"
1418+
)
1419+
tmp_changelog_file = project_root / "CHANGELOG.md"
1420+
tmp_changelog_file.write_text("## v1.0.0")
1421+
create_file_and_commit("feat(user): new file")
1422+
create_tag("v1.0.0")
1423+
1424+
# Add a commit and bump to v2.0.0
1425+
create_file_and_commit("feat(user)!: new file")
1426+
testargs = ["cz", "bump", "--yes"]
1427+
mocker.patch.object(sys, "argv", testargs)
1428+
cli.main()
1429+
_ = capsys.readouterr()
1430+
1431+
# Add a commit and create the incremental changelog to v3.0.0
1432+
# it should only include v3 changes
1433+
create_file_and_commit("feat(next)!: next version")
1434+
testargs = ["cz", "bump", "--yes", "--files-only", "--changelog-to-stdout"]
1435+
mocker.patch.object(sys, "argv", testargs)
1436+
with pytest.raises(ExpectedExit):
1437+
cli.main()
1438+
out, _ = capsys.readouterr()
1439+
1440+
assert "3.0.0" in out
1441+
assert "2.0.0" not in out

tests/commands/test_changelog_command.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,15 @@ def test_changelog_hook(mocker: MockFixture, config: BaseConfig, dry_run: bool):
294294
changelog()
295295
except DryRunExit:
296296
pass
297+
297298
full_changelog = (
298299
"## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
299300
)
301+
partial_changelog = full_changelog
302+
if dry_run:
303+
partial_changelog = ""
300304

301-
changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
305+
changelog_hook_mock.assert_called_with(full_changelog, partial_changelog)
302306

303307

304308
@pytest.mark.usefixtures("tmp_commitizen_project")

0 commit comments

Comments
 (0)