From 6cb92f9fd578189985adc1586520777cffcbbec4 Mon Sep 17 00:00:00 2001 From: Sigurd Spieckermann Date: Tue, 29 Oct 2024 13:40:18 +0100 Subject: [PATCH] fix: re-render answers file path when producing render context --- copier/main.py | 2 +- tests/test_answersfile_templating.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/copier/main.py b/copier/main.py index b616bb0d6..bb514fba0 100644 --- a/copier/main.py +++ b/copier/main.py @@ -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. diff --git a/tests/test_answersfile_templating.py b/tests/test_answersfile_templating.py index dbf68bdfa..6a951b8df 100644 --- a/tests/test_answersfile_templating.py +++ b/tests/test_answersfile_templating.py @@ -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"