Skip to content

Commit

Permalink
fix: render default list items for multi-select choice questions
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp committed Apr 18, 2024
1 parent b4801b6 commit 69662e3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion copier/user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,11 @@ def render_value(
template = self.jinja_env.from_string(value)
except TypeError:
# value was not a string
return value
return (
[self.render_value(item) for item in value]
if isinstance(value, list)
else value
)
try:
return template.render({**self.answers.combined, **(extra_answers or {})})
except UndefinedError as error:
Expand Down
62 changes: 62 additions & 0 deletions tests/test_templated_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,65 @@ def test_templated_prompt_with_conditional_choices(
for iac in iac_choices:
tui.expect_exact(iac)
tui.sendline()


def test_multiselect_choices_with_templated_default_value(
tmp_path_factory: pytest.TempPathFactory,
spawn: Spawn,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): (
"""\
python_version:
type: str
help: What version of python are you targeting?
default: "3.11"
choices:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
github_runner_python_version:
type: str
help: Which Python versions do you want to use on your Github Runner?
default: ["{{ python_version }}"]
multiselect: true
choices:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
"""
),
(src / "{{ _copier_conf.answers_file }}.jinja"): (
"{{ _copier_answers|to_nice_yaml }}"
),
}
)

tui = spawn(COPIER_PATH + ("copy", str(src), str(dst)), timeout=10)
expect_prompt(
tui, "python_version", "str", help="What version of python are you targeting?"
)
tui.sendline() # select `3.11" (default value)
expect_prompt(
tui,
"github_runner_python_version",
"str",
help="Which Python versions do you want to use on your Github Runner?",
)
tui.sendline() # select "[3.11]" (default value)
tui.expect_exact(pexpect.EOF)

answers_file = dst / ".copier-answers.yml"
assert answers_file.exists()
assert yaml.safe_load(answers_file.read_text()) == {
"_src_path": str(src),
"python_version": "3.11",
"github_runner_python_version": ["3.11"],
}

0 comments on commit 69662e3

Please sign in to comment.