Skip to content

Commit 267f374

Browse files
noirbizarrewoile
authored andcommitted
fix(changelog): changelog hook was not called on dry run
1 parent 3100fd5 commit 267f374

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

commitizen/commands/changelog.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def write_changelog(
132132

133133
if changelog_hook:
134134
changelog_out = changelog_hook(changelog_out, partial_changelog)
135+
136+
if self.dry_run:
137+
out.write(changelog_out)
138+
raise DryRunExit()
139+
135140
changelog_file.write(changelog_out)
136141

137142
def export_template(self):
@@ -216,10 +221,6 @@ def __call__(self):
216221
)
217222
changelog_out = changelog_out.lstrip("\n")
218223

219-
if self.dry_run:
220-
out.write(changelog_out)
221-
raise DryRunExit()
222-
223224
lines = []
224225
if self.incremental and os.path.isfile(self.file_name):
225226
with open(self.file_name, encoding=self.encoding) as changelog_file:

tests/commands/test_changelog_command.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from commitizen import __file__ as commitizen_init
1313
from commitizen import cli, git
1414
from commitizen.commands.changelog import Changelog
15+
from commitizen.config.base_config import BaseConfig
1516
from commitizen.cz.base import BaseCommitizen
1617
from commitizen.exceptions import (
1718
DryRunExit,
@@ -275,20 +276,24 @@ def test_changelog_incremental_keep_a_changelog_sample(
275276

276277

277278
@pytest.mark.usefixtures("tmp_commitizen_project")
278-
def test_changelog_hook(mocker: MockFixture, config):
279+
@pytest.mark.parametrize("dry_run", [True, False])
280+
def test_changelog_hook(mocker: MockFixture, config: BaseConfig, dry_run: bool):
279281
changelog_hook_mock = mocker.Mock()
280282
changelog_hook_mock.return_value = "cool changelog hook"
281283

282284
create_file_and_commit("feat: new file")
283285
create_file_and_commit("refactor: is in changelog")
284286
create_file_and_commit("Merge into master")
285287

286-
config.settings["change_type_order"] = ["Refactor", "Feat"]
288+
config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
287289
changelog = Changelog(
288-
config, {"unreleased_version": None, "incremental": True, "dry_run": False}
290+
config, {"unreleased_version": None, "incremental": True, "dry_run": dry_run}
289291
)
290292
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
291-
changelog()
293+
try:
294+
changelog()
295+
except DryRunExit:
296+
pass
292297
full_changelog = (
293298
"## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n"
294299
)

0 commit comments

Comments
 (0)