diff options
Diffstat (limited to 'Lib/test/test_argparse.py')
| -rw-r--r-- | Lib/test/test_argparse.py | 88 |
1 files changed, 62 insertions, 26 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 30fdf4a..22c26cc 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -11,13 +11,12 @@ import tempfile import unittest import argparse -from StringIO import StringIO +from io import StringIO +from test import support class StdIOBuffer(StringIO): pass -from test import test_support - class TestCase(unittest.TestCase): def assertEqual(self, obj1, obj2): @@ -33,7 +32,7 @@ class TestCase(unittest.TestCase): # The tests assume that line wrapping occurs at 80 columns, but this # behaviour can be overridden by setting the COLUMNS environment # variable. To ensure that this assumption is true, unset COLUMNS. - env = test_support.EnvironmentVarGuard() + env = support.EnvironmentVarGuard() env.unset("COLUMNS") self.addCleanup(env.__exit__) @@ -72,8 +71,6 @@ class NS(object): kwarg_str = ', '.join(['%s=%r' % tup for tup in sorted_items]) return '%s(%s)' % (type(self).__name__, kwarg_str) - __hash__ = None - def __eq__(self, other): return vars(self) == vars(other) @@ -1430,8 +1427,6 @@ class RFile(object): def __init__(self, name): self.name = name - __hash__ = None - def __eq__(self, other): if other in self.seen: text = self.seen[other] @@ -1458,7 +1453,7 @@ class TestFileTypeR(TempDirMixin, ParserTestCase): Sig('-x', type=argparse.FileType()), Sig('spam', type=argparse.FileType('r')), ] - failures = ['-x', '-x bar', 'non-existent-file.txt'] + failures = ['-x', '', 'non-existent-file.txt'] successes = [ ('foo', NS(x=None, spam=RFile('foo'))), ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), @@ -1498,7 +1493,7 @@ class TestFileTypeRB(TempDirMixin, ParserTestCase): Sig('-x', type=argparse.FileType('rb')), Sig('spam', type=argparse.FileType('rb')), ] - failures = ['-x', '-x bar'] + failures = ['-x', ''] successes = [ ('foo', NS(x=None, spam=RFile('foo'))), ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))), @@ -1513,8 +1508,6 @@ class WFile(object): def __init__(self, name): self.name = name - __hash__ = None - def __eq__(self, other): if other not in self.seen: text = 'Check that file is writable.' @@ -1539,8 +1532,7 @@ class TestFileTypeW(TempDirMixin, ParserTestCase): Sig('-x', type=argparse.FileType('w')), Sig('spam', type=argparse.FileType('w')), ] - failures = ['-x', '-x bar'] - failures = ['-x', '-x bar', 'readonly'] + failures = ['-x', '', 'readonly'] successes = [ ('foo', NS(x=None, spam=WFile('foo'))), ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), @@ -1555,7 +1547,7 @@ class TestFileTypeWB(TempDirMixin, ParserTestCase): Sig('-x', type=argparse.FileType('wb')), Sig('spam', type=argparse.FileType('wb')), ] - failures = ['-x', '-x bar'] + failures = ['-x', ''] successes = [ ('foo', NS(x=None, spam=WFile('foo'))), ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))), @@ -1587,8 +1579,6 @@ class TestTypeUserDefined(ParserTestCase): def __init__(self, value): self.value = value - __hash__ = None - def __eq__(self, other): return (type(self), self.value) == (type(other), other.value) @@ -1611,8 +1601,6 @@ class TestTypeClassicClass(ParserTestCase): def __init__(self, value): self.value = value - __hash__ = None - def __eq__(self, other): return (type(self), self.value) == (type(other), other.value) @@ -1745,7 +1733,8 @@ class TestAddSubparsers(TestCase): def assertArgumentParserError(self, *args, **kwargs): self.assertRaises(ArgumentParserError, *args, **kwargs) - def _get_parser(self, subparser_help=False, prefix_chars=None): + def _get_parser(self, subparser_help=False, prefix_chars=None, + aliases=False): # create a parser with a subparsers argument if prefix_chars: parser = ErrorRaisingArgumentParser( @@ -1761,13 +1750,21 @@ class TestAddSubparsers(TestCase): 'bar', type=float, help='bar help') # check that only one subparsers argument can be added - subparsers = parser.add_subparsers(help='command help') + subparsers_kwargs = {} + if aliases: + subparsers_kwargs['metavar'] = 'COMMAND' + subparsers_kwargs['title'] = 'commands' + else: + subparsers_kwargs['help'] = 'command help' + subparsers = parser.add_subparsers(**subparsers_kwargs) self.assertArgumentParserError(parser.add_subparsers) # add first sub-parser parser1_kwargs = dict(description='1 description') if subparser_help: parser1_kwargs['help'] = '1 help' + if aliases: + parser1_kwargs['aliases'] = ['1alias1', '1alias2'] parser1 = subparsers.add_parser('1', **parser1_kwargs) parser1.add_argument('-w', type=int, help='w help') parser1.add_argument('x', choices='abc', help='x help') @@ -1792,7 +1789,7 @@ class TestAddSubparsers(TestCase): return parser def setUp(self): - super(TestAddSubparsers, self).setUp() + super().setUp() self.parser = self._get_parser() self.command_help_parser = self._get_parser(subparser_help=True) @@ -1997,6 +1994,45 @@ class TestAddSubparsers(TestCase): -y {1,2,3} y help ''')) + def test_alias_invocation(self): + parser = self._get_parser(aliases=True) + self.assertEqual( + parser.parse_known_args('0.5 1alias1 b'.split()), + (NS(foo=False, bar=0.5, w=None, x='b'), []), + ) + self.assertEqual( + parser.parse_known_args('0.5 1alias2 b'.split()), + (NS(foo=False, bar=0.5, w=None, x='b'), []), + ) + + def test_error_alias_invocation(self): + parser = self._get_parser(aliases=True) + self.assertArgumentParserError(parser.parse_args, + '0.5 1alias3 b'.split()) + + def test_alias_help(self): + parser = self._get_parser(aliases=True, subparser_help=True) + self.maxDiff = None + self.assertEqual(parser.format_help(), textwrap.dedent("""\ + usage: PROG [-h] [--foo] bar COMMAND ... + + main description + + positional arguments: + bar bar help + + optional arguments: + -h, --help show this help message and exit + --foo foo help + + commands: + COMMAND + 1 (1alias1, 1alias2) + 1 help + 2 2 help + 3 3 help + """)) + # ============ # Groups tests # ============ @@ -2047,7 +2083,7 @@ class TestParentParsers(TestCase): self.assertRaises(ArgumentParserError, *args, **kwargs) def setUp(self): - super(TestParentParsers, self).setUp() + super().setUp() self.wxyz_parent = ErrorRaisingArgumentParser(add_help=False) self.wxyz_parent.add_argument('--w') x_group = self.wxyz_parent.add_argument_group('x') @@ -4740,19 +4776,19 @@ class TestImportStar(TestCase): items = [ name for name, value in vars(argparse).items() - if not name.startswith("_") + if not (name.startswith("_") or name == 'ngettext') if not inspect.ismodule(value) ] self.assertEqual(sorted(items), sorted(argparse.__all__)) def test_main(): # silence warnings about version argument - these are expected - with test_support.check_warnings( + with support.check_warnings( ('The "version" argument to ArgumentParser is deprecated.', DeprecationWarning), ('The (format|print)_version method is deprecated', DeprecationWarning)): - test_support.run_unittest(__name__) + support.run_unittest(__name__) # Remove global references to avoid looking like we have refleaks. RFile.seen = {} WFile.seen = set() |
