|
5 | 5 | import pytest |
6 | 6 | from jinja2 import Environment |
7 | 7 | from jinja2.ext import Extension |
| 8 | +from plumbum import local |
8 | 9 |
|
9 | 10 | import copier |
10 | 11 |
|
11 | | -from .helpers import PROJECT_TEMPLATE, build_file_tree |
| 12 | +from .helpers import PROJECT_TEMPLATE, build_file_tree, git_save |
12 | 13 |
|
13 | 14 |
|
14 | 15 | class FilterExtension(Extension): |
@@ -36,6 +37,39 @@ def super_func(argument: Any) -> str: |
36 | 37 | environment.globals.update(super_var="super var!") |
37 | 38 |
|
38 | 39 |
|
| 40 | +@pytest.fixture |
| 41 | +def template_with_extension(tmp_path_factory: pytest.TempPathFactory) -> Path: |
| 42 | + src = tmp_path_factory.mktemp("src") |
| 43 | + build_file_tree( |
| 44 | + { |
| 45 | + src / "extensions" / "__init__.py": "", |
| 46 | + src / "extensions" / "data.py": """\ |
| 47 | + DATA_VALUE = "hello from data module" |
| 48 | + """, |
| 49 | + src / "extensions" / "filters.py": """\ |
| 50 | + from jinja2 import Environment |
| 51 | + from jinja2.ext import Extension |
| 52 | +
|
| 53 | + from extensions import data |
| 54 | +
|
| 55 | +
|
| 56 | + class DataFilter(Extension): |
| 57 | + def __init__(self, environment: Environment) -> None: |
| 58 | + super().__init__(environment) |
| 59 | + environment.filters["data_value"] = lambda _: data.DATA_VALUE |
| 60 | + """, |
| 61 | + src / "copier.yml": """\ |
| 62 | + _jinja_extensions: |
| 63 | + - extensions.filters.DataFilter |
| 64 | + """, |
| 65 | + src |
| 66 | + / "{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}", |
| 67 | + src / "result.txt.jinja": "{{ '' | data_value }}", |
| 68 | + } |
| 69 | + ) |
| 70 | + return src |
| 71 | + |
| 72 | + |
39 | 73 | def test_default_jinja2_extensions(tmp_path: Path) -> None: |
40 | 74 | copier.run_copy(str(PROJECT_TEMPLATE) + "_extensions_default", tmp_path) |
41 | 75 | super_file = tmp_path / "super_file.md" |
@@ -64,3 +98,44 @@ def test_to_json_filter_with_conf(tmp_path_factory: pytest.TempPathFactory) -> N |
64 | 98 | assert conf_file.exists() |
65 | 99 | # must not raise an error |
66 | 100 | assert json.loads(conf_file.read_text()) |
| 101 | + |
| 102 | + |
| 103 | +def test_extension_from_copy_with_vcs_ref( |
| 104 | + template_with_extension: Path, tmp_path_factory: pytest.TempPathFactory |
| 105 | +) -> None: |
| 106 | + dst = tmp_path_factory.mktemp("dst") |
| 107 | + with local.cwd(template_with_extension): |
| 108 | + git_save(tag="1.0.0") |
| 109 | + copier.run_copy(str(template_with_extension), dst, unsafe=True, vcs_ref="HEAD") |
| 110 | + result = dst / "result.txt" |
| 111 | + assert result.exists() |
| 112 | + assert result.read_text() == "hello from data module" |
| 113 | + |
| 114 | + |
| 115 | +def test_extension_on_update( |
| 116 | + template_with_extension: Path, tmp_path_factory: pytest.TempPathFactory |
| 117 | +) -> None: |
| 118 | + dst = tmp_path_factory.mktemp("dst") |
| 119 | + with local.cwd(template_with_extension): |
| 120 | + git_save(tag="1.0.0") |
| 121 | + # Initial copy |
| 122 | + copier.run_copy( |
| 123 | + str(template_with_extension), |
| 124 | + dst, |
| 125 | + unsafe=True, |
| 126 | + vcs_ref="1.0.0", |
| 127 | + defaults=True, |
| 128 | + overwrite=True, |
| 129 | + ) |
| 130 | + assert (dst / "result.txt").read_text() == "hello from data module" |
| 131 | + with local.cwd(dst): |
| 132 | + git_save() |
| 133 | + # Add a new file in v2 (extension stays the same) |
| 134 | + build_file_tree({template_with_extension / "v2.txt": "new in v2"}) |
| 135 | + with local.cwd(template_with_extension): |
| 136 | + git_save(tag="2.0.0") |
| 137 | + # Run update — extension should work because template is trusted |
| 138 | + copier.run_update(dst, unsafe=True, defaults=True, overwrite=True) |
| 139 | + # Extension still works during update rendering |
| 140 | + assert (dst / "result.txt").read_text() == "hello from data module" |
| 141 | + assert (dst / "v2.txt").read_text() == "new in v2" |
0 commit comments