-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathtest_init.py
More file actions
317 lines (232 loc) · 9.14 KB
/
test_init.py
File metadata and controls
317 lines (232 loc) · 9.14 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from unittest.mock import ANY, Mock, PropertyMock, patch
import pytest
from rpdk.core.cli import main
from rpdk.core.exceptions import WizardAbortError, WizardValidationError
from rpdk.core.init import (
ValidatePluginChoice,
check_for_existing_project,
ignore_abort,
input_language,
input_typename,
input_with_validation,
validate_type_name,
validate_yes,
)
from rpdk.core.project import Project
from .utils import add_dummy_language_plugin, dummy_parser, get_args, get_mock_project
PROMPT = "MECVGD"
ERROR = "TUJFEL"
def test_init_method_interactive():
type_name = object()
language = object()
mock_project, patch_project = get_mock_project()
patch_tn = patch("rpdk.core.init.input_typename", return_value=type_name)
patch_l = patch("rpdk.core.init.input_language", return_value=language)
with patch_project, patch_tn as mock_tn, patch_l as mock_l:
main(args_in=["init"])
mock_tn.assert_called_once_with()
mock_l.assert_called_once_with()
mock_project.load_settings.assert_called_once_with()
mock_project.init.assert_called_once_with(
type_name,
language,
{
"version": False,
"subparser_name": None,
"verbose": 0,
"force": False,
"type_name": None,
},
)
mock_project.generate.assert_called_once_with()
def test_init_method_noninteractive():
add_dummy_language_plugin()
args = get_args("dummy", "Test::Test::Test")
mock_project, patch_project = get_mock_project()
patch_get_parser = patch(
"rpdk.core.init.get_parsers", return_value={"dummy": dummy_parser}
)
with patch_project, patch_get_parser as mock_parser:
main(args_in=["init", "--type-name", args.type_name, args.language, "--dummy"])
mock_parser.assert_called_once()
mock_project.load_settings.assert_called_once_with()
mock_project.init.assert_called_once_with(
args.type_name,
args.language,
{
"version": False,
"subparser_name": args.language,
"verbose": 0,
"force": False,
"type_name": args.type_name,
"language": args.language,
"dummy": True,
},
)
mock_project.generate.assert_called_once_with()
def test_init_method_noninteractive_invalid_type_name():
add_dummy_language_plugin()
type_name = object()
args = get_args("dummy", "invalid_type_name")
mock_project, patch_project = get_mock_project()
patch_tn = patch("rpdk.core.init.input_typename", return_value=type_name)
patch_get_parser = patch(
"rpdk.core.init.get_parsers", return_value={"dummy": dummy_parser}
)
with patch_project, patch_tn as mock_tn, patch_get_parser as mock_parser:
main(args_in=["init", "-t", args.type_name, args.language, "--dummy"])
mock_tn.assert_called_once_with()
mock_parser.assert_called_once()
mock_project.load_settings.assert_called_once_with()
mock_project.init.assert_called_once_with(
type_name,
args.language,
{
"version": False,
"subparser_name": args.language,
"verbose": 0,
"force": False,
"type_name": args.type_name,
"language": args.language,
"dummy": True,
},
)
mock_project.generate.assert_called_once_with()
def test_input_with_validation_valid_first_try(capsys):
sentinel1 = object()
sentinel2 = object()
validator = Mock(return_value=sentinel1)
with patch("rpdk.core.init.input", return_value=sentinel2) as mock_input:
ret = input_with_validation(PROMPT, validator)
mock_input.assert_called_once_with()
validator.assert_called_once_with(sentinel2)
assert ret is sentinel1
out, err = capsys.readouterr()
assert not err
assert PROMPT in out
def test_input_with_validation_valid_second_try(capsys):
def mock_validator(value):
if value == ERROR:
raise WizardValidationError(ERROR)
return value
sentinel = object()
with patch("rpdk.core.init.input", side_effect=(ERROR, sentinel)) as mock_input:
ret = input_with_validation(PROMPT, mock_validator)
assert mock_input.call_count == 2
assert ret is sentinel
out, err = capsys.readouterr()
assert not err
assert ERROR in out
def test_validate_type_name_valid():
assert validate_type_name("AWS::Color::Red") == "AWS::Color::Red"
def test_validate_type_name_invalid():
with pytest.raises(WizardValidationError):
validate_type_name("AWS-Color-Red")
@pytest.mark.parametrize("value", ("y", "yes", "Y", "YES", "yEs", "YeS"))
def test_validate_yes_yes(value):
assert validate_yes(value)
@pytest.mark.parametrize("value", ("n", "N", "no", "NO", "yesn't"))
def test_validate_yes_no(value):
assert not validate_yes(value)
def test_validate_plugin_choice_not_an_int():
validator = ValidatePluginChoice(["test"])
with pytest.raises(WizardValidationError) as excinfo:
validator("a")
assert "integer" in str(excinfo.value)
def test_validate_plugin_choice_less_than_zero():
validator = ValidatePluginChoice(["test"])
with pytest.raises(WizardValidationError) as excinfo:
validator("-1")
assert "select" in str(excinfo.value)
def test_validate_plugin_choice_greater_than_choice():
choices = range(3)
validator = ValidatePluginChoice(choices)
with pytest.raises(WizardValidationError) as excinfo:
validator(str(len(choices) + 1)) # index is 1 based for input
assert "select" in str(excinfo.value)
def test_validate_plugin_choice_valid():
choices = ["1", PROMPT, "2"]
validator = ValidatePluginChoice(choices)
assert validator("2") == PROMPT
def test_check_for_existing_project_good_path():
project = Mock(spec=Project)
project.load_settings.side_effect = FileNotFoundError
type(project).overwrite_enabled = mock_overwrite = PropertyMock()
check_for_existing_project(project)
project.load_settings.assert_called_once_with()
mock_overwrite.assert_not_called()
def test_check_for_existing_project_bad_path_overwrite():
project = Mock(spec=Project)
project.overwrite_enabled = True
project.settings_path = "" # not sure why this doesn't get specced?
project.type_name = ""
with patch("rpdk.core.init.input_with_validation", autospec=True) as mock_input:
check_for_existing_project(project)
mock_input.assert_not_called()
def test_check_for_existing_project_bad_path_ask_yes():
project = Mock(spec=Project)
project.overwrite_enabled = False
project.settings_path = ""
project.type_name = ""
patch_input = patch(
"rpdk.core.init.input_with_validation", autospec=True, return_value=True
)
with patch_input as mock_input:
check_for_existing_project(project)
mock_input.assert_called_once_with(ANY, validate_yes, ANY)
assert project.overwrite_enabled
def test_check_for_existing_project_bad_path_ask_no():
project = Mock(spec=Project)
project.overwrite_enabled = False
project.settings_path = ""
project.type_name = ""
patch_input = patch(
"rpdk.core.init.input_with_validation", autospec=True, return_value=False
)
with patch_input as mock_input:
with pytest.raises(WizardAbortError):
check_for_existing_project(project)
mock_input.assert_called_once_with(ANY, validate_yes, ANY)
assert not project.overwrite_enabled
def test_ignore_abort_ok():
sentinel = object()
function = Mock()
wrapped = ignore_abort(function)
wrapped(sentinel)
function.assert_called_once_with(sentinel)
def test_ignore_abort_keyboard_interrupt():
sentinel = object()
function = Mock(side_effect=KeyboardInterrupt)
wrapped = ignore_abort(function)
with pytest.raises(SystemExit):
wrapped(sentinel)
function.assert_called_once_with(sentinel)
def test_ignore_abort_abort():
sentinel = object()
function = Mock(side_effect=WizardAbortError)
wrapped = ignore_abort(function)
with pytest.raises(SystemExit):
wrapped(sentinel)
function.assert_called_once_with(sentinel)
def test_input_typename():
type_name = "AWS::Color::Red"
patch_input = patch("rpdk.core.init.input", return_value=type_name)
with patch_input as mock_input:
assert input_typename() == type_name
mock_input.assert_called_once()
def test_input_language_no_plugins():
validator = ValidatePluginChoice([])
with patch("rpdk.core.init.validate_plugin_choice", validator):
with pytest.raises(WizardAbortError):
input_language()
def test_input_language_one_plugin():
validator = ValidatePluginChoice([PROMPT])
with patch("rpdk.core.init.validate_plugin_choice", validator):
assert input_language() == PROMPT
def test_input_language_several_plugins():
validator = ValidatePluginChoice(["1", PROMPT, "2"])
patch_validator = patch("rpdk.core.init.validate_plugin_choice", validator)
patch_input = patch("rpdk.core.init.input", return_value="2")
with patch_validator, patch_input as mock_input:
assert input_language() == PROMPT
mock_input.assert_called_once()