copier/tests/test_conditional_file_name.py
Jairo Llopis 6996b9cc7a feat: add recopy command and function
This new command allows to reapply a template, keeping old answers but discarding subproject evolution.

It is useful when there are bugs replaying an old version of the template, or when the subproject has drifted too much from the template and you need to reset it.

BREAKING CHANGE: All CLI calls to Copier must now include the subcommand as the 1st argument. For example, `copier` must become now `copier update`; also `copier ./tpl ./dst` must become `copier copy ./tpl ./dst`.

BREAKING CHANGE: All flags must go after the subcommand now. For example, `copier -r HEAD update ./dst` must now become `copier update -r HEAD ./dst` or `copier update ./dst -r HEAD`.

BREAKING CHANGE: Automatic mode removed. Since now subcommands are required, the automatic mode is removed.

BREAKING CHANGE: Deprecated `copier.copy` function is removed. Use `copier.run_copy`, `copier.run_update` or `copier.run_recopy` explicitly as needed.

Fix https://github.com/copier-org/copier/issues/1081
Close https://github.com/copier-org/copier/issues/1082
2023-05-17 11:56:13 +01:00

114 lines
3.5 KiB
Python

import pexpect
import pytest
from plumbum import local
from plumbum.cmd import git
from pytest import TempPathFactory
import copier
from .helpers import COPIER_PATH, Spawn, build_file_tree, expect_prompt
def test_render_conditional(tmp_path_factory: TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "{% if conditional %}file.txt{% endif %}.jinja"): (
"This is {{ conditional.variable }}."
),
}
)
copier.run_copy(str(src), dst, data={"conditional": {"variable": True}})
assert (dst / "file.txt").read_text() == "This is True."
def test_dont_render_conditional(tmp_path_factory: TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "{% if conditional %}file.txt{% endif %}.jinja"): (
"This is {{ conditional.variable }}."
),
}
)
copier.run_copy(str(src), dst)
assert not (dst / "file.txt").exists()
def test_render_conditional_subdir(tmp_path_factory: TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "subdir" / "{% if conditional %}file.txt{% endif %}.jinja"): (
"This is {{ conditional.variable }}."
),
}
)
copier.run_copy(str(src), dst, data={"conditional": {"variable": True}})
assert (dst / "subdir" / "file.txt").read_text() == "This is True."
def test_dont_render_conditional_subdir(tmp_path_factory: TempPathFactory) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "subdir" / "{% if conditional %}file.txt{% endif %}.jinja"): (
"This is a {{ conditional.variable }}."
),
}
)
copier.run_copy(str(src), dst)
assert not (dst / "subdir" / "file.txt").exists()
@pytest.mark.parametrize("interactive", [False, True])
def test_answer_changes(
tmp_path_factory: TempPathFactory, spawn: Spawn, interactive: bool
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
with local.cwd(src):
build_file_tree(
{
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_nice_yaml }}",
"copier.yml": """
condition:
type: bool
""",
"{% if condition %}file.txt{% endif %}.jinja": "",
}
)
git("init")
git("add", ".")
git("commit", "-mv1")
git("tag", "v1")
if interactive:
tui = spawn(COPIER_PATH + ("copy", str(src), str(dst)), timeout=10)
expect_prompt(tui, "condition", "bool")
tui.expect_exact("(y/N)")
tui.sendline("y")
tui.expect_exact("Yes")
tui.expect_exact(pexpect.EOF)
else:
copier.run_copy(str(src), dst, data={"condition": True})
assert (dst / "file.txt").exists()
with local.cwd(dst):
git("init")
git("add", ".")
git("commit", "-mv1")
if interactive:
tui = spawn(COPIER_PATH + ("update", "--overwrite", str(dst)), timeout=10)
expect_prompt(tui, "condition", "bool")
tui.expect_exact("(Y/n)")
tui.sendline("n")
tui.expect_exact("No")
tui.expect_exact(pexpect.EOF)
else:
copier.run_update(dst_path=dst, data={"condition": False}, overwrite=True)
assert not (dst / "file.txt").exists()