Skip to content

Commit 126337f

Browse files
committed
Apply review suggestions
1 parent a55f2c9 commit 126337f

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

docs/configuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ Each pattern can be templated using Jinja.
902902

903903
```yaml
904904
_exclude:
905-
- '{%- if _operation == "update" -%}src/*_example.py{%- endif %}'
905+
- "{%- if _operation == 'update' -%}src/*_example.py{%- endif %}"
906906
```
907907

908908
!!! info

tests/test_context.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import json
1+
from pathlib import Path
22

33
import pytest
44
from plumbum import local
@@ -8,87 +8,90 @@
88
from .helpers import build_file_tree, git_save
99

1010

11-
def test_exclude_templating_with_operation(tmp_path_factory: pytest.TempPathFactory) -> None:
11+
def test_exclude_templating_with_operation(
12+
tmp_path_factory: pytest.TempPathFactory,
13+
) -> None:
1214
"""
1315
Ensure it's possible to create one-off boilerplate files that are not
14-
managed during updates via _exclude using the ``_operation`` context variable.
16+
managed during updates via `_exclude` using the `_operation` context variable.
1517
"""
1618
src, dst, dst2 = map(tmp_path_factory.mktemp, ("src", "dst", "dst2"))
1719

18-
template = r"{%- if _operation == 'update' %}boilerplate{%- endif %}"
20+
template = r"{%- if _operation == 'update' %}copy-only{%- endif %}"
1921
with local.cwd(src):
2022
build_file_tree(
2123
{
22-
"copier.yml": f"_exclude:\n - \"{template}\"",
24+
"copier.yml": f'_exclude:\n - "{template}"',
2325
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
24-
"boilerplate": "foo",
25-
"other_file": "foo",
26+
"copy-only": "foo",
27+
"copy-and-update": "foo",
2628
}
2729
)
2830
git_save(tag="1.0.0")
2931
build_file_tree(
3032
{
31-
"boilerplate": "bar",
32-
"other_file": "bar",
33+
"copy-only": "bar",
34+
"copy-and-update": "bar",
3335
}
3436
)
3537
git_save(tag="2.0.0")
36-
boilerplate = dst / "boilerplate"
37-
other_file = dst / "other_file"
38+
copy_only = dst / "copy-only"
39+
copy_and_update = dst / "copy-and-update"
3840

3941
copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0")
40-
for file in (boilerplate, other_file):
42+
for file in (copy_only, copy_and_update):
4143
assert file.exists()
4244
assert file.read_text() == "foo"
4345

4446
with local.cwd(dst):
4547
git_save()
4648

4749
copier.run_update(str(dst), overwrite=True)
48-
assert boilerplate.read_text() == "foo" # This file is excluded from updates
49-
assert other_file.read_text() == "bar"
50+
assert copy_only.read_text() == "foo"
51+
assert copy_and_update.read_text() == "bar"
5052

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

5961

6062
def test_task_templating_with_operation(
61-
tmp_path_factory: pytest.TempPathFactory
63+
tmp_path_factory: pytest.TempPathFactory, tmp_path: Path
6264
) -> None:
6365
"""
6466
Ensure that it is possible to define tasks that are only executed when copying.
6567
"""
6668
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
67-
task = {
68-
"command": ["{{ _copier_python }}", "-c", "from pathlib import Path; Path('foo').touch(exist_ok=False)"],
69-
"when": "{{ _operation == 'copy'}}",
70-
}
69+
# Use a file outside the Copier working directories to ensure accurate tracking
70+
task_counter = tmp_path / "task_calls.txt"
7171
with local.cwd(src):
7272
build_file_tree(
7373
{
74-
"copier.yml": f"_tasks: {json.dumps([task])}",
74+
"copier.yml": (
75+
f"""\
76+
_tasks:
77+
- command: "echo {{{{ _operation }}}} >> '{task_counter}'"
78+
when: "{{{{ _operation == 'copy' }}}}"
79+
"""
80+
),
7581
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
7682
}
7783
)
7884
git_save(tag="1.0.0")
7985

8086
copier.run_copy(str(src), dst, defaults=True, overwrite=True, unsafe=True)
81-
dst_file = dst / "foo"
82-
assert dst_file.exists()
87+
assert task_counter.exists()
88+
assert len(task_counter.read_text().splitlines()) == 1
8389

84-
dst_file.unlink()
8590
with local.cwd(dst):
8691
git_save()
8792

8893
copier.run_recopy(dst, defaults=True, overwrite=True, unsafe=True)
89-
assert dst_file.exists()
90-
91-
dst_file.unlink()
94+
assert len(task_counter.read_text().splitlines()) == 2
9295

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

0 commit comments

Comments
 (0)