-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathtest_argparse_custom.py
337 lines (253 loc) · 10.5 KB
/
test_argparse_custom.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# flake8: noqa E302
"""
Unit/functional testing for argparse customizations in cmd2
"""
import argparse
import pytest
import cmd2
from cmd2 import (
Cmd2ArgumentParser,
constants,
)
from cmd2.argparse_custom import (
generate_range_error,
)
from .conftest import (
run_cmd,
)
class ApCustomTestApp(cmd2.Cmd):
"""Test app for cmd2's argparse customization"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
range_parser = Cmd2ArgumentParser()
range_parser.add_argument('--arg0', nargs=1)
range_parser.add_argument('--arg1', nargs=2)
range_parser.add_argument('--arg2', nargs=(3,))
range_parser.add_argument('--arg3', nargs=(2, 3))
range_parser.add_argument('--arg4', nargs=argparse.ZERO_OR_MORE)
range_parser.add_argument('--arg5', nargs=argparse.ONE_OR_MORE)
@cmd2.with_argparser(range_parser)
def do_range(self, _):
pass
@pytest.fixture
def cust_app():
return ApCustomTestApp()
def fake_func():
pass
@pytest.mark.parametrize(
'kwargs, is_valid',
[
({'choices_provider': fake_func}, True),
({'completer': fake_func}, True),
({'choices_provider': fake_func, 'completer': fake_func}, False),
],
)
def test_apcustom_choices_callable_count(kwargs, is_valid):
parser = Cmd2ArgumentParser()
try:
parser.add_argument('name', **kwargs)
assert is_valid
except ValueError as ex:
assert not is_valid
assert 'Only one of the following parameters' in str(ex)
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
def test_apcustom_no_choices_callables_alongside_choices(kwargs):
with pytest.raises(TypeError) as excinfo:
parser = Cmd2ArgumentParser()
parser.add_argument('name', choices=['my', 'choices', 'list'], **kwargs)
assert 'None of the following parameters can be used alongside a choices parameter' in str(excinfo.value)
@pytest.mark.parametrize('kwargs', [({'choices_provider': fake_func}), ({'completer': fake_func})])
def test_apcustom_no_choices_callables_when_nargs_is_0(kwargs):
with pytest.raises(TypeError) as excinfo:
parser = Cmd2ArgumentParser()
parser.add_argument('name', action='store_true', **kwargs)
assert 'None of the following parameters can be used on an action that takes no arguments' in str(excinfo.value)
def test_apcustom_usage():
usage = "A custom usage statement"
parser = Cmd2ArgumentParser(usage=usage)
assert usage in parser.format_help()
def test_apcustom_nargs_help_format(cust_app):
out, err = run_cmd(cust_app, 'help range')
assert 'Usage: range [-h] [--arg0 ARG0] [--arg1 ARG1{2}] [--arg2 ARG2{3+}]' in out[0]
assert ' [--arg3 ARG3{2..3}] [--arg4 [ARG4 [...]]] [--arg5 ARG5 [...]]' in out[1]
def test_apcustom_nargs_range_validation(cust_app):
# nargs = (3,)
out, err = run_cmd(cust_app, 'range --arg2 one two')
assert 'Error: argument --arg2: expected at least 3 arguments' in err[2]
out, err = run_cmd(cust_app, 'range --arg2 one two three')
assert not err
out, err = run_cmd(cust_app, 'range --arg2 one two three four')
assert not err
# nargs = (2,3)
out, err = run_cmd(cust_app, 'range --arg3 one')
assert 'Error: argument --arg3: expected 2 to 3 arguments' in err[2]
out, err = run_cmd(cust_app, 'range --arg3 one two')
assert not err
out, err = run_cmd(cust_app, 'range --arg2 one two three')
assert not err
@pytest.mark.parametrize(
'nargs_tuple',
[
(),
('f', 5),
(5, 'f'),
(1, 2, 3),
],
)
def test_apcustom_narg_invalid_tuples(nargs_tuple):
with pytest.raises(ValueError) as excinfo:
parser = Cmd2ArgumentParser()
parser.add_argument('invalid_tuple', nargs=nargs_tuple)
assert 'Ranged values for nargs must be a tuple of 1 or 2 integers' in str(excinfo.value)
def test_apcustom_narg_tuple_order():
with pytest.raises(ValueError) as excinfo:
parser = Cmd2ArgumentParser()
parser.add_argument('invalid_tuple', nargs=(2, 1))
assert 'Invalid nargs range. The first value must be less than the second' in str(excinfo.value)
def test_apcustom_narg_tuple_negative():
with pytest.raises(ValueError) as excinfo:
parser = Cmd2ArgumentParser()
parser.add_argument('invalid_tuple', nargs=(-1, 1))
assert 'Negative numbers are invalid for nargs range' in str(excinfo.value)
def test_apcustom_narg_tuple_zero_base():
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0,))
assert arg.nargs == argparse.ZERO_OR_MORE
assert arg.nargs_range is None
assert "[arg [...]]" in parser.format_help()
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0, 1))
assert arg.nargs == argparse.OPTIONAL
assert arg.nargs_range is None
assert "[arg]" in parser.format_help()
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(0, 3))
assert arg.nargs == argparse.ZERO_OR_MORE
assert arg.nargs_range == (0, 3)
assert "arg{0..3}" in parser.format_help()
def test_apcustom_narg_tuple_one_base():
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(1,))
assert arg.nargs == argparse.ONE_OR_MORE
assert arg.nargs_range is None
assert "arg [...]" in parser.format_help()
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(1, 5))
assert arg.nargs == argparse.ONE_OR_MORE
assert arg.nargs_range == (1, 5)
assert "arg{1..5}" in parser.format_help()
def test_apcustom_narg_tuple_other_ranges():
# Test range with no upper bound on max
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(2,))
assert arg.nargs == argparse.ONE_OR_MORE
assert arg.nargs_range == (2, constants.INFINITY)
# Test finite range
parser = Cmd2ArgumentParser()
arg = parser.add_argument('arg', nargs=(2, 5))
assert arg.nargs == argparse.ONE_OR_MORE
assert arg.nargs_range == (2, 5)
def test_apcustom_print_message(capsys):
import sys
test_message = 'The test message'
# Specify the file
parser = Cmd2ArgumentParser()
parser._print_message(test_message, file=sys.stdout)
out, err = capsys.readouterr()
assert test_message in out
# Make sure file defaults to sys.stderr
parser = Cmd2ArgumentParser()
parser._print_message(test_message)
out, err = capsys.readouterr()
assert test_message in err
def test_generate_range_error():
# max is INFINITY
err_str = generate_range_error(1, constants.INFINITY)
assert err_str == "expected at least 1 argument"
err_str = generate_range_error(2, constants.INFINITY)
assert err_str == "expected at least 2 arguments"
# min and max are equal
err_str = generate_range_error(1, 1)
assert err_str == "expected 1 argument"
err_str = generate_range_error(2, 2)
assert err_str == "expected 2 arguments"
# min and max are not equal
err_str = generate_range_error(0, 1)
assert err_str == "expected 0 to 1 argument"
err_str = generate_range_error(0, 2)
assert err_str == "expected 0 to 2 arguments"
def test_apcustom_required_options():
# Make sure a 'required arguments' section shows when a flag is marked required
parser = Cmd2ArgumentParser()
parser.add_argument('--required_flag', required=True)
assert 'required arguments' in parser.format_help()
def test_override_parser():
"""Test overriding argparse_custom.DEFAULT_ARGUMENT_PARSER"""
import importlib
from cmd2 import (
argparse_custom,
)
# The standard parser is Cmd2ArgumentParser
assert argparse_custom.DEFAULT_ARGUMENT_PARSER == Cmd2ArgumentParser
# Set our parser module and force a reload of cmd2 so it loads the module
argparse.cmd2_parser_module = 'examples.custom_parser'
importlib.reload(cmd2)
# Verify DEFAULT_ARGUMENT_PARSER is now our CustomParser
from examples.custom_parser import (
CustomParser,
)
assert argparse_custom.DEFAULT_ARGUMENT_PARSER == CustomParser
def test_apcustom_metavar_tuple():
# Test the case when a tuple metavar is used with nargs an integer > 1
parser = Cmd2ArgumentParser()
parser.add_argument('--aflag', nargs=2, metavar=('foo', 'bar'), help='This is a test')
assert '[--aflag foo bar]' in parser.format_help()
def test_cmd2_attribute_wrapper():
initial_val = 5
wrapper = cmd2.Cmd2AttributeWrapper(initial_val)
assert wrapper.get() == initial_val
new_val = 22
wrapper.set(new_val)
assert wrapper.get() == new_val
def test_completion_items_as_choices(capsys):
"""
Test cmd2's patch to Argparse._check_value() which supports CompletionItems as choices.
Choices are compared to CompletionItems.orig_value instead of the CompletionItem instance.
"""
from cmd2.argparse_custom import (
CompletionItem,
)
##############################################################
# Test CompletionItems with str values
##############################################################
choices = [CompletionItem("1", "Description One"), CompletionItem("2", "Two")]
parser = Cmd2ArgumentParser()
parser.add_argument("choices_arg", type=str, choices=choices)
# First test valid choices. Confirm the parsed data matches the correct type of str.
args = parser.parse_args(['1'])
assert args.choices_arg == '1'
args = parser.parse_args(['2'])
assert args.choices_arg == '2'
# Next test invalid choice
with pytest.raises(SystemExit):
args = parser.parse_args(['3'])
# Confirm error text contains correct value type of str
out, err = capsys.readouterr()
assert "invalid choice: '3' (choose from '1', '2')" in err
##############################################################
# Test CompletionItems with int values
##############################################################
choices = [CompletionItem(1, "Description One"), CompletionItem(2, "Two")]
parser = Cmd2ArgumentParser()
parser.add_argument("choices_arg", type=int, choices=choices)
# First test valid choices. Confirm the parsed data matches the correct type of int.
args = parser.parse_args(['1'])
assert args.choices_arg == 1
args = parser.parse_args(['2'])
assert args.choices_arg == 2
# Next test invalid choice
with pytest.raises(SystemExit):
args = parser.parse_args(['3'])
# Confirm error text contains correct value type of int
out, err = capsys.readouterr()
assert 'invalid choice: 3 (choose from 1, 2)' in err