Skip to content

Commit

Permalink
fix: skip excluded paths before rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp authored and yajo committed Nov 29, 2023
1 parent c857ec3 commit 8383bc9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,6 @@ def _render_allowed(
assert not dst_relpath.is_absolute()
assert not expected_contents or not is_dir, "Dirs cannot have expected content"
dst_abspath = Path(self.subproject.local_abspath, dst_relpath)
if dst_relpath != Path(".") and self.match_exclude(dst_relpath):
return False
previous_is_symlink = dst_abspath.is_symlink()
try:
previous_content: Union[bytes, Path]
Expand Down Expand Up @@ -676,6 +674,9 @@ def _render_path(self, relpath: Path) -> Optional[Path]:
part = Path(part).name
rendered_parts.append(part)
result = Path(*rendered_parts)
# Skip excluded paths.
if result != Path(".") and self.match_exclude(relpath):
return None
if not is_template:
templated_sibling = (
self.template.local_abspath
Expand Down
49 changes: 49 additions & 0 deletions tests/test_exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,52 @@ def test_config_exclude_with_subdirectory(
build_file_tree({src / "copier.yml": "", src / "template" / "copier.yml": ""})
run_copy(str(src), dst, quiet=True)
assert not (dst / "template" / "copier.yml").exists()


def test_config_exclude_copieryml_without_templates_suffix(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): (
"""\
_subdirectory: .
_templates_suffix: ""
_envops:
block_start_string: "[?"
block_end_string: "?]"
"""
),
}
)
run_copy(str(src), dst, quiet=True)
assert not (dst / "copier.yml").exists()


def test_config_exclude_file_with_bad_jinja_syntax_without_templates_suffix(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): (
"""\
_subdirectory: .
_templates_suffix: ""
_exclude:
- copier.yml
- exclude-me.txt
"""
),
(src / "exclude-me.txt"): (
"""\
"{%" is malformed Jinja syntax but it doesn't matter because
this file is excluded
"""
),
}
)
run_copy(str(src), dst, quiet=True)
assert not (dst / "copier.yml").exists()
assert not (dst / "exclude-me.txt").exists()

0 comments on commit 8383bc9

Please sign in to comment.