Skip to content

Commit 3f5a235

Browse files
committed
Fixing tests on Python 3.10
1 parent 453fa6b commit 3f5a235

File tree

8 files changed

+47
-51
lines changed

8 files changed

+47
-51
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def __init__(
405405

406406
# Check for command line args
407407
if allow_cli_args:
408-
parser = argparse.ArgumentParser()
408+
parser = DEFAULT_ARGUMENT_PARSER()
409409
parser.add_argument('-t', '--test', action="store_true", help='Test against transcript(s) in FILE (wildcards OK)')
410410
callopts, callargs = parser.parse_known_args()
411411

tests/test_argparse.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def namespace_provider(self) -> argparse.Namespace:
3939
ns.custom_stuff = "custom"
4040
return ns
4141

42-
say_parser = argparse.ArgumentParser()
42+
say_parser = cmd2.Cmd2ArgumentParser()
4343
say_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
4444
say_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
4545
say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -65,7 +65,7 @@ def do_say(self, args, *, keyword_arg: Optional[str] = None):
6565
if keyword_arg is not None:
6666
print(keyword_arg)
6767

68-
tag_parser = argparse.ArgumentParser(description='create a html tag')
68+
tag_parser = cmd2.Cmd2ArgumentParser(description='create a html tag')
6969
tag_parser.add_argument('tag', help='tag')
7070
tag_parser.add_argument('content', nargs='+', help='content to surround with tag')
7171

@@ -74,7 +74,7 @@ def do_tag(self, args):
7474
self.stdout.write('<{0}>{1}</{0}>'.format(args.tag, ' '.join(args.content)))
7575
self.stdout.write('\n')
7676

77-
@cmd2.with_argparser(argparse.ArgumentParser(), ns_provider=namespace_provider)
77+
@cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), ns_provider=namespace_provider)
7878
def do_test_argparse_ns(self, args):
7979
self.stdout.write('{}'.format(args.custom_stuff))
8080

@@ -92,7 +92,7 @@ def do_arglist(self, arglist, *, keyword_arg: Optional[str] = None):
9292
def do_preservelist(self, arglist):
9393
self.stdout.write('{}'.format(arglist))
9494

95-
known_parser = argparse.ArgumentParser()
95+
known_parser = cmd2.Cmd2ArgumentParser()
9696
known_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay')
9797
known_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE')
9898
known_parser.add_argument('-r', '--repeat', type=int, help='output [n] times')
@@ -117,11 +117,11 @@ def do_speak(self, args, extra, *, keyword_arg: Optional[str] = None):
117117
if keyword_arg is not None:
118118
print(keyword_arg)
119119

120-
@cmd2.with_argparser(argparse.ArgumentParser(), preserve_quotes=True, with_unknown_args=True)
120+
@cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), preserve_quotes=True, with_unknown_args=True)
121121
def do_test_argparse_with_list_quotes(self, args, extra):
122122
self.stdout.write('{}'.format(' '.join(extra)))
123123

124-
@cmd2.with_argparser(argparse.ArgumentParser(), ns_provider=namespace_provider, with_unknown_args=True)
124+
@cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), ns_provider=namespace_provider, with_unknown_args=True)
125125
def do_test_argparse_with_list_ns(self, args, extra):
126126
self.stdout.write('{}'.format(args.custom_stuff))
127127

@@ -208,14 +208,14 @@ def test_argparse_quoted_arguments_multiple(argparse_app):
208208

209209
def test_argparse_help_docstring(argparse_app):
210210
out, err = run_cmd(argparse_app, 'help say')
211-
assert out[0].startswith('usage: say')
211+
assert out[0].startswith('Usage: say')
212212
assert out[1] == ''
213213
assert out[2] == 'Repeat what you tell me to.'
214214

215215

216216
def test_argparse_help_description(argparse_app):
217217
out, err = run_cmd(argparse_app, 'help tag')
218-
assert out[0].startswith('usage: tag')
218+
assert out[0].startswith('Usage: tag')
219219
assert out[1] == ''
220220
assert out[2] == 'create a html tag'
221221

