From 525d32cfde6c2f9fecb0ee44972c18d4b0bf614f Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 16 Jun 2021 13:29:30 -0400 Subject: Fixing tests on Python 3.10 --- cmd2/cmd2.py | 2 +- tests/test_argparse.py | 40 +++++++++++----------- tests/test_cmd2.py | 8 ++--- tests/test_completion.py | 3 +- tests/test_transcript.py | 5 ++- tests/transcripts/from_cmdloop.txt | 12 +++---- .../test_commandset/test_argparse_subcommands.py | 26 +++++++------- tests_isolated/test_commandset/test_commandset.py | 2 +- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 72d93f49..df4d1dc2 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -405,7 +405,7 @@ class Cmd(cmd.Cmd): # Check for command line args if allow_cli_args: - parser = argparse.ArgumentParser() + parser = DEFAULT_ARGUMENT_PARSER() parser.add_argument('-t', '--test', action="store_true", help='Test against transcript(s) in FILE (wildcards OK)') callopts, callargs = parser.parse_known_args() diff --git a/tests/test_argparse.py b/tests/test_argparse.py index 16664d7e..1a637396 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -39,7 +39,7 @@ class ArgparseApp(cmd2.Cmd): ns.custom_stuff = "custom" return ns - say_parser = argparse.ArgumentParser() + say_parser = cmd2.Cmd2ArgumentParser() say_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') say_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -65,7 +65,7 @@ class ArgparseApp(cmd2.Cmd): if keyword_arg is not None: print(keyword_arg) - tag_parser = argparse.ArgumentParser(description='create a html tag') + tag_parser = cmd2.Cmd2ArgumentParser(description='create a html tag') tag_parser.add_argument('tag', help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') @@ -74,7 +74,7 @@ class ArgparseApp(cmd2.Cmd): self.stdout.write('<{0}>{1}'.format(args.tag, ' '.join(args.content))) self.stdout.write('\n') - @cmd2.with_argparser(argparse.ArgumentParser(), ns_provider=namespace_provider) + @cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), ns_provider=namespace_provider) def do_test_argparse_ns(self, args): self.stdout.write('{}'.format(args.custom_stuff)) @@ -92,7 +92,7 @@ class ArgparseApp(cmd2.Cmd): def do_preservelist(self, arglist): self.stdout.write('{}'.format(arglist)) - known_parser = argparse.ArgumentParser() + known_parser = cmd2.Cmd2ArgumentParser() known_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') known_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') known_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -117,11 +117,11 @@ class ArgparseApp(cmd2.Cmd): if keyword_arg is not None: print(keyword_arg) - @cmd2.with_argparser(argparse.ArgumentParser(), preserve_quotes=True, with_unknown_args=True) + @cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), preserve_quotes=True, with_unknown_args=True) def do_test_argparse_with_list_quotes(self, args, extra): self.stdout.write('{}'.format(' '.join(extra))) - @cmd2.with_argparser(argparse.ArgumentParser(), ns_provider=namespace_provider, with_unknown_args=True) + @cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), ns_provider=namespace_provider, with_unknown_args=True) def do_test_argparse_with_list_ns(self, args, extra): self.stdout.write('{}'.format(args.custom_stuff)) @@ -208,14 +208,14 @@ def test_argparse_quoted_arguments_multiple(argparse_app): def test_argparse_help_docstring(argparse_app): out, err = run_cmd(argparse_app, 'help say') - assert out[0].startswith('usage: say') + assert out[0].startswith('Usage: say') assert out[1] == '' assert out[2] == 'Repeat what you tell me to.' def test_argparse_help_description(argparse_app): out, err = run_cmd(argparse_app, 'help tag') - assert out[0].startswith('usage: tag') + assert out[0].startswith('Usage: tag') assert out[1] == '' assert out[2] == 'create a html tag' @@ -263,7 +263,7 @@ class SubcommandApp(cmd2.Cmd): self.poutput('((%s))' % args.z) # create the top-level parser for the base command - base_parser = argparse.ArgumentParser() + base_parser = cmd2.Cmd2ArgumentParser() base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') base_subparsers.required = True @@ -338,13 +338,13 @@ def test_subcommand_bar(subcommand_app): def test_subcommand_invalid(subcommand_app): out, err = run_cmd(subcommand_app, 'base baz') - assert err[0].startswith('usage: base') - assert err[1].startswith("base: error: argument SUBCOMMAND: invalid choice: 'baz'") + assert err[0].startswith('Usage: base') + assert err[1].startswith("Error: argument SUBCOMMAND: invalid choice: 'baz'") def test_subcommand_base_help(subcommand_app): out, err = run_cmd(subcommand_app, 'help base') - assert out[0].startswith('usage: base') + assert out[0].startswith('Usage: base') assert out[1] == '' assert out[2] == 'Base command help' @@ -352,46 +352,46 @@ def test_subcommand_base_help(subcommand_app): def test_subcommand_help(subcommand_app): # foo has no aliases out, err = run_cmd(subcommand_app, 'help base foo') - assert out[0].startswith('usage: base foo') + assert out[0].startswith('Usage: base foo') assert out[1] == '' assert out[2] == 'positional arguments:' # bar has aliases (usage should never show alias name) out, err = run_cmd(subcommand_app, 'help base bar') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base bar_1') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base bar_2') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' # helpless has aliases and no help text (usage should never show alias name) out, err = run_cmd(subcommand_app, 'help base helpless') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base helpless_1') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base helpless_2') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' def test_subcommand_invalid_help(subcommand_app): out, err = run_cmd(subcommand_app, 'help base baz') - assert out[0].startswith('usage: base') + assert out[0].startswith('Usage: base') def test_add_another_subcommand(subcommand_app): diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 49d90dac..928c323a 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -3,7 +3,6 @@ """ Cmd2 unit/functional testing """ -import argparse import builtins import io import os @@ -1365,7 +1364,7 @@ def test_select_ctrl_c(outsim_app, monkeypatch, capsys): class HelpNoDocstringApp(cmd2.Cmd): - greet_parser = argparse.ArgumentParser() + greet_parser = cmd2.Cmd2ArgumentParser() greet_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE") @cmd2.with_argparser(greet_parser, with_unknown_args=True) @@ -1383,11 +1382,12 @@ def test_help_with_no_docstring(capsys): assert err == '' assert ( out - == """usage: greet [-h] [-s] + == """Usage: greet [-h] [-s] optional arguments: -h, --help show this help message and exit -s, --shout N00B EMULATION MODE + """ ) @@ -1396,7 +1396,7 @@ class MultilineApp(cmd2.Cmd): def __init__(self, *args, **kwargs): super().__init__(*args, multiline_commands=['orate'], **kwargs) - orate_parser = argparse.ArgumentParser() + orate_parser = cmd2.Cmd2ArgumentParser() orate_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE") @cmd2.with_argparser(orate_parser, with_unknown_args=True) diff --git a/tests/test_completion.py b/tests/test_completion.py index cde77b93..c61a0eec 100755 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -6,7 +6,6 @@ Unit/functional testing for readline tab completion functions in the cmd2.py mod These are primarily tests related to readline completer functions which handle tab completion of cmd2/cmd commands, file system paths, and shell commands. """ -import argparse import enum import os import sys @@ -1184,7 +1183,7 @@ class SubcommandsWithUnknownExample(cmd2.Cmd): self.poutput('Sport is {}'.format(args.sport)) # create the top-level parser for the base command - base_parser = argparse.ArgumentParser() + base_parser = cmd2.Cmd2ArgumentParser() base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') # create the parser for the "foo" subcommand diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 22e04239..f5ca653e 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -3,7 +3,6 @@ """ Cmd2 functional testing based on transcript """ -import argparse import os import random import re @@ -46,7 +45,7 @@ class CmdLineApp(cmd2.Cmd): self.intro = 'This is an intro banner ...' - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action="store_true", help="atinLay") speak_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE") speak_parser.add_argument('-r', '--repeat', type=int, help="output [n] times") @@ -69,7 +68,7 @@ class CmdLineApp(cmd2.Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input - mumble_parser = argparse.ArgumentParser() + mumble_parser = cmd2.Cmd2ArgumentParser() mumble_parser.add_argument('-r', '--repeat', type=int, help="output [n] times") @cmd2.with_argparser(mumble_parser, with_unknown_args=True) diff --git a/tests/transcripts/from_cmdloop.txt b/tests/transcripts/from_cmdloop.txt index 95537665..f1c68d81 100644 --- a/tests/transcripts/from_cmdloop.txt +++ b/tests/transcripts/from_cmdloop.txt @@ -2,16 +2,16 @@ # so you can see where they are. (Cmd) help say -usage: speak [-h] [-p] [-s] [-r REPEAT]/ */ +Usage: speak [-h] [-p] [-s] [-r REPEAT]/ */ Repeats what you tell me to./ */ optional arguments:/ */ - -h, --help show this help message and exit/ */ - -p, --piglatin atinLay/ */ - -s, --shout N00B EMULATION MODE/ */ - -r REPEAT, --repeat REPEAT/ */ - output [n] times + -h, --help show this help message and exit/ */ + -p, --piglatin atinLay/ */ + -s, --shout N00B EMULATION MODE/ */ + -r, --repeat REPEAT output [n] times/ */ + (Cmd) say goodnight, Gracie goodnight, Gracie (Cmd) say -ps --repeat=5 goodnight, Gracie diff --git a/tests_isolated/test_commandset/test_argparse_subcommands.py b/tests_isolated/test_commandset/test_argparse_subcommands.py index 753806e8..0fc8c47e 100644 --- a/tests_isolated/test_commandset/test_argparse_subcommands.py +++ b/tests_isolated/test_commandset/test_argparse_subcommands.py @@ -4,8 +4,6 @@ reproduces test_argparse.py except with SubCommands """ -import argparse - import pytest import cmd2 @@ -36,7 +34,7 @@ class SubcommandSet(cmd2.CommandSet): self._cmd.poutput('((%s))' % args.z) # create the top-level parser for the base command - base_parser = argparse.ArgumentParser() + base_parser = cmd2.Cmd2ArgumentParser() base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') base_subparsers.required = True @@ -85,13 +83,13 @@ def test_subcommand_bar(subcommand_app): def test_subcommand_invalid(subcommand_app): out, err = run_cmd(subcommand_app, 'base baz') - assert err[0].startswith('usage: base') - assert err[1].startswith("base: error: argument SUBCOMMAND: invalid choice: 'baz'") + assert err[0].startswith('Usage: base') + assert err[1].startswith("Error: argument SUBCOMMAND: invalid choice: 'baz'") def test_subcommand_base_help(subcommand_app): out, err = run_cmd(subcommand_app, 'help base') - assert out[0].startswith('usage: base') + assert out[0].startswith('Usage: base') assert out[1] == '' assert out[2] == 'Base command help' @@ -99,43 +97,43 @@ def test_subcommand_base_help(subcommand_app): def test_subcommand_help(subcommand_app): # foo has no aliases out, err = run_cmd(subcommand_app, 'help base foo') - assert out[0].startswith('usage: base foo') + assert out[0].startswith('Usage: base foo') assert out[1] == '' assert out[2] == 'positional arguments:' # bar has aliases (usage should never show alias name) out, err = run_cmd(subcommand_app, 'help base bar') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base bar_1') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base bar_2') - assert out[0].startswith('usage: base bar') + assert out[0].startswith('Usage: base bar') assert out[1] == '' assert out[2] == 'positional arguments:' # helpless has aliases and no help text (usage should never show alias name) out, err = run_cmd(subcommand_app, 'help base helpless') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base helpless_1') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' out, err = run_cmd(subcommand_app, 'help base helpless_2') - assert out[0].startswith('usage: base helpless') + assert out[0].startswith('Usage: base helpless') assert out[1] == '' assert out[2] == 'positional arguments:' def test_subcommand_invalid_help(subcommand_app): out, err = run_cmd(subcommand_app, 'help base baz') - assert out[0].startswith('usage: base') + assert out[0].startswith('Usage: base') diff --git a/tests_isolated/test_commandset/test_commandset.py b/tests_isolated/test_commandset/test_commandset.py index d082c92e..7e4e1821 100644 --- a/tests_isolated/test_commandset/test_commandset.py +++ b/tests_isolated/test_commandset/test_commandset.py @@ -861,7 +861,7 @@ class CommandSetWithPathComplete(cmd2.CommandSet): """dummy variable prevents this from being autoloaded in other tests""" super(CommandSetWithPathComplete, self).__init__() - parser = argparse.ArgumentParser() + parser = cmd2.Cmd2ArgumentParser() parser.add_argument('path', nargs='+', help='paths', completer=cmd2.Cmd.path_complete) @cmd2.with_argparser(parser) -- cgit v1.2.1