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

fix: re-render answers file path when producing render context #1840

Merged
merged 1 commit into from
Oct 29, 2024
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
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"
Loading