@@ -263,7 +263,7 @@ def base_helpless(self, args):
263263
self.poutput('((%s))' % args.z)
264264

265265
# create the top-level parser for the base command
266-
base_parser = argparse.ArgumentParser()
266+
base_parser = cmd2.Cmd2ArgumentParser()
267267
base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
268268
base_subparsers.required = True
269269

@@ -338,60 +338,60 @@ def test_subcommand_bar(subcommand_app):
338338

339339
def test_subcommand_invalid(subcommand_app):
340340
out, err = run_cmd(subcommand_app, 'base baz')
341-
assert err[0].startswith('usage: base')
342-
assert err[1].startswith("base: error: argument SUBCOMMAND: invalid choice: 'baz'")
341+
assert err[0].startswith('Usage: base')
342+
assert err[1].startswith("Error: argument SUBCOMMAND: invalid choice: 'baz'")
343343

344344

345345
def test_subcommand_base_help(subcommand_app):
346346
out, err = run_cmd(subcommand_app, 'help base')
347-
assert out[0].startswith('usage: base')
347+
assert out[0].startswith('Usage: base')
348348
assert out[1] == ''
349349
assert out[2] == 'Base command help'
350350

351351

352352
def test_subcommand_help(subcommand_app):
353353
# foo has no aliases
354354
out, err = run_cmd(subcommand_app, 'help base foo')
355-
assert out[0].startswith('usage: base foo')
355+
assert out[0].startswith('Usage: base foo')
356356
assert out[1] == ''
357357
assert out[2] == 'positional arguments:'
358358

359359
# bar has aliases (usage should never show alias name)
360360
out, err = run_cmd(subcommand_app, 'help base bar')
361-
assert out[0].startswith('usage: base bar')
361+
assert out[0].startswith('Usage: base bar')
362362
assert out[1] == ''
363363
assert out[2] == 'positional arguments:'
364364

365365
out, err = run_cmd(subcommand_app, 'help base bar_1')
366-
assert out[0].startswith('usage: base bar')
366+
assert out[0].startswith('Usage: base bar')
367367
assert out[1] == ''
368368
assert out[2] == 'positional arguments:'
369369

370370
out, err = run_cmd(subcommand_app, 'help base bar_2')
371-
assert out[0].startswith('usage: base bar')
371+
assert out[0].startswith('Usage: base bar')
372372
assert out[1] == ''
373373
assert out[2] == 'positional arguments:'
374374

375375
# helpless has aliases and no help text (usage should never show alias name)
376376
out, err = run_cmd(subcommand_app, 'help base helpless')
377-
assert out[0].startswith('usage: base helpless')
377+
assert out[0].startswith('Usage: base helpless')
378378
assert out[1] == ''
379379
assert out[2] == 'positional arguments:'
380380

381381
out, err = run_cmd(subcommand_app, 'help base helpless_1')
382-
assert out[0].startswith('usage: base helpless')
382+
assert out[0].startswith('Usage: base helpless')
383383
assert out[1] == ''
384384
assert out[2] == 'positional arguments:'
385385

386386
out, err = run_cmd(subcommand_app, 'help base helpless_2')
387-
assert out[0].startswith('usage: base helpless')
387+
assert out[0].startswith('Usage: base helpless')
388388
assert out[1] == ''
389389
assert out[2] == 'positional arguments:'
390390

391391

392392
def test_subcommand_invalid_help(subcommand_app):
393393
out, err = run_cmd(subcommand_app, 'help base baz')
394-
assert out[0].startswith('usage: base')
394+
assert out[0].startswith('Usage: base')
395395

396396

397397
def test_add_another_subcommand(subcommand_app):

