From fcb501b15fd1d57022c65488f015aa91ada36d4e Mon Sep 17 00:00:00 2001 From: ongdisheng Date: Sun, 6 Jul 2025 14:35:08 +0800 Subject: [PATCH] fix(changelog): resolved tpl.filename type --- commitizen/commands/changelog.py | 7 +++++-- tests/commands/test_changelog_command.py | 26 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index dc50eb3ad2..37faeca2ee 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -176,8 +176,11 @@ def _write_changelog( def _export_template(self) -> None: tpl = changelog.get_changelog_template(self.cz.template_loader, self.template) - # TODO: fix the following type ignores - src = Path(tpl.filename) # type: ignore[arg-type] + + if tpl.filename is None: + raise ValueError("Cannot export template: tpl.filename is None") + + src = Path(tpl.filename) Path(self.export_template_to).write_text(src.read_text()) # type: ignore[arg-type] def __call__(self) -> None: diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 0eb29cdb04..6fb3bf76c2 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1914,6 +1914,32 @@ def test_export_changelog_template_from_plugin( assert target.read_text() == tpl +def test_export_changelog_template_fails_when_template_has_no_filename( + mocker: MockFixture, + tmp_commitizen_project: Path, +): + project_root = Path(tmp_commitizen_project) + target = project_root / "changelog.jinja" + + # Mock a template object with no filename + class FakeTemplate: + filename = None + + # Patch get_changelog_template to return a template without a filename + mocker.patch( + "commitizen.changelog.get_changelog_template", return_value=FakeTemplate() + ) + + args = ["cz", "changelog", "--export-template", str(target)] + mocker.patch.object(sys, "argv", args) + + with pytest.raises(ValueError) as exc_info: + cli.main() + + assert not target.exists() + assert "tpl.filename is None" in str(exc_info.value) + + @skip_below_py_3_13 def test_changelog_command_shows_description_when_use_help_option( mocker: MockFixture, capsys, file_regression