-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
166 lines (135 loc) · 4.26 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from textwrap import dedent
import click
import pytest
from click.testing import CliRunner
from cock import Option, build_entrypoint, build_options_from_dict, get_options_defaults
@pytest.fixture(scope="function")
def runner(request):
return CliRunner()
options = [
click.option("--a-b-c", default="abc-default"),
click.option("--b-c-d", default=666, type=int),
click.option("--c-d-e", multiple=True),
]
def test_defaults(runner):
def main(config):
assert config.a_b_c == "abc-default"
assert config.b_c_d == 666
assert config.c_d_e == ()
assert config.configuration_file is None
assert len(config) == 4
ep = build_entrypoint(main, options)
runner.invoke(ep, [], catch_exceptions=False)
def test_config(runner, tmp_path):
def main(config):
assert config.a_b_c == "abc-config"
assert config.b_c_d == 667
assert config.c_d_e == ("a", "b")
assert config.configuration_file == str(config_path)
config_path = tmp_path / "config.yml"
ep = build_entrypoint(main, options)
config_path.write_text(dedent("""\
a:
b:
c: abc-config
b-c:
d: "667"
c_d-e:
- a
- b
"""))
runner.invoke(ep, [str(config_path)], catch_exceptions=False)
def test_config_duplicate(runner, tmp_path):
def main(config):
assert config.a_b_c == "abc-config"
assert config.b_c_d == 667
config_path = tmp_path / "config.yml"
ep = build_entrypoint(main, options)
config_path.write_text(dedent("""\
a:
b:
c: abc-config
a-b_c: fail
"""))
with pytest.raises(RuntimeError):
runner.invoke(ep, [str(config_path)], catch_exceptions=False)
def test_dictionary_configuration(runner):
def main(config):
assert config.a_b_c == "abc_default"
dict_options = {
"a": {
"b": {
"c": Option(default="abc-default"),
},
},
}
ep = build_entrypoint(main, build_options_from_dict(dict_options))
runner.invoke(ep)
def test_dictionary_configuration_fail(runner):
def main(config):
assert config.a_b_c == "abc_default"
dict_options = {
"a": {
"b": {
"c": "fail",
},
},
}
with pytest.raises(TypeError):
build_entrypoint(main, build_options_from_dict(dict_options))
def test_dictionary_defaults():
dict_options = {
"b": {
"c": Option(),
},
"a": {
"b": {
"c": Option(default="abc-default"),
},
},
}
config = get_options_defaults(dict_options)
assert config == {
"a_b_c": "abc-default",
}
def test_dictionary_defaults_fail():
dict_options = {
"a_b_c": Option(default="foo"),
"a": {
"b": {
"c": Option(default="abc-default"),
},
},
}
with pytest.raises(RuntimeError):
get_options_defaults(dict_options)
def test_required_argument():
with pytest.raises(ValueError):
Option(required=True)
def test_mix_options(runner):
def main(config):
assert config.a_b_c == "foo"
assert config.a_b_d == "bar"
assert config.a_b_e == "baz"
dict_options = {"a_b_c": Option(default="foo")}
list_options = [Option("a_b_d", default="bar"),
click.option("--a-b-e", default="baz")]
ep = build_entrypoint(main, dict_options, list_options)
runner.invoke(ep)
def test_get_defaults_mix():
dict_options = {"a_b-c": Option(default="foo")}
list_options = [Option("a-b_d", default="bar")]
config = get_options_defaults(dict_options, list_options)
assert config.a_b_c == "foo"
assert config.a_b_d == "bar"
def test_option_corner_cases():
list_options = [Option(default="bar")]
with pytest.raises(RuntimeError):
build_entrypoint(None, list_options)
dict_options = {"a_b_c": Option("b_c_d", default="foo")}
with pytest.raises(RuntimeError):
build_entrypoint(None, dict_options)
def test_get_defaults_fail_on_click_option():
list_options = [click.option("--foo", default="bar")]
with pytest.raises(TypeError):
get_options_defaults(list_options)