Skip to content

Commit

Permalink
Apply review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb committed Nov 11, 2024
1 parent a55f2c9 commit 75c5603
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ Each pattern can be templated using Jinja.

```yaml
_exclude:
- '{%- if _operation == "update" -%}src/*_example.py{%- endif %}'
- "{%- if _operation == 'update' -%}src/*_example.py{%- endif %}"
```

!!! info
Expand Down
59 changes: 31 additions & 28 deletions tests/test_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
from pathlib import Path

import pytest
from plumbum import local
Expand All @@ -8,87 +8,90 @@
from .helpers import build_file_tree, git_save


def test_exclude_templating_with_operation(tmp_path_factory: pytest.TempPathFactory) -> None:
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 via _exclude using the ``_operation`` context variable.
managed during updates via `_exclude` using the `_operation` context variable.
"""
src, dst, dst2 = map(tmp_path_factory.mktemp, ("src", "dst", "dst2"))

template = r"{%- if _operation == 'update' %}boilerplate{%- endif %}"
template = r"{%- if _operation == 'update' %}copy-only{%- endif %}"
with local.cwd(src):
build_file_tree(
{
"copier.yml": f"_exclude:\n - \"{template}\"",
"copier.yml": f'_exclude:\n - "{template}"',
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
"boilerplate": "foo",
"other_file": "foo",
"copy-only": "foo",
"copy-and-update": "foo",
}
)
git_save(tag="1.0.0")
build_file_tree(
{
"boilerplate": "bar",
"other_file": "bar",
"copy-only": "bar",
"copy-and-update": "bar",
}
)
git_save(tag="2.0.0")
boilerplate = dst / "boilerplate"
other_file = dst / "other_file"
copy_only = dst / "copy-only"
copy_and_update = dst / "copy-and-update"

copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0")
for file in (boilerplate, other_file):
for file in (copy_only, copy_and_update):
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" # This file is excluded from updates
assert other_file.read_text() == "bar"
assert copy_only.read_text() == "foo"
assert copy_and_update.read_text() == "bar"

# After using the worker for an `update` operation, reuse it for a `copy` again.
# This checks that the cached `match_exclude` property is regenerated
# after a context switch back from update to copy.
copier.run_copy(str(src), dst2, defaults=True, overwrite=True, vcs_ref="1.0.0")
for filename in ("boilerplate", "other_file"):
for filename in ("copy-only", "copy-and-update"):
assert (dst2 / filename).exists()
assert (dst2 / filename).read_text() == "foo"


def test_task_templating_with_operation(
tmp_path_factory: pytest.TempPathFactory
tmp_path_factory: pytest.TempPathFactory, tmp_path: Path
) -> None:
"""
Ensure that it is possible to define tasks that are only executed when copying.
"""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
task = {
"command": ["{{ _copier_python }}", "-c", "from pathlib import Path; Path('foo').touch(exist_ok=False)"],
"when": "{{ _operation == 'copy'}}",
}
# Use a file outside the Copier working directories to ensure accurate tracking
task_counter = tmp_path / "task_calls.txt"
with local.cwd(src):
build_file_tree(
{
"copier.yml": f"_tasks: {json.dumps([task])}",
"copier.yml": (
f"""\
_tasks:
- command: "echo {{{{ _operation }}}} >> '{task_counter}'"
when: "{{{{ _operation == 'copy' }}}}"
"""
),
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
}
)
git_save(tag="1.0.0")

copier.run_copy(str(src), dst, defaults=True, overwrite=True, unsafe=True)
dst_file = dst / "foo"
assert dst_file.exists()
assert task_counter.exists()
assert len(task_counter.read_text().splitlines()) == 1

dst_file.unlink()
with local.cwd(dst):
git_save()

copier.run_recopy(dst, defaults=True, overwrite=True, unsafe=True)
assert dst_file.exists()

dst_file.unlink()
assert len(task_counter.read_text().splitlines()) == 2

copier.run_update(dst, defaults=True, overwrite=True, unsafe=True)
assert not dst_file.exists()
assert len(task_counter.read_text().splitlines()) == 2

0 comments on commit 75c5603

Please sign in to comment.