Skip to content

Commit

Permalink
fix: re-render answers file path when producing render context
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Oct 29, 2024
1 parent 05aa175 commit 6cb92f9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def _ask(self) -> None: # noqa: C901

self.answers = result

@cached_property
@property
def answers_relpath(self) -> Path:
"""Obtain the proper relative path for the answers file.
Expand Down
40 changes: 40 additions & 0 deletions tests/test_answersfile_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,43 @@ def test_answersfile_templating(
assert (tmp_path / first_answers_file).exists()
answers = load_answersfile_data(tmp_path, first_answers_file)
assert answers["module_name"] == "mymodule"


def test_answersfile_templating_with_message_before_copy(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
"""Test templated `_answers_file` setting with `_message_before_copy`.
Checks that the tempated answers file name is rendered correctly while
having printing a message before the "copy" operation, which uses the render
context before including any answers from the questionnaire.
"""
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): (
"""\
_answers_file: ".copier-answers-{{ module_name }}.yml"
_message_before_copy: "Hello world"
module_name:
type: str
"""
),
(src / "{{ _copier_conf.answers_file }}.jinja"): (
"""\
# Changes here will be overwritten by Copier
{{ _copier_answers|to_nice_yaml }}
"""
),
(src / "result.txt.jinja"): "{{ module_name }}",
}
)
copier.run_copy(
str(src), dst, data={"module_name": "mymodule"}, overwrite=True, unsafe=True
)
assert (dst / ".copier-answers-mymodule.yml").exists()
answers = load_answersfile_data(dst, ".copier-answers-mymodule.yml")
assert answers["module_name"] == "mymodule"
assert (dst / "result.txt").exists()
assert (dst / "result.txt").read_text() == "mymodule"

0 comments on commit 6cb92f9

Please sign in to comment.