Skip to content

Commit 35dd1ff

Browse files
committed
fix(updating): disable secret question validator when replaying old copy
1 parent 0d8140d commit 35dd1ff

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

copier/_main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,13 @@ def _apply_update(self) -> None: # noqa: C901
11541154
src_path=self.subproject.template.url, # type: ignore[union-attr]
11551155
vcs_ref=self.subproject.template.commit, # type: ignore[union-attr]
11561156
) as old_worker:
1157+
# HACK: Remove the validator from a secret question when replaying
1158+
# a fresh copy using the old template because only the default value
1159+
# is available which may not pass validation.
1160+
assert old_worker.template is not self.template
1161+
for q in old_worker.template.questions_data.values():
1162+
if q.get("secret", False):
1163+
q.pop("validator", None)
11571164
old_worker.run_copy()
11581165
# Run pre-migration tasks
11591166
with Phase.use(Phase.MIGRATE):

tests/test_updatediff.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,3 +1982,44 @@ def test_conditional_computed_value(tmp_path_factory: pytest.TempPathFactory) ->
19821982
assert answers["first"] is True
19831983
assert answers["second"] is True
19841984
assert (dst / "log.txt").read_text() == "True True"
1985+
1986+
1987+
def test_disable_secret_validator_on_replay(
1988+
tmp_path_factory: pytest.TempPathFactory,
1989+
) -> None:
1990+
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
1991+
1992+
build_file_tree(
1993+
{
1994+
src / "copier.yml": (
1995+
"""\
1996+
token:
1997+
type: str
1998+
secret: true
1999+
default: ""
2000+
validator: "{% if token == '' %}Must not be empty{% endif %}"
2001+
"""
2002+
),
2003+
src / "{{ _copier_conf.answers_file }}.jinja": (
2004+
"{{ _copier_answers|to_nice_yaml }}"
2005+
),
2006+
src / ".gitignore": ".env",
2007+
src / ".env.jinja": "TOKEN={{ token }}",
2008+
}
2009+
)
2010+
with local.cwd(src):
2011+
git_init("v1")
2012+
git("tag", "v1")
2013+
2014+
run_copy(str(src), dst, data={"token": "$3cr3t"})
2015+
answers = load_answersfile_data(dst)
2016+
assert answers == {"_src_path": str(src), "_commit": "v1"}
2017+
assert (dst / ".env").read_text() == "TOKEN=$3cr3t"
2018+
2019+
with local.cwd(dst):
2020+
git_init("v1")
2021+
2022+
run_update(dst, data={"token": "$up3r-$3cr3t"}, overwrite=True)
2023+
answers = load_answersfile_data(dst)
2024+
assert answers == {"_src_path": str(src), "_commit": "v1"}
2025+
assert (dst / ".env").read_text() == "TOKEN=$up3r-$3cr3t"

0 commit comments

Comments
 (0)