Skip to content
Open
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
6 changes: 6 additions & 0 deletions copier/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ def _ask(self) -> None: # noqa: C901
raise CopierAnswersInterrupt(
self.answers, question, self.template
) from err
# Computed values (i.e., `when: false`) are intentionally not
# validated at the moment.
# https://github.com/copier-org/copier/issues/1779#issuecomment-2365006990
# https://github.com/copier-org/copier/pull/1785
if question.get_when():
question.validate_answer(new_answer)
self.answers.user[var_name] = new_answer

# Reload external data, which may depend on answers
Expand Down
9 changes: 1 addition & 8 deletions copier/_user_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,7 @@ def get_default(self) -> Any:
result = self.render_value(
self.settings.defaults.get(self.var_name, self.default)
)
result = self.parse_answer(result)
# Computed values (i.e., `when: false`) are intentionally not validated
# at the moment.
# https://github.com/copier-org/copier/issues/1779#issuecomment-2365006990
# https://github.com/copier-org/copier/pull/1785
if self.get_when():
self.validate_answer(result)
return result
return self.parse_answer(result)

def get_default_rendered(self) -> bool | str | Choice | None | MissingType:
"""Get default answer rendered for the questionary lib.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,38 @@ def test_when(
assert context == {"question_2": "something"}


def test_default(tmp_path_factory: pytest.TempPathFactory, spawn: Spawn) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
(src / "copier.yml"): yaml.dump(
{
"_envops": BRACKET_ENVOPS,
"_templates_suffix": SUFFIX_TMPL,
"team": {
"type": "str",
"default": "@copier-org/",
"validator": "[% if not (team | regex_search('^@copier-org/.+$')) %]Must be a copier-org GitHub team[% endif %]",
},
}
),
(src / "[[ _copier_conf.answers_file ]].tmpl"): (
"[[ _copier_answers | to_nice_yaml ]]"
),
}
)
tui = spawn(COPIER_PATH + ("copy", str(src), str(dst)))
expect_prompt(tui, "team", "str")
tui.expect_exact("@copier-org/")
tui.sendline("everyone")
tui.expect_exact(pexpect.EOF)
answers = load_answersfile_data(dst)
assert answers == {
"_src_path": str(src),
"team": "@copier-org/everyone",
}


def test_placeholder(tmp_path_factory: pytest.TempPathFactory, spawn: Spawn) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
Expand Down
Loading