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 committed Aug 20, 2024
1 parent e347f7b commit fcb4031
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 @@ -479,9 +479,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 @@ -501,6 +498,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 @@ -1478,3 +1478,31 @@ def test_update_with_new_file_in_template_and_project_via_migration(
>>>>>>> after updating
"""
)


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

Check warning on line 1486 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1486

Added line #L1486 was not covered by tests

with local.cwd(src):
build_file_tree(

Check warning on line 1489 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1488-L1489

Added lines #L1488 - L1489 were not covered by tests
{
"copier.yml": "boolean: false",
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_nice_yaml }}",
}
)
git_init("v1")
git("tag", "v1")

Check warning on line 1496 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1495-L1496

Added lines #L1495 - L1496 were not covered by tests

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

Check warning on line 1501 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1498-L1501

Added lines #L1498 - L1501 were not covered by tests

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

Check warning on line 1504 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1503-L1504

Added lines #L1503 - L1504 were not covered by tests

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

Check warning on line 1508 in tests/test_updatediff.py

View check run for this annotation

Codecov / codecov/patch

tests/test_updatediff.py#L1506-L1508

Added lines #L1506 - L1508 were not covered by tests

0 comments on commit fcb4031

Please sign in to comment.