tests/test_cmd2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
Cmd2 unit/functional testing
55
"""
6-
import argparse
76
import builtins
87
import io
98
import os
@@ -1365,7 +1364,7 @@ def test_select_ctrl_c(outsim_app, monkeypatch, capsys):
13651364

13661365

13671366
class HelpNoDocstringApp(cmd2.Cmd):
1368-
greet_parser = argparse.ArgumentParser()
1367+
greet_parser = cmd2.Cmd2ArgumentParser()
13691368
greet_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE")
13701369

13711370
@cmd2.with_argparser(greet_parser, with_unknown_args=True)
@@ -1383,11 +1382,12 @@ def test_help_with_no_docstring(capsys):
13831382
assert err == ''
13841383
assert (
13851384
out
1386-
== """usage: greet [-h] [-s]
1385+
== """Usage: greet [-h] [-s]
13871386
13881387
optional arguments:
13891388
-h, --help show this help message and exit
13901389
-s, --shout N00B EMULATION MODE
1390+
13911391
"""
13921392
)
13931393

@@ -1396,7 +1396,7 @@ class MultilineApp(cmd2.Cmd):
13961396
def __init__(self, *args, **kwargs):
13971397
super().__init__(*args, multiline_commands=['orate'], **kwargs)
13981398

1399-
orate_parser = argparse.ArgumentParser()
1399+
orate_parser = cmd2.Cmd2ArgumentParser()
14001400
orate_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE")
14011401

14021402
@cmd2.with_argparser(orate_parser, with_unknown_args=True)

tests/test_completion.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
These are primarily tests related to readline completer functions which handle tab completion of cmd2/cmd commands,
77
file system paths, and shell commands.
88
"""
9-
import argparse
109
import enum
1110
import os
1211
import sys
@@ -1184,7 +1183,7 @@ def base_sport(self, args):
11841183
self.poutput('Sport is {}'.format(args.sport))
11851184

11861185
# create the top-level parser for the base command
1187-
base_parser = argparse.ArgumentParser()
1186+
base_parser = cmd2.Cmd2ArgumentParser()
11881187
base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help')
11891188

11901189
# create the parser for the "foo" subcommand

tests/test_transcript.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
Cmd2 functional testing based on transcript
55
"""
6-
import argparse
76
import os
87
import random
98
import re
@@ -46,7 +45,7 @@ def __init__(self, *args, **kwargs):
4645

4746
self.intro = 'This is an intro banner ...'
4847

49-
speak_parser = argparse.ArgumentParser()
48+
speak_parser = cmd2.Cmd2ArgumentParser()
5049
speak_parser.add_argument('-p', '--piglatin', action="store_true", help="atinLay")
5150
speak_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE")
5251
speak_parser.add_argument('-r', '--repeat', type=int, help="output [n] times")
@@ -69,7 +68,7 @@ def do_speak(self, opts, arg):
6968
do_say = do_speak # now "say" is a synonym for "speak"
7069
do_orate = do_speak # another synonym, but this one takes multi-line input
7170

72-
mumble_parser = argparse.ArgumentParser()
71+
mumble_parser = cmd2.Cmd2ArgumentParser()
7372
mumble_parser.add_argument('-r', '--repeat', type=int, help="output [n] times")
7473

7574
@cmd2.with_argparser(mumble_parser, with_unknown_args=True)

tests/transcripts/from_cmdloop.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
# so you can see where they are.
33

44
(Cmd) help say
5-
usage: speak [-h] [-p] [-s] [-r REPEAT]/ */
5+
Usage: speak [-h] [-p] [-s] [-r REPEAT]/ */
66

77
Repeats what you tell me to./ */
88

99
optional arguments:/ */
10-
-h, --help show this help message and exit/ */
11-
-p, --piglatin atinLay/ */
12-
-s, --shout N00B EMULATION MODE/ */
13-
-r REPEAT, --repeat REPEAT/ */
14-
output [n] times
10+
-h, --help show this help message and exit/ */
11+
-p, --piglatin atinLay/ */
12+
-s, --shout N00B EMULATION MODE/ */
13+
-r, --repeat REPEAT output [n] times/ */
14+
1515
(Cmd) say goodnight, Gracie
1616
goodnight, Gracie
1717
(Cmd) say -ps --repeat=5 goodnight, Gracie

tests_isolated/test_commandset/test_argparse_subcommands.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
reproduces test_argparse.py except with SubCommands
55
"""
66

7-
import argparse
8-
97
import pytest
108

119
import cmd2
@@ -36,7 +34,7 @@ def base_helpless(self, args):
3634
self._cmd.poutput('((%s))' % args.z)
3735

3836
# create the top-level parser for the base command
39-
base_parser = argparse.ArgumentParser()
37+
base_parser = cmd2.Cmd2ArgumentParser()
4038
base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND')
4139
base_subparsers.required = True
4240

@@ -85,57 +83,57 @@ def test_subcommand_bar(subcommand_app):
8583

8684
def test_subcommand_invalid(subcommand_app):
8785
out, err = run_cmd(subcommand_app, 'base baz')
88-
assert err[0].startswith('usage: base')
89-
assert err[1].startswith("base: error: argument SUBCOMMAND: invalid choice: 'baz'")
86+
assert err[0].startswith('Usage: base')
87+
assert err[1].startswith("Error: argument SUBCOMMAND: invalid choice: 'baz'")
9088

9189

9290
def test_subcommand_base_help(subcommand_app):
9391
out, err = run_cmd(subcommand_app, 'help base')
94-
assert out[0].startswith('usage: base')
92+
assert out[0].startswith('Usage: base')
9593
assert out[1] == ''
9694
assert out[2] == 'Base command help'
9795

9896

9997
def test_subcommand_help(subcommand_app):
10098
# foo has no aliases
10199
out, err = run_cmd(subcommand_app, 'help base foo')
102-
assert out[0].startswith('usage: base foo')
100+
assert out[0].startswith('Usage: base foo')
103101
assert out[1] == ''
104102
assert out[2] == 'positional arguments:'
105103

106104
# bar has aliases (usage should never show alias name)
107105
out, err = run_cmd(subcommand_app, 'help base bar')
108-
assert out[0].startswith('usage: base bar')
106+
assert out[0].startswith('Usage: base bar')
109107
assert out[1] == ''
110108
assert out[2] == 'positional arguments:'
111109

112110
out, err = run_cmd(subcommand_app, 'help base bar_1')
113-
assert out[0].startswith('usage: base bar')
111+
assert out[0].startswith('Usage: base bar')
114112
assert out[1] == ''
115113
assert out[2] == 'positional arguments:'
116114

117115
out, err = run_cmd(subcommand_app, 'help base bar_2')
118-
assert out[0].startswith('usage: base bar')
116+
assert out[0].startswith('Usage: base bar')
119117
assert out[1] == ''
120118
assert out[2] == 'positional arguments:'
121119

122120
# helpless has aliases and no help text (usage should never show alias name)
123121
out, err = run_cmd(subcommand_app, 'help base helpless')
124-
assert out[0].startswith('usage: base helpless')
122+
assert out[0].startswith('Usage: base helpless')
125123
assert out[1] == ''
126124
assert out[2] == 'positional arguments:'
127125

128126
out, err = run_cmd(subcommand_app, 'help base helpless_1')
129-
assert out[0].startswith('usage: base helpless')
127+
assert out[0].startswith('Usage: base helpless')
130128
assert out[1] == ''
131129
assert out[2] == 'positional arguments:'
132130

133131
out, err = run_cmd(subcommand_app, 'help base helpless_2')
134-
assert out[0].startswith('usage: base helpless')
132+
assert out[0].startswith('Usage: base helpless')
135133
assert out[1] == ''
136134
assert out[2] == 'positional arguments:'
137135

138136

139137
def test_subcommand_invalid_help(subcommand_app):
140138
out, err = run_cmd(subcommand_app, 'help base baz')
141-
assert out[0].startswith('usage: base')
139+
assert out[0].startswith('Usage: base')

tests_isolated/test_commandset/test_commandset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ def __init__(self, dummy):
861861
"""dummy variable prevents this from being autoloaded in other tests"""
862862
super(CommandSetWithPathComplete, self).__init__()
863863

864-
parser = argparse.ArgumentParser()
864+
parser = cmd2.Cmd2ArgumentParser()
865865
parser.add_argument('path', nargs='+', help='paths', completer=cmd2.Cmd.path_complete)
866866

867867
@cmd2.with_argparser(parser)

0 commit comments

Comments
 (0)