|
1 |
| -import json |
| 1 | +from pathlib import Path |
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from plumbum import local
|
|
8 | 8 | from .helpers import build_file_tree, git_save
|
9 | 9 |
|
10 | 10 |
|
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: |
12 | 14 | """
|
13 | 15 | 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. |
15 | 17 | """
|
16 | 18 | src, dst, dst2 = map(tmp_path_factory.mktemp, ("src", "dst", "dst2"))
|
17 | 19 |
|
18 |
| - template = r"{%- if _operation == 'update' %}boilerplate{%- endif %}" |
| 20 | + template = r"{%- if _operation == 'update' %}copy-only{%- endif %}" |
19 | 21 | with local.cwd(src):
|
20 | 22 | build_file_tree(
|
21 | 23 | {
|
22 |
| - "copier.yml": f"_exclude:\n - \"{template}\"", |
| 24 | + "copier.yml": f'_exclude:\n - "{template}"', |
23 | 25 | "{{ _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", |
26 | 28 | }
|
27 | 29 | )
|
28 | 30 | git_save(tag="1.0.0")
|
29 | 31 | build_file_tree(
|
30 | 32 | {
|
31 |
| - "boilerplate": "bar", |
32 |
| - "other_file": "bar", |
| 33 | + "copy-only": "bar", |
| 34 | + "copy-and-update": "bar", |
33 | 35 | }
|
34 | 36 | )
|
35 | 37 | 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" |
38 | 40 |
|
39 | 41 | 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): |
41 | 43 | assert file.exists()
|
42 | 44 | assert file.read_text() == "foo"
|
43 | 45 |
|
44 | 46 | with local.cwd(dst):
|
45 | 47 | git_save()
|
46 | 48 |
|
47 | 49 | 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" |
50 | 52 |
|
51 | 53 | # After using the worker for an `update` operation, reuse it for a `copy` again.
|
52 | 54 | # This checks that the cached `match_exclude` property is regenerated
|
53 | 55 | # after a context switch back from update to copy.
|
54 | 56 | 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"): |
56 | 58 | assert (dst2 / filename).exists()
|
57 | 59 | assert (dst2 / filename).read_text() == "foo"
|
58 | 60 |
|
59 | 61 |
|
60 | 62 | def test_task_templating_with_operation(
|
61 |
| - tmp_path_factory: pytest.TempPathFactory |
| 63 | + tmp_path_factory: pytest.TempPathFactory, tmp_path: Path |
62 | 64 | ) -> None:
|
63 | 65 | """
|
64 | 66 | Ensure that it is possible to define tasks that are only executed when copying.
|
65 | 67 | """
|
66 | 68 | 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" |
71 | 71 | with local.cwd(src):
|
72 | 72 | build_file_tree(
|
73 | 73 | {
|
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 | + ), |
75 | 81 | "{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
|
76 | 82 | }
|
77 | 83 | )
|
78 | 84 | git_save(tag="1.0.0")
|
79 | 85 |
|
80 | 86 | 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 |
83 | 89 |
|
84 |
| - dst_file.unlink() |
85 | 90 | with local.cwd(dst):
|
86 | 91 | git_save()
|
87 | 92 |
|
88 | 93 | 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 |
92 | 95 |
|
93 | 96 | 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