Skip to content

Commit

Permalink
Refactor tests, render Jinja in _exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Aug 16, 2024
1 parent eea53da commit aa434c4
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 94 deletions.
2 changes: 1 addition & 1 deletion copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def _pathjoin(
@cached_property
def match_exclude(self) -> Callable[[Path], bool]:
"""Get a callable to match paths against all exclusions."""
return self._path_matcher(self.all_exclusions)
return self._path_matcher(map(self._render_string, self.all_exclusions))

@cached_property
def match_skip(self) -> Callable[[Path], bool]:
Expand Down
4 changes: 0 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@

from .helpers import Spawn

pytest_plugins = [
"tests.templates",
]


@pytest.fixture
def spawn() -> Spawn:
Expand Down
37 changes: 0 additions & 37 deletions tests/templates.py

This file was deleted.

76 changes: 76 additions & 0 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest
from plumbum import local

import copier

from .helpers import build_file_tree, git_save


@pytest.mark.parametrize("operation", ("recopy", "update"))
def test_operation_in_context_matches(
operation: str, tmp_path_factory: pytest.TempPathFactory
) -> None:
"""
Ensure that the _copier_conf.operation context variable is set
as expected during template rendering.
"""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
with local.cwd(src):
build_file_tree(
{
# Ensure the file is regenerated on update.
"copier.yml": "_skip_if_exists: [foo]",
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
"foo.jinja": "{{ _copier_conf.operation }}",
}
)
git_save(tag="1.0.0")

copier.run_copy(str(src), dst, defaults=True, overwrite=True)
ctx_file = dst / "foo"
assert ctx_file.read_text() == "copy"
# Ensure the file is regenerated on update.
# If we left it, an update would detect custom changes
# that would be reapplied, i.e. an update would leave us with `copy`.
ctx_file.unlink()
with local.cwd(dst):
git_save()
getattr(copier, f"run_{operation}")(str(dst), overwrite=True)
assert ctx_file.read_text() == operation


def test_exclude_templating_with_operation(tmp_path_factory: pytest.TempPathFactory) -> None:
"""
Ensure it's possible to create one-off boilerplate files
that are not managed during updates.
"""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
exclude = r"{%- if _copier_conf.operation == 'update' %}dumb_boilerplate{%- endif %}"
with local.cwd(src):
build_file_tree(
{
"copier.yml": f"_exclude:\n - \"{exclude}\"",
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
"dumb_boilerplate": "foo",
"other_file": "foo",
}
)
git_save(tag="1.0.0")
build_file_tree(
{
"dumb_boilerplate": "bar",
"other_file": "bar",
}
)
git_save(tag="2.0.0")
copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0")
boilerplate = dst / "dumb_boilerplate"
other_file = dst / "other_file"
for file in (boilerplate, other_file):
assert file.exists()
assert file.read_text() == "foo"
with local.cwd(dst):
git_save()
copier.run_update(str(dst), overwrite=True)
assert boilerplate.read_text() == "foo"
assert other_file.read_text() == "bar"
10 changes: 0 additions & 10 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,13 +945,3 @@ def test_multiselect_choices_preserve_order(
)
copier.run_copy(str(src), dst, data={"q": ["three", "one", "two"]})
assert yaml.safe_load((dst / "q.yml").read_text()) == ["one", "two", "three"]


def test_operation_context(tmp_path: Path, operation_context_template: Path) -> None:
run_copy(str(operation_context_template), tmp_path)
conditional_file = tmp_path / "foo"
expected = "_copy" in operation_context_template.name
assert conditional_file.exists() is expected
if expected:
assert conditional_file.read_text() == "foo"
assert (tmp_path / "bar").read_text() == "bar"
20 changes: 0 additions & 20 deletions tests/test_recopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,3 @@ def test_recopy_works_without_replay(tpl: str, tmp_path: Path) -> None:
# Recopy
run_recopy(tmp_path, skip_answered=True, overwrite=True)
assert (tmp_path / "name.txt").read_text() == "This is my name: Mario."


def test_operation_context(tmp_path: Path, operation_context_template: Path) -> None:
run_copy(str(operation_context_template), tmp_path)
git_save(tmp_path)
conditional_file = tmp_path / "foo"
expected_copy = "_copy" in operation_context_template.name
expected_recopy = "recopy" in operation_context_template.name
assert conditional_file.exists() is expected_copy
assert (tmp_path / "bar").read_text() == "bar"
if expected_copy:
assert conditional_file.read_text() == "foo"
conditional_file.unlink()
(tmp_path / "bar").write_text("baz")
git_save(tmp_path)
run_recopy(str(tmp_path), overwrite=True)
assert conditional_file.exists() is expected_recopy
if expected_recopy:
assert conditional_file.read_text() == "foo"
assert (tmp_path / "bar").read_text() == "bar"
22 changes: 0 additions & 22 deletions tests/test_updatediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
build_file_tree,
git,
git_init,
git_save,
)


Expand Down Expand Up @@ -1291,24 +1290,3 @@ def test_update_with_new_file_in_template_and_project_via_migration(
>>>>>>> after updating
"""
)


def test_operation_context(tmp_path: Path, operation_context_template: Path, request: pytest.FixtureRequest) -> None:
run_copy(str(operation_context_template), tmp_path)
conditional_file = tmp_path / "foo"
expected_copy = "_copy" in operation_context_template.name
expected_update = "update" in operation_context_template.name
assert conditional_file.exists() is expected_copy
assert (tmp_path / "bar").read_text() == "bar"
if expected_copy:
assert conditional_file.read_text() == "foo"
git_save(tmp_path)
request.getfixturevalue("operation_context_template_v2")
run_update(str(tmp_path), overwrite=True)
if expected_update:
assert conditional_file.read_text() == "foo_update"
elif expected_copy:
assert conditional_file.read_text() == "foo"
else:
assert not conditional_file.exists()
assert (tmp_path / "bar").read_text() == "bar_update"

0 comments on commit aa434c4

Please sign in to comment.