mirror of
https://github.com/copier-org/copier.git
synced 2025-05-05 23:42:55 +00:00
When data came from `--data` flag, it was always a string. If some data was expected to be of another type, it could make the template fail. The type casting system must be applied also to user answers, so here's the patch, docs and tests. @Tecnativa TT20357
177 lines
4.7 KiB
Python
177 lines
4.7 KiB
Python
from pathlib import Path
|
|
from textwrap import dedent
|
|
|
|
from plumbum.cmd import copier as copier_cmd
|
|
|
|
from copier import copy
|
|
|
|
from .helpers import PROJECT_TEMPLATE
|
|
|
|
SRC = f"{PROJECT_TEMPLATE}_complex_questions"
|
|
|
|
|
|
def test_api(dst):
|
|
"""Test copier correctly processes advanced questions and answers through API."""
|
|
copy(
|
|
SRC,
|
|
dst,
|
|
{
|
|
"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 = dst / "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
|
|
optional_value: null
|
|
"""
|
|
)
|
|
|
|
|
|
def test_cli(dst):
|
|
"""Test copier correctly processes advanced questions and answers through CLI."""
|
|
(
|
|
copier_cmd["copy", SRC, dst]
|
|
<< dedent(
|
|
# These are the answers; those marked as "wrong" will fail and
|
|
# make the prompt function ask again
|
|
"""
|
|
y
|
|
Guybrush Threpwood
|
|
wrong your_age
|
|
22
|
|
wrong your_height
|
|
1.56
|
|
wrong more_json_info
|
|
{"objective": "be a pirate"}
|
|
{wrong anything_else
|
|
['Want some grog?', I'd love it]
|
|
2
|
|
3
|
|
1
|
|
1
|
|
|
|
"""
|
|
)
|
|
)()
|
|
results_file = dst / "results.txt"
|
|
assert results_file.read_text() == dedent(
|
|
r"""
|
|
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.1
|
|
optional_value: null
|
|
"""
|
|
)
|
|
|
|
|
|
def test_api_str_data(dst):
|
|
"""Test copier when all data comes as a string.
|
|
|
|
This happens i.e. when using the --data CLI argument.
|
|
"""
|
|
copy(
|
|
SRC,
|
|
dst,
|
|
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 = dst / "results.txt"
|
|
assert results_file.read_text() == dedent(
|
|
r"""
|
|
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: 0.0
|
|
optional_value: null
|
|
"""
|
|
)
|
|
|
|
|
|
def test_cli_with_flag_data_and_type_casts(tmp_path: Path):
|
|
"""Assert how choices work when copier is invoked with --data interactively."""
|
|
(
|
|
copier_cmd[
|
|
"--data=choose_list=second",
|
|
"--data=choose_dict=first",
|
|
"--data=choose_tuple=third",
|
|
"--data=choose_number=1",
|
|
"copy",
|
|
SRC,
|
|
tmp_path,
|
|
]
|
|
<< dedent(
|
|
# These are the answers; those marked as "wrong" will fail and
|
|
# make the prompt function ask again
|
|
"""
|
|
y
|
|
Guybrush Threpwood
|
|
wrong your_age
|
|
22
|
|
wrong your_height
|
|
1.56
|
|
wrong more_json_info
|
|
{"objective": "be a pirate"}
|
|
{wrong anything_else
|
|
['Want some grog?', I'd love it]
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
)
|
|
)()
|
|
results_file = tmp_path / "results.txt"
|
|
assert results_file.read_text() == dedent(
|
|
r"""
|
|
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.0
|
|
optional_value: null
|
|
"""
|
|
)
|