Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse templates in _answers_file setting #1027

Merged
merged 6 commits into from
Mar 25, 2023
Merged
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
4 changes: 3 additions & 1 deletion copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ def answers_relpath(self) -> Path:
2. Template default.
3. Copier default.
"""
return self.answers_file or self.template.answers_relpath
path = self.answers_file or self.template.answers_relpath
template = self.jinja_env.from_string(str(path))
return Path(template.render(**self.answers.combined))
icj217 marked this conversation as resolved.
Show resolved Hide resolved

@cached_property
def all_exclusions(self) -> StrSeq:
Expand Down
74 changes: 74 additions & 0 deletions tests/test_answersfile_templating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest

import copier
from copier.user_data import load_answersfile_data

from .helpers import build_file_tree


@pytest.fixture(scope="module")
def template_path(tmp_path_factory) -> str:
root = tmp_path_factory.mktemp("template")
build_file_tree(
{
root
/ "{{ _copier_conf.answers_file }}.jinja": """\
# Changes here will be overwritten by Copier
{{ _copier_answers|to_nice_yaml }}
""",
root
/ "copier.yml": """\
_answers_file: ".copier-answers-{{ module_name }}.yml"

module_name:
type: str
""",
}
)
return str(root)


@pytest.mark.parametrize("answers_file", [None, ".changed-by-user.yml"])
def test_answersfile_templating(template_path, tmp_path, answers_file):
"""
Test copier behaves properly when _answers_file contains a template

Checks that template is resolved successfully and that a subsequent
copy that resolves to a different answers file doesn't clobber the
old answers file.
"""
copier.copy(
template_path,
tmp_path,
{"module_name": "mymodule"},
answers_file=answers_file,
defaults=True,
overwrite=True,
)
first_answers_file = (
".copier-answers-mymodule.yml"
if answers_file is None
else ".changed-by-user.yml"
)
assert (tmp_path / first_answers_file).exists()
answers = load_answersfile_data(tmp_path, first_answers_file)
assert answers["module_name"] == "mymodule"

copier.copy(
template_path,
tmp_path,
{"module_name": "anothermodule"},
defaults=True,
overwrite=True,
)

# Assert second one created
second_answers_file = ".copier-answers-anothermodule.yml"
assert (tmp_path / second_answers_file).exists()
answers = load_answersfile_data(tmp_path, second_answers_file)
assert answers["module_name"] == "anothermodule"

# Assert first one still exists
assert (tmp_path / first_answers_file).exists()
answers = load_answersfile_data(tmp_path, first_answers_file)
assert answers["module_name"] == "mymodule"