copier/tests/test_complex_questions.py
Jairo Llopis 0441b86f0c
Refactor (#314)
* Refactor
* Fix #110.
* Rewrite test_config_exclude, test_config_exclude_overridden and test_config_include. These tests were badly designed, using a monkeypatch that would never happen in the real world, and actually producing false positives. I moved them to test_exclude.py and rewritten to test the what and not the how.
* Fix #214 by removing skip option. Relevant tests use the better skip_if_exists=["**"].
* Remove subdirectory flag from API/CLI. It was confusing and could lead to bad maintenance situations. Fixes #315.
* Remove extra_paths and fix #321
* Remember that you cannot use _copier_conf.src_path as a path now
* use dataclasses
* Create errors module, simplify some tests, fix many others
* Fix some tests, complete EnvOps removal
* Fix #214 and some tests related to it
* Reorder code
* Update docs and imports
* Modularize test_complex_questions
* Interlink worker and questionary a bit better
* Removal of Questionary class, which only had 1 meaningful method that is now merged into Worker to avoid circular dependencies.
* Fix #280 in a simple way: only user answers are type-casted inside API, and CLI transforms all `--data` using YAML always. Predictable.
* Use prereleases correctly.
* Reorder AnswersMap to have a more significative repr.
* Simpler cache for old `Question.get_choices()` method (renamed now).
* fix wrong test
* Fix test_subdirectory
* Fix test_tasks (and remove tests/demo_tasks)
* Fix path filter tests, and move it to test_exclude, where it belongs
* Make test_config pass
* Fix more wrongly designed tests
* Use cached_property backport if needed
* xfail test known to fail on mac
* force posix paths on windows
* Add typing_extensions for python < 3.8
* Sort dependencies in pyproject.toml
* Support python 3.6 str-to-datetime conversion
* Workaround https://bugs.python.org/issue43095
* xfail test_path_filter on windows
* Upgrade mkdocs and other dependencies to fix https://github.com/pawamoy/mkdocstrings/issues/222
* Add missing reference docs.
* Add workaround for https://github.com/pawamoy/mkdocstrings/pull/209
* Docs.
* Remove validators module
* Add workaround for https://github.com/pawamoy/mkdocstrings/issues/225
* Restore docs autorefs as explained in https://github.com/pawamoy/mkdocstrings/issues/226#issuecomment-775413562.
* Workaround https://github.com/pawamoy/pytkdocs/issues/86
2021-02-09 18:22:47 +00:00

329 lines
10 KiB
Python

from pathlib import Path
from textwrap import dedent
import pexpect
import pytest
from copier import copy
from .helpers import COPIER_PATH, Keyboard, build_file_tree
@pytest.fixture(scope="module")
def template_path(tmp_path_factory) -> str:
root = tmp_path_factory.mktemp("template")
build_file_tree(
{
root
/ "copier.yml": """\
love_me:
help: I need to know it. Do you love me?
type: bool
your_name:
help: Please tell me your name.
type: str
your_age:
help: How old are you?
type: int
your_height:
help: What's your height?
type: float
more_json_info:
multiline: true
type: json
anything_else:
multiline: true
help: Wanna give me any more info?
# In choices, the user will always write the choice index (1, 2, 3...)
choose_list:
help: You see the value of the list items
default: first
choices:
- first
- second
- third
choose_tuple:
help: You see the 1st tuple item, but I get the 2nd item as a result
default: second
choices:
- [one, first]
- [two, second]
- [three, third]
choose_dict:
help: You see the dict key, but I get the dict value
default: third
choices:
one: first
two: second
three: third
choose_number:
help: This must be a number
default: null
type: float
choices:
- -1.1
- 0
- 1
# Simplified format is still supported
minutes_under_water: 10
optional_value: null
""",
root
/ "results.txt.tmpl": """\
love_me: [[love_me|tojson]]
your_name: [[your_name|tojson]]
your_age: [[your_age|tojson]]
your_height: [[your_height|tojson]]
more_json_info: [[more_json_info|tojson]]
anything_else: [[anything_else|tojson]]
choose_list: [[choose_list|tojson]]
choose_tuple: [[choose_tuple|tojson]]
choose_dict: [[choose_dict|tojson]]
choose_number: [[choose_number|tojson]]
minutes_under_water: [[minutes_under_water|tojson]]
optional_value: [[optional_value|tojson]]
""",
}
)
return str(root)
def test_api(tmp_path, template_path):
"""Test copier correctly processes advanced questions and answers through API."""
copy(
template_path,
tmp_path,
{
"love_me": False,
"your_name": "LeChuck",
"your_age": 220,
"your_height": 1.9,
"more_json_info": ["bad", "guy"],
"anything_else": {"hates": "all"},
},
force=True,
)
results_file = tmp_path / "results.txt"
assert results_file.read_text() == dedent(
"""\
love_me: false
your_name: "LeChuck"
your_age: 220
your_height: 1.9
more_json_info: ["bad", "guy"]
anything_else: {"hates": "all"}
choose_list: "first"
choose_tuple: "second"
choose_dict: "third"
choose_number: null
minutes_under_water: 10
optional_value: null
"""
)
def test_cli_interactive(tmp_path, spawn, template_path):
"""Test copier correctly processes advanced questions and answers through CLI."""
invalid = [
"Invalid value",
"please try again",
]
tui = spawn([COPIER_PATH, "copy", template_path, str(tmp_path)], timeout=10)
tui.expect_exact(["I need to know it. Do you love me?", "love_me", "Format: bool"])
tui.send("y")
tui.expect_exact(["Please tell me your name.", "your_name", "Format: str"])
tui.sendline("Guybrush Threpwood")
q = ["How old are you?", "your_age", "Format: int"]
tui.expect_exact(q)
tui.sendline("wrong your_age")
tui.expect_exact(invalid + q)
tui.send((Keyboard.Alt + Keyboard.Backspace) * 2)
tui.sendline("22")
q = ["What's your height?", "your_height", "Format: float"]
tui.expect_exact(q)
tui.sendline("wrong your_height")
tui.expect_exact(invalid + q)
tui.send((Keyboard.Alt + Keyboard.Backspace) * 2)
tui.sendline("1.56")
q = ["more_json_info", "Format: json"]
tui.expect_exact(q)
tui.sendline('{"objective":')
tui.expect_exact(invalid + q)
tui.sendline('"be a pirate"}')
tui.send(Keyboard.Esc + Keyboard.Enter)
q = ["Wanna give me any more info?", "anything_else", "Format: yaml"]
tui.expect_exact(q)
tui.sendline("- Want some grog?")
tui.expect_exact(invalid + q)
tui.sendline("- I'd love it")
tui.send(Keyboard.Esc + Keyboard.Enter)
tui.expect_exact(
[
"You see the value of the list items",
"choose_list",
"Format: str",
"first",
"second",
"third",
]
)
tui.sendline()
tui.expect_exact(
[
"You see the 1st tuple item, but I get the 2nd item as a result",
"choose_tuple",
"one",
"two",
"three",
]
)
tui.sendline()
tui.expect_exact(
[
"You see the dict key, but I get the dict value",
"choose_dict",
"one",
"two",
"three",
]
)
tui.sendline()
tui.expect_exact(["This must be a number", "choose_number", "-1.1", "0", "1"])
tui.sendline()
tui.expect_exact(["minutes_under_water", "Format: int", "10"])
tui.send(Keyboard.Backspace)
tui.sendline()
tui.expect_exact(["optional_value", "Format: yaml"])
tui.sendline()
tui.expect_exact(pexpect.EOF)
results_file = tmp_path / "results.txt"
assert results_file.read_text() == dedent(
"""\
love_me: true
your_name: "Guybrush Threpwood"
your_age: 22
your_height: 1.56
more_json_info: {"objective": "be a pirate"}
anything_else: ["Want some grog?", "I\\u0027d love it"]
choose_list: "first"
choose_tuple: "second"
choose_dict: "third"
choose_number: -1.1
minutes_under_water: 1
optional_value: null
"""
)
def test_api_str_data(tmp_path, template_path):
"""Test copier when all data comes as a string.
This happens i.e. when using the --data CLI argument.
"""
copy(
template_path,
tmp_path,
data={
"love_me": "false",
"your_name": "LeChuck",
"your_age": "220",
"your_height": "1.9",
"more_json_info": '["bad", "guy"]',
"anything_else": "{'hates': 'all'}",
"choose_number": "0",
},
force=True,
)
results_file = tmp_path / "results.txt"
assert results_file.read_text() == dedent(
"""\
love_me: "false"
your_name: "LeChuck"
your_age: "220"
your_height: "1.9"
more_json_info: "[\\"bad\\", \\"guy\\"]"
anything_else: "{\\u0027hates\\u0027: \\u0027all\\u0027}"
choose_list: "first"
choose_tuple: "second"
choose_dict: "third"
choose_number: "0"
minutes_under_water: 10
optional_value: null
"""
)
def test_cli_interatively_with_flag_data_and_type_casts(
tmp_path: Path, spawn, template_path
):
"""Assert how choices work when copier is invoked with --data interactively."""
invalid = [
"Invalid value",
"please try again",
]
tui = spawn(
[
COPIER_PATH,
"--data=choose_list=second",
"--data=choose_dict=first",
"--data=choose_tuple=third",
"--data=choose_number=1",
"copy",
template_path,
str(tmp_path),
],
timeout=10,
)
tui.expect_exact(["I need to know it. Do you love me?", "love_me", "Format: bool"])
tui.send("y")
tui.expect_exact(["Please tell me your name.", "your_name", "Format: str"])
tui.sendline("Guybrush Threpwood")
q = ["How old are you?", "your_age", "Format: int"]
tui.expect_exact(q)
tui.sendline("wrong your_age")
tui.expect_exact(invalid + q)
tui.send((Keyboard.Alt + Keyboard.Backspace) * 2)
tui.sendline("22")
q = ["What's your height?", "your_height", "Format: float"]
tui.expect_exact(q)
tui.sendline("wrong your_height")
tui.expect_exact(invalid + q)
tui.send((Keyboard.Alt + Keyboard.Backspace) * 2)
tui.sendline("1.56")
q = ["more_json_info", "Format: json"]
tui.expect_exact(q)
tui.sendline('{"objective":')
tui.expect_exact(invalid + q)
tui.sendline('"be a pirate"}')
tui.send(Keyboard.Esc + Keyboard.Enter)
q = ["Wanna give me any more info?", "anything_else", "Format: yaml"]
tui.expect_exact(q)
tui.sendline("- Want some grog?")
tui.expect_exact(invalid + q)
tui.sendline("- I'd love it")
tui.send(Keyboard.Esc + Keyboard.Enter)
tui.expect_exact(["minutes_under_water", "Format: int", "10"])
tui.sendline()
tui.expect_exact(["optional_value", "Format: yaml"])
tui.sendline()
tui.expect_exact(pexpect.EOF)
results_file = tmp_path / "results.txt"
assert results_file.read_text() == dedent(
"""\
love_me: true
your_name: "Guybrush Threpwood"
your_age: 22
your_height: 1.56
more_json_info: {"objective": "be a pirate"}
anything_else: ["Want some grog?", "I\\u0027d love it"]
choose_list: "second"
choose_tuple: "third"
choose_dict: "first"
choose_number: 1
minutes_under_water: 10
optional_value: null
"""
)