Skip to content
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

feat: add dynamic file structures in loop using yield-tag #1855

Merged
merged 14 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions copier/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ def __init__(self, features: Sequence[str]):
)


class YieldTagInFileError(CopierError):
"""A yield tag is used in the file content, but it is not allowed."""

pass


# Warnings
class CopierWarning(Warning):
"""Base class for all other Copier warnings."""
Expand Down
5 changes: 5 additions & 0 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ExtensionNotFoundError,
UnsafeTemplateError,
UserMessageError,
YieldTagInFileError,
)
from .jinja_ext import YieldEnvironment, YieldExtension
from .subproject import Subproject
Expand Down Expand Up @@ -615,6 +616,10 @@ def _render_template(self) -> None:
self._render_folder(dst_relpath)
else:
self._render_file(src_relpath, dst_relpath, extra_context=ctx or {})
if self.jinja_env.yield_context:
raise YieldTagInFileError(
f"File {src_relpath} contains a yield tag, but it was not allowed."
)

def _render_file(
self,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_dynamic_file_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import copier
from copier.errors import YieldTagInFileError
from tests.helpers import build_file_tree


Expand Down Expand Up @@ -163,3 +164,25 @@ def test_folder_loop_dict_items(tmp_path_factory: pytest.TempPathFactory) -> Non
unexpected_files = set(all_files) - set(expected_files)

assert not unexpected_files, f"Unexpected files found: {unexpected_files}"


def test_raise_yield_tag_in_file(tmp_path_factory: pytest.TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
src / "copier.yml": "",
src
/ "file.txt.jinja": "{% yield item from strings %}{{ item }}{% endyield %}",
}
)

with pytest.raises(YieldTagInFileError, match="file.txt.jinja"):
copier.run_copy(
str(src),
dst,
data={
"strings": ["a", "b", "c"],
},
defaults=True,
overwrite=True,
)
Loading