Skip to content

Commit fcb501b

Browse files
committed
fix(changelog): resolved tpl.filename type
1 parent d0c89af commit fcb501b

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

commitizen/commands/changelog.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ def _write_changelog(
176176

177177
def _export_template(self) -> None:
178178
tpl = changelog.get_changelog_template(self.cz.template_loader, self.template)
179-
# TODO: fix the following type ignores
180-
src = Path(tpl.filename) # type: ignore[arg-type]
179+
180+
if tpl.filename is None:
181+
raise ValueError("Cannot export template: tpl.filename is None")
182+
183+
src = Path(tpl.filename)
181184
Path(self.export_template_to).write_text(src.read_text()) # type: ignore[arg-type]
182185

183186
def __call__(self) -> None:

tests/commands/test_changelog_command.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,32 @@ def test_export_changelog_template_from_plugin(
19141914
assert target.read_text() == tpl
19151915

19161916

1917+
def test_export_changelog_template_fails_when_template_has_no_filename(
1918+
mocker: MockFixture,
1919+
tmp_commitizen_project: Path,
1920+
):
1921+
project_root = Path(tmp_commitizen_project)
1922+
target = project_root / "changelog.jinja"
1923+
1924+
# Mock a template object with no filename
1925+
class FakeTemplate:
1926+
filename = None
1927+
1928+
# Patch get_changelog_template to return a template without a filename
1929+
mocker.patch(
1930+
"commitizen.changelog.get_changelog_template", return_value=FakeTemplate()
1931+
)
1932+
1933+
args = ["cz", "changelog", "--export-template", str(target)]
1934+
mocker.patch.object(sys, "argv", args)
1935+
1936+
with pytest.raises(ValueError) as exc_info:
1937+
cli.main()
1938+
1939+
assert not target.exists()
1940+
assert "tpl.filename is None" in str(exc_info.value)
1941+
1942+
19171943
@skip_below_py_3_13
19181944
def test_changelog_command_shows_description_when_use_help_option(
19191945
mocker: MockFixture, capsys, file_regression

0 commit comments

Comments
 (0)