Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ filterwarnings =
error
# TODO: remove once pytest-xdist 4 is released
ignore:.*rsyncdir:DeprecationWarning:xdist
ignore:.*:ResourceWarning:plumbum
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ classifiers = [
"Typing :: Typed",
]
dependencies = [
"copier>=9.2",
"copier>=9.4",
# "copier>=9.5",
]

[project.urls]
Expand Down
4 changes: 4 additions & 0 deletions src/copier_templates_extensions/extensions/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def __init__(
DeprecationWarning,
stacklevel=1,
)

# if "_copier_conf" not in parent or parent["_copier_conf"]["phase"].value != "render":
# return

if "_copier_conf" in parent and (context := extension_self.hook(parent)) is not None: # type: ignore[attr-defined]
parent.update(context)
warnings.warn(
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/update_project/copier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
_jinja_extensions:
- copier_templates_extensions.TemplateExtensionLoader
- extensions.py:ContextUpdater

the_question:
type: str
6 changes: 6 additions & 0 deletions tests/fixtures/update_project/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from copier_templates_extensions import ContextHook


class ContextUpdater(ContextHook):
def hook(self, context):
context["success"] = bool(context["the_question"])
1 change: 1 addition & 0 deletions tests/fixtures/update_project/result.txt.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Success variable: {{ success|default("not set") }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{_copier_answers|to_nice_yaml}}
54 changes: 54 additions & 0 deletions tests/test_extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Tests for the `extensions` module."""

from __future__ import annotations

import shutil
import subprocess
from pathlib import Path

import copier
Expand All @@ -9,6 +13,24 @@
TEMPLATES_DIRECTORY = Path(__file__).parent / "fixtures"


def git(*args: str | Path) -> None:
"""Run a git command.

Arguments:
*args: The git command to run.
"""
subprocess.run( # noqa: S603
["git", *args], # noqa: S607
check=True,
env={
"GIT_AUTHOR_NAME": "Copier Test",
"GIT_AUTHOR_EMAIL": "copier@test",
"GIT_COMMITTER_NAME": "Copier Test",
"GIT_COMMITTER_EMAIL": "copier@test",
},
)


@pytest.mark.parametrize(
("template_name", "expected"),
[
Expand Down Expand Up @@ -77,3 +99,35 @@ def test_deprecated_usage(tmp_path: Path, template_name: str) -> None:
result_file = tmp_path / "result.txt"
assert result_file.exists()
assert result_file.read_text() == "Success variable: True"


def test_update(tmp_path_factory: pytest.TempPathFactory) -> None:
"""Test updating a project.

Arguments:
tmp_path: A pytest fixture.
expected: The parametrized value we expect.
"""
src_path = tmp_path_factory.mktemp("template")
dest_path = tmp_path_factory.mktemp("project")

shutil.copytree(TEMPLATES_DIRECTORY / "update_project", src_path, dirs_exist_ok=True)
git("-C", src_path, "init")
git("-C", src_path, "add", "-A", ".")
git("-C", src_path, "commit", "-m", "Initial commit")
git("-C", src_path, "tag", "0.1.0")

copier.run_copy(
str(src_path),
dest_path,
unsafe=True,
overwrite=True,
defaults=True,
data={"the_question": "the_answer"},
)
git("-C", dest_path, "init")
git("-C", dest_path, "add", "-A", ".")
git("-C", dest_path, "commit", "-m", "Initial commit")
git("-C", dest_path, "tag", "1.0.0")

copier.run_update(dest_path, unsafe=True, overwrite=True)
Loading