Skip to content

Commit

Permalink
fix: parse new answer when --skip-answered is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sisp authored and yajo committed Sep 21, 2024
1 parent 2421d77 commit 04dc881
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,6 @@ def _ask(self) -> None: # noqa: C901
else:
if question.validate_answer(answer):
del result.last[var_name]
# Skip a question when the user already answered it.
if self.skip_answered and var_name in result.last:
continue
# Skip a question when the skip condition is met.
if not question.get_when():
# Omit its answer from the answers file.
Expand All @@ -502,6 +499,9 @@ def _ask(self) -> None: # noqa: C901
# question again, but set answer as the user's answer instead.
result.user[var_name] = answer
continue
# Skip a question when the user already answered it.
if self.skip_answered and var_name in result.last:
continue

# Display TUI and ask user interactively only without --defaults
try:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_recopy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
import yaml
from plumbum import local

from copier import run_copy, run_recopy
Expand Down Expand Up @@ -72,3 +73,28 @@ def test_recopy_works_without_replay(tpl: str, tmp_path: Path) -> None:
# Recopy
run_recopy(tmp_path, skip_answered=True, overwrite=True)
assert (tmp_path / "name.txt").read_text() == "This is my name: Mario."


def test_recopy_with_skip_answered_and_new_answer(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
build_file_tree(
{
src / "copier.yml": "boolean: false",
src / "{{ _copier_conf.answers_file }}.jinja": (
"{{ _copier_answers|to_nice_yaml }}"
),
}
)
git_save(src)
# First copy
run_copy(str(src), dst, defaults=True, overwrite=True)
git_save(dst)
answers_file = dst / ".copier-answers.yml"
answers = yaml.safe_load(answers_file.read_text())
assert answers["boolean"] is False
# Recopy with different answer and `skip_answered=True`
run_recopy(dst, data={"boolean": "true"}, skip_answered=True, overwrite=True)
answers = yaml.safe_load(answers_file.read_text())
assert answers["boolean"] is True
28 changes: 28 additions & 0 deletions tests/test_updatediff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,31 @@ def test_update_with_separate_git_directory(

run_update(dst, overwrite=True)
assert "_commit: v2" in (dst / ".copier-answers.yml").read_text()


def test_update_with_skip_answered_and_new_answer(
tmp_path_factory: pytest.TempPathFactory,
) -> None:
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))

with local.cwd(src):
build_file_tree(
{
"copier.yml": "boolean: false",
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_nice_yaml }}",
}
)
git_init("v1")
git("tag", "v1")

run_copy(str(src), dst, defaults=True, overwrite=True)
answers_file = dst / ".copier-answers.yml"
answers = yaml.safe_load(answers_file.read_text())
assert answers["boolean"] is False

with local.cwd(dst):
git_init("v1")

run_update(dst, data={"boolean": "true"}, skip_answered=True, overwrite=True)
answers = yaml.safe_load(answers_file.read_text())
assert answers["boolean"] is True

0 comments on commit 04dc881

Please sign in to comment.