-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow templating
_answers_file
setting (#1027)
Co-authored-by: Ethan Barker <ethanbarker0@gmail.com>
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |