@@ -92,6 +92,7 @@ def do_preservelist(self, arglist):
92
92
known_parser .add_argument ('-p' , '--piglatin' , action = 'store_true' , help = 'atinLay' )
93
93
known_parser .add_argument ('-s' , '--shout' , action = 'store_true' , help = 'N00B EMULATION MODE' )
94
94
known_parser .add_argument ('-r' , '--repeat' , type = int , help = 'output [n] times' )
95
+
95
96
@cmd2 .with_argparser (known_parser , with_unknown_args = True )
96
97
def do_speak (self , args , extra , * , keyword_arg : Optional [str ] = None ):
97
98
"""Repeat what you tell me to."""
@@ -131,89 +132,108 @@ def test_invalid_syntax(argparse_app):
131
132
out , err = run_cmd (argparse_app , 'speak "' )
132
133
assert err [0 ] == "Invalid syntax: No closing quotation"
133
134
135
+
134
136
def test_argparse_basic_command (argparse_app ):
135
137
out , err = run_cmd (argparse_app , 'say hello' )
136
138
assert out == ['hello' ]
137
139
140
+
138
141
def test_argparse_remove_quotes (argparse_app ):
139
142
out , err = run_cmd (argparse_app , 'say "hello there"' )
140
143
assert out == ['hello there' ]
141
144
145
+
142
146
def test_argparser_kwargs (argparse_app , capsys ):
143
147
"""Test with_argparser wrapper passes through kwargs to command function"""
144
148
argparse_app .do_say ('word' , keyword_arg = "foo" )
145
149
out , err = capsys .readouterr ()
146
150
assert out == "foo\n "
147
151
152
+
148
153
def test_argparse_preserve_quotes (argparse_app ):
149
154
out , err = run_cmd (argparse_app , 'tag mytag "hello"' )
150
155
assert out [0 ] == '<mytag>"hello"</mytag>'
151
156
157
+
152
158
def test_argparse_custom_namespace (argparse_app ):
153
159
out , err = run_cmd (argparse_app , 'test_argparse_ns' )
154
160
assert out [0 ] == 'custom'
155
161
162
+
156
163
def test_argparse_with_list (argparse_app ):
157
164
out , err = run_cmd (argparse_app , 'speak -s hello world!' )
158
165
assert out == ['HELLO WORLD!' ]
159
166
167
+
160
168
def test_argparse_with_list_remove_quotes (argparse_app ):
161
169
out , err = run_cmd (argparse_app , 'speak -s hello "world!"' )
162
170
assert out == ['HELLO WORLD!' ]
163
171
172
+
164
173
def test_argparse_with_list_preserve_quotes (argparse_app ):
165
174
out , err = run_cmd (argparse_app , 'test_argparse_with_list_quotes "hello" person' )
166
175
assert out [0 ] == '"hello" person'
167
176
177
+
168
178
def test_argparse_with_list_custom_namespace (argparse_app ):
169
179
out , err = run_cmd (argparse_app , 'test_argparse_with_list_ns' )
170
180
assert out [0 ] == 'custom'
171
181
182
+
172
183
def test_argparse_with_list_and_empty_doc (argparse_app ):
173
184
out , err = run_cmd (argparse_app , 'speak -s hello world!' )
174
185
assert out == ['HELLO WORLD!' ]
175
186
187
+
176
188
def test_argparser_correct_args_with_quotes_and_midline_options (argparse_app ):
177
189
out , err = run_cmd (argparse_app , "speak 'This is a' -s test of the emergency broadcast system!" )
178
190
assert out == ['THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!' ]
179
191
192
+
180
193
def test_argparser_and_unknown_args_kwargs (argparse_app , capsys ):
181
194
"""Test with_argparser_and_unknown_args wrapper passes through kwargs to command function"""
182
195
argparse_app .do_speak ('' , keyword_arg = "foo" )
183
196
out , err = capsys .readouterr ()
184
197
assert out == "foo\n "
185
198
199
+
186
200
def test_argparse_quoted_arguments_multiple (argparse_app ):
187
201
out , err = run_cmd (argparse_app , 'say "hello there" "rick & morty"' )
188
202
assert out == ['hello there rick & morty' ]
189
203
204
+
190
205
def test_argparse_help_docstring (argparse_app ):
191
206
out , err = run_cmd (argparse_app , 'help say' )
192
207
assert out [0 ].startswith ('usage: say' )
193
208
assert out [1 ] == ''
194
209
assert out [2 ] == 'Repeat what you tell me to.'
195
210
211
+
196
212
def test_argparse_help_description (argparse_app ):
197
213
out , err = run_cmd (argparse_app , 'help tag' )
198
214
assert out [0 ].startswith ('usage: tag' )
199
215
assert out [1 ] == ''
200
216
assert out [2 ] == 'create a html tag'
201
217
218
+
202
219
def test_argparse_prog (argparse_app ):
203
220
out , err = run_cmd (argparse_app , 'help tag' )
204
221
progname = out [0 ].split (' ' )[1 ]
205
222
assert progname == 'tag'
206
223
224
+
207
225
def test_arglist (argparse_app ):
208
226
out , err = run_cmd (argparse_app , 'arglist "we should" get these' )
209
227
assert out [0 ] == 'True'
210
228
229
+
211
230
def test_arglist_kwargs (argparse_app , capsys ):
212
231
"""Test with_argument_list wrapper passes through kwargs to command function"""
213
232
argparse_app .do_arglist ('arg' , keyword_arg = "foo" )
214
233
out , err = capsys .readouterr ()
215
234
assert out == "foo\n "
216
235
236
+
217
237
def test_preservelist (argparse_app ):
218
238
out , err = run_cmd (argparse_app , 'preservelist foo "bar baz"' )
219
239
assert out [0 ] == "['foo', '\" bar baz\" ']"
@@ -269,6 +289,7 @@ def do_base(self, args):
269
289
func = getattr (args , 'func' )
270
290
func (self , args )
271
291
292
+
272
293
@pytest .fixture
273
294
def subcommand_app ():
274
295
app = SubcommandApp ()
@@ -284,17 +305,20 @@ def test_subcommand_bar(subcommand_app):
284
305
out , err = run_cmd (subcommand_app , 'base bar baz' )
285
306
assert out == ['((baz))' ]
286
307
308
+
287
309
def test_subcommand_invalid (subcommand_app ):
288
310
out , err = run_cmd (subcommand_app , 'base baz' )
289
311
assert err [0 ].startswith ('usage: base' )
290
312
assert err [1 ].startswith ("base: error: argument SUBCOMMAND: invalid choice: 'baz'" )
291
313
314
+
292
315
def test_subcommand_base_help (subcommand_app ):
293
316
out , err = run_cmd (subcommand_app , 'help base' )
294
317
assert out [0 ].startswith ('usage: base' )
295
318
assert out [1 ] == ''
296
319
assert out [2 ] == 'Base command help'
297
320
321
+
298
322
def test_subcommand_help (subcommand_app ):
299
323
# foo has no aliases
300
324
out , err = run_cmd (subcommand_app , 'help base foo' )
@@ -334,14 +358,53 @@ def test_subcommand_help(subcommand_app):
334
358
assert out [1 ] == ''
335
359
assert out [2 ] == 'positional arguments:'
336
360
361
+
337
362
def test_subcommand_invalid_help (subcommand_app ):
338
363
out , err = run_cmd (subcommand_app , 'help base baz' )
339
364
assert out [0 ].startswith ('usage: base' )
340
365
366
+
341
367
def test_add_another_subcommand (subcommand_app ):
342
368
"""
343
369
This tests makes sure _set_parser_prog() sets _prog_prefix on every _SubParsersAction so that all future calls
344
370
to add_parser() write the correct prog value to the parser being added.
345
371
"""
346
372
new_parser = subcommand_app .base_subparsers .add_parser ('new_sub' , help = "stuff" )
347
373
assert new_parser .prog == "base new_sub"
374
+
375
+
376
+ def test_unittest_mock ():
377
+ from unittest import mock
378
+ from cmd2 import CommandSetRegistrationError
379
+
380
+ with mock .patch .object (ArgparseApp , 'namespace_provider' ):
381
+ with pytest .raises (CommandSetRegistrationError ):
382
+ app = ArgparseApp ()
383
+
384
+ with mock .patch .object (ArgparseApp , 'namespace_provider' , spec = True ):
385
+ app = ArgparseApp ()
386
+
387
+ with mock .patch .object (ArgparseApp , 'namespace_provider' , spec_set = True ):
388
+ app = ArgparseApp ()
389
+
390
+ with mock .patch .object (ArgparseApp , 'namespace_provider' , autospec = True ):
391
+ app = ArgparseApp ()
392
+
393
+
394
+ def test_pytest_mock_invalid (mocker ):
395
+ from cmd2 import CommandSetRegistrationError
396
+
397
+ mocker .patch .object (ArgparseApp , 'namespace_provider' )
398
+ with pytest .raises (CommandSetRegistrationError ):
399
+ app = ArgparseApp ()
400
+
401
+
402
+ @pytest .mark .parametrize ('spec_param' , [
403
+ {'spec' : True },
404
+ {'spec_set' : True },
405
+ {'autospec' : True },
406
+ ])
407
+ def test_pytest_mock_valid (mocker , spec_param ):
408
+ mocker .patch .object (ArgparseApp , 'namespace_provider' , ** spec_param )
409
+ app = ArgparseApp ()
410
+
0 commit comments