Skip to content

fix(changelog): resolved tpl.filename type #1556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down