|
1 | 1 | from pathlib import Path
|
2 |
| -from typing import Any, Dict, List, Mapping, Tuple, Union |
| 2 | +from typing import Any, Dict, List, Mapping, Protocol, Tuple, Union |
3 | 3 |
|
4 | 4 | import pexpect
|
5 | 5 | import pytest
|
6 | 6 | import yaml
|
| 7 | +from pexpect.popen_spawn import PopenSpawn |
7 | 8 | from plumbum import local
|
8 | 9 | from plumbum.cmd import git
|
9 | 10 |
|
|
21 | 22 | git_save,
|
22 | 23 | )
|
23 | 24 |
|
| 25 | +try: |
| 26 | + from typing import TypeAlias # type: ignore[attr-defined] |
| 27 | +except ImportError: |
| 28 | + from typing_extensions import TypeAlias |
| 29 | + |
| 30 | + |
24 | 31 | MARIO_TREE: Mapping[StrOrPath, Union[str, bytes]] = {
|
25 | 32 | "copier.yml": (
|
26 | 33 | f"""\
|
@@ -785,3 +792,163 @@ def test_required_choice_question(
|
785 | 792 | "_src_path": str(src),
|
786 | 793 | "question": expected_answer,
|
787 | 794 | }
|
| 795 | + |
| 796 | + |
| 797 | +QuestionType: TypeAlias = str |
| 798 | +QuestionChoices: TypeAlias = Union[List[Any], Dict[str, Any]] |
| 799 | +ParsedValues: TypeAlias = List[Any] |
| 800 | + |
| 801 | +_CHOICES: Dict[str, Tuple[QuestionType, QuestionChoices, ParsedValues]] = { |
| 802 | + "str": ("str", ["one", "two", "three"], ["one", "two", "three"]), |
| 803 | + "int": ("int", [1, 2, 3], [1, 2, 3]), |
| 804 | + "int-label-list": ("int", [["one", 1], ["two", 2], ["three", 3]], [1, 2, 3]), |
| 805 | + "int-label-dict": ("int", {"1. one": 1, "2. two": 2, "3. three": 3}, [1, 2, 3]), |
| 806 | + "float": ("float", [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]), |
| 807 | + "json": ("json", ["[1]", "[2]", "[3]"], [[1], [2], [3]]), |
| 808 | + "yaml": ("yaml", ["- 1", "- 2", "- 3"], [[1], [2], [3]]), |
| 809 | +} |
| 810 | +CHOICES = [pytest.param(*specs, id=id) for id, specs in _CHOICES.items()] |
| 811 | + |
| 812 | + |
| 813 | +class QuestionTreeFixture(Protocol): |
| 814 | + def __call__(self, **kwargs) -> Tuple[Path, Path]: |
| 815 | + ... |
| 816 | + |
| 817 | + |
| 818 | +@pytest.fixture |
| 819 | +def question_tree(tmp_path_factory: pytest.TempPathFactory) -> QuestionTreeFixture: |
| 820 | + def builder(**question) -> Tuple[Path, Path]: |
| 821 | + src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) |
| 822 | + build_file_tree( |
| 823 | + { |
| 824 | + (src / "copier.yml"): yaml.dump( |
| 825 | + { |
| 826 | + "_envops": BRACKET_ENVOPS, |
| 827 | + "_templates_suffix": SUFFIX_TMPL, |
| 828 | + "question": question, |
| 829 | + } |
| 830 | + ), |
| 831 | + (src / "[[ _copier_conf.answers_file ]].tmpl"): ( |
| 832 | + "[[ _copier_answers|to_nice_yaml ]]" |
| 833 | + ), |
| 834 | + } |
| 835 | + ) |
| 836 | + return src, dst |
| 837 | + |
| 838 | + return builder |
| 839 | + |
| 840 | + |
| 841 | +class CopierFixture(Protocol): |
| 842 | + def __call__(self, *args, **kwargs) -> PopenSpawn: |
| 843 | + ... |
| 844 | + |
| 845 | + |
| 846 | +@pytest.fixture |
| 847 | +def copier(spawn: Spawn) -> CopierFixture: |
| 848 | + """Multiple choices are properly remembered and selected in TUI when updating.""" |
| 849 | + |
| 850 | + def fixture(*args, **kwargs) -> PopenSpawn: |
| 851 | + return spawn(COPIER_PATH + args, **kwargs) |
| 852 | + |
| 853 | + return fixture |
| 854 | + |
| 855 | + |
| 856 | +@pytest.mark.parametrize("type_name, choices, values", CHOICES) |
| 857 | +def test_multiselect_choices_question_single_answer( |
| 858 | + question_tree: QuestionTreeFixture, |
| 859 | + copier: CopierFixture, |
| 860 | + type_name: QuestionType, |
| 861 | + choices: QuestionChoices, |
| 862 | + values: ParsedValues, |
| 863 | +) -> None: |
| 864 | + src, dst = question_tree(type=type_name, choices=choices, multiselect=True) |
| 865 | + tui = copier("copy", str(src), str(dst), timeout=10) |
| 866 | + expect_prompt(tui, "question", type_name) |
| 867 | + tui.send(" ") # select 1 |
| 868 | + tui.sendline() |
| 869 | + tui.expect_exact(pexpect.EOF) |
| 870 | + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) |
| 871 | + assert answers["question"] == values[:1] |
| 872 | + |
| 873 | + |
| 874 | +@pytest.mark.parametrize("type_name, choices, values", CHOICES) |
| 875 | +def test_multiselect_choices_question_multiple_answers( |
| 876 | + question_tree: QuestionTreeFixture, |
| 877 | + copier: CopierFixture, |
| 878 | + type_name: QuestionType, |
| 879 | + choices: QuestionChoices, |
| 880 | + values: ParsedValues, |
| 881 | +) -> None: |
| 882 | + src, dst = question_tree(type=type_name, choices=choices, multiselect=True) |
| 883 | + tui = copier("copy", str(src), str(dst), timeout=10) |
| 884 | + expect_prompt(tui, "question", type_name) |
| 885 | + tui.send(" ") # select 0 |
| 886 | + tui.send(Keyboard.Down) |
| 887 | + tui.send(" ") # select 1 |
| 888 | + tui.sendline() |
| 889 | + tui.expect_exact(pexpect.EOF) |
| 890 | + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) |
| 891 | + assert answers["question"] == values[:2] |
| 892 | + |
| 893 | + |
| 894 | +@pytest.mark.parametrize("type_name, choices, values", CHOICES) |
| 895 | +def test_multiselect_choices_question_with_default( |
| 896 | + question_tree: QuestionTreeFixture, |
| 897 | + copier: CopierFixture, |
| 898 | + type_name: QuestionType, |
| 899 | + choices: QuestionChoices, |
| 900 | + values: ParsedValues, |
| 901 | +) -> None: |
| 902 | + src, dst = question_tree( |
| 903 | + type=type_name, choices=choices, multiselect=True, default=values |
| 904 | + ) |
| 905 | + tui = copier("copy", str(src), str(dst), timeout=10) |
| 906 | + expect_prompt(tui, "question", type_name) |
| 907 | + tui.send(" ") # toggle first |
| 908 | + tui.sendline() |
| 909 | + tui.expect_exact(pexpect.EOF) |
| 910 | + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) |
| 911 | + assert answers["question"] == values[1:] |
| 912 | + |
| 913 | + |
| 914 | +@pytest.mark.parametrize("type_name, choices, values", CHOICES) |
| 915 | +def test_update_multiselect_choices( |
| 916 | + question_tree: QuestionTreeFixture, |
| 917 | + copier: CopierFixture, |
| 918 | + type_name: QuestionType, |
| 919 | + choices: QuestionChoices, |
| 920 | + values: ParsedValues, |
| 921 | +) -> None: |
| 922 | + """Multiple choices are properly remembered and selected in TUI when updating.""" |
| 923 | + src, dst = question_tree( |
| 924 | + type=type_name, choices=choices, multiselect=True, default=values |
| 925 | + ) |
| 926 | + |
| 927 | + with local.cwd(src): |
| 928 | + git("init") |
| 929 | + git("add", ".") |
| 930 | + git("commit", "-m one") |
| 931 | + git("tag", "v1") |
| 932 | + |
| 933 | + # Copy |
| 934 | + tui = copier("copy", str(src), str(dst), timeout=10) |
| 935 | + expect_prompt(tui, "question", type_name) |
| 936 | + tui.send(" ") # toggle first |
| 937 | + tui.sendline() |
| 938 | + tui.expect_exact(pexpect.EOF) |
| 939 | + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) |
| 940 | + assert answers["question"] == values[1:] |
| 941 | + |
| 942 | + with local.cwd(dst): |
| 943 | + git("init") |
| 944 | + git("add", ".") |
| 945 | + git("commit", "-m1") |
| 946 | + |
| 947 | + # Update |
| 948 | + tui = copier("update", str(dst), timeout=10) |
| 949 | + expect_prompt(tui, "question", type_name) |
| 950 | + tui.send(" ") # toggle first |
| 951 | + tui.sendline() |
| 952 | + tui.expect_exact(pexpect.EOF) |
| 953 | + answers = yaml.safe_load((dst / ".copier-answers.yml").read_text()) |
| 954 | + assert answers["question"] == values |
0 commit comments