/lib-python/modified-2.7/test/test_argparse.py
Python | 4646 lines | 4639 code | 6 blank | 1 comment | 2 complexity | f31dd679868b0aab7953a6b7fab8a988 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- # Author: Steven J. Bethard <steven.bethard@gmail.com>.
- import codecs
- import inspect
- import os
- import shutil
- import stat
- import sys
- import textwrap
- import tempfile
- import unittest
- import argparse
- from StringIO import StringIO
- class StdIOBuffer(StringIO):
- pass
- from test import test_support
- class TestCase(unittest.TestCase):
- def assertEqual(self, obj1, obj2):
- if obj1 != obj2:
- print('')
- print(repr(obj1))
- print(repr(obj2))
- print(obj1)
- print(obj2)
- super(TestCase, self).assertEqual(obj1, obj2)
- def setUp(self):
- # 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.unset("COLUMNS")
- self.addCleanup(env.__exit__)
- class TempDirMixin(object):
- def setUp(self):
- self.temp_dir = tempfile.mkdtemp()
- self.old_dir = os.getcwd()
- os.chdir(self.temp_dir)
- def tearDown(self):
- os.chdir(self.old_dir)
- shutil.rmtree(self.temp_dir, True)
- def create_readonly_file(self, filename):
- file_path = os.path.join(self.temp_dir, filename)
- with open(file_path, 'w') as file:
- file.write(filename)
- os.chmod(file_path, stat.S_IREAD)
- class Sig(object):
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kwargs = kwargs
- class NS(object):
- def __init__(self, **kwargs):
- self.__dict__.update(kwargs)
- def __repr__(self):
- sorted_items = sorted(self.__dict__.items())
- 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)
- def __ne__(self, other):
- return not (self == other)
- class ArgumentParserError(Exception):
- def __init__(self, message, stdout=None, stderr=None, error_code=None):
- Exception.__init__(self, message, stdout, stderr)
- self.message = message
- self.stdout = stdout
- self.stderr = stderr
- self.error_code = error_code
- def stderr_to_parser_error(parse_args, *args, **kwargs):
- # if this is being called recursively and stderr or stdout is already being
- # redirected, simply call the function and let the enclosing function
- # catch the exception
- if isinstance(sys.stderr, StdIOBuffer) or isinstance(sys.stdout, StdIOBuffer):
- return parse_args(*args, **kwargs)
- # if this is not being called recursively, redirect stderr and
- # use it as the ArgumentParserError message
- old_stdout = sys.stdout
- old_stderr = sys.stderr
- sys.stdout = StdIOBuffer()
- sys.stderr = StdIOBuffer()
- try:
- try:
- result = parse_args(*args, **kwargs)
- for key in list(vars(result)):
- if getattr(result, key) is sys.stdout:
- setattr(result, key, old_stdout)
- if getattr(result, key) is sys.stderr:
- setattr(result, key, old_stderr)
- return result
- except SystemExit:
- code = sys.exc_info()[1].code
- stdout = sys.stdout.getvalue()
- stderr = sys.stderr.getvalue()
- raise ArgumentParserError("SystemExit", stdout, stderr, code)
- finally:
- sys.stdout = old_stdout
- sys.stderr = old_stderr
- class ErrorRaisingArgumentParser(argparse.ArgumentParser):
- def parse_args(self, *args, **kwargs):
- parse_args = super(ErrorRaisingArgumentParser, self).parse_args
- return stderr_to_parser_error(parse_args, *args, **kwargs)
- def exit(self, *args, **kwargs):
- exit = super(ErrorRaisingArgumentParser, self).exit
- return stderr_to_parser_error(exit, *args, **kwargs)
- def error(self, *args, **kwargs):
- error = super(ErrorRaisingArgumentParser, self).error
- return stderr_to_parser_error(error, *args, **kwargs)
- class ParserTesterMetaclass(type):
- """Adds parser tests using the class attributes.
- Classes of this type should specify the following attributes:
- argument_signatures -- a list of Sig objects which specify
- the signatures of Argument objects to be created
- failures -- a list of args lists that should cause the parser
- to fail
- successes -- a list of (initial_args, options, remaining_args) tuples
- where initial_args specifies the string args to be parsed,
- options is a dict that should match the vars() of the options
- parsed out of initial_args, and remaining_args should be any
- remaining unparsed arguments
- """
- def __init__(cls, name, bases, bodydict):
- if name == 'ParserTestCase':
- return
- # default parser signature is empty
- if not hasattr(cls, 'parser_signature'):
- cls.parser_signature = Sig()
- if not hasattr(cls, 'parser_class'):
- cls.parser_class = ErrorRaisingArgumentParser
- # ---------------------------------------
- # functions for adding optional arguments
- # ---------------------------------------
- def no_groups(parser, argument_signatures):
- """Add all arguments directly to the parser"""
- for sig in argument_signatures:
- parser.add_argument(*sig.args, **sig.kwargs)
- def one_group(parser, argument_signatures):
- """Add all arguments under a single group in the parser"""
- group = parser.add_argument_group('foo')
- for sig in argument_signatures:
- group.add_argument(*sig.args, **sig.kwargs)
- def many_groups(parser, argument_signatures):
- """Add each argument in its own group to the parser"""
- for i, sig in enumerate(argument_signatures):
- group = parser.add_argument_group('foo:%i' % i)
- group.add_argument(*sig.args, **sig.kwargs)
- # --------------------------
- # functions for parsing args
- # --------------------------
- def listargs(parser, args):
- """Parse the args by passing in a list"""
- return parser.parse_args(args)
- def sysargs(parser, args):
- """Parse the args by defaulting to sys.argv"""
- old_sys_argv = sys.argv
- sys.argv = [old_sys_argv[0]] + args
- try:
- return parser.parse_args()
- finally:
- sys.argv = old_sys_argv
- # class that holds the combination of one optional argument
- # addition method and one arg parsing method
- class AddTests(object):
- def __init__(self, tester_cls, add_arguments, parse_args):
- self._add_arguments = add_arguments
- self._parse_args = parse_args
- add_arguments_name = self._add_arguments.__name__
- parse_args_name = self._parse_args.__name__
- for test_func in [self.test_failures, self.test_successes]:
- func_name = test_func.__name__
- names = func_name, add_arguments_name, parse_args_name
- test_name = '_'.join(names)
- def wrapper(self, test_func=test_func):
- test_func(self)
- try:
- wrapper.__name__ = test_name
- except TypeError:
- pass
- setattr(tester_cls, test_name, wrapper)
- def _get_parser(self, tester):
- args = tester.parser_signature.args
- kwargs = tester.parser_signature.kwargs
- parser = tester.parser_class(*args, **kwargs)
- self._add_arguments(parser, tester.argument_signatures)
- return parser
- def test_failures(self, tester):
- parser = self._get_parser(tester)
- for args_str in tester.failures:
- args = args_str.split()
- raises = tester.assertRaises
- raises(ArgumentParserError, parser.parse_args, args)
- def test_successes(self, tester):
- parser = self._get_parser(tester)
- for args, expected_ns in tester.successes:
- if isinstance(args, str):
- args = args.split()
- result_ns = self._parse_args(parser, args)
- tester.assertEqual(expected_ns, result_ns)
- # add tests for each combination of an optionals adding method
- # and an arg parsing method
- for add_arguments in [no_groups, one_group, many_groups]:
- for parse_args in [listargs, sysargs]:
- AddTests(cls, add_arguments, parse_args)
- bases = TestCase,
- ParserTestCase = ParserTesterMetaclass('ParserTestCase', bases, {})
- # ===============
- # Optionals tests
- # ===============
- class TestOptionalsSingleDash(ParserTestCase):
- """Test an Optional with a single-dash option string"""
- argument_signatures = [Sig('-x')]
- failures = ['-x', 'a', '--foo', '-x --foo', '-x -y']
- successes = [
- ('', NS(x=None)),
- ('-x a', NS(x='a')),
- ('-xa', NS(x='a')),
- ('-x -1', NS(x='-1')),
- ('-x-1', NS(x='-1')),
- ]
- class TestOptionalsSingleDashCombined(ParserTestCase):
- """Test an Optional with a single-dash option string"""
- argument_signatures = [
- Sig('-x', action='store_true'),
- Sig('-yyy', action='store_const', const=42),
- Sig('-z'),
- ]
- failures = ['a', '--foo', '-xa', '-x --foo', '-x -z', '-z -x',
- '-yx', '-yz a', '-yyyx', '-yyyza', '-xyza']
- successes = [
- ('', NS(x=False, yyy=None, z=None)),
- ('-x', NS(x=True, yyy=None, z=None)),
- ('-za', NS(x=False, yyy=None, z='a')),
- ('-z a', NS(x=False, yyy=None, z='a')),
- ('-xza', NS(x=True, yyy=None, z='a')),
- ('-xz a', NS(x=True, yyy=None, z='a')),
- ('-x -za', NS(x=True, yyy=None, z='a')),
- ('-x -z a', NS(x=True, yyy=None, z='a')),
- ('-y', NS(x=False, yyy=42, z=None)),
- ('-yyy', NS(x=False, yyy=42, z=None)),
- ('-x -yyy -za', NS(x=True, yyy=42, z='a')),
- ('-x -yyy -z a', NS(x=True, yyy=42, z='a')),
- ]
- class TestOptionalsSingleDashLong(ParserTestCase):
- """Test an Optional with a multi-character single-dash option string"""
- argument_signatures = [Sig('-foo')]
- failures = ['-foo', 'a', '--foo', '-foo --foo', '-foo -y', '-fooa']
- successes = [
- ('', NS(foo=None)),
- ('-foo a', NS(foo='a')),
- ('-foo -1', NS(foo='-1')),
- ('-fo a', NS(foo='a')),
- ('-f a', NS(foo='a')),
- ]
- class TestOptionalsSingleDashSubsetAmbiguous(ParserTestCase):
- """Test Optionals where option strings are subsets of each other"""
- argument_signatures = [Sig('-f'), Sig('-foobar'), Sig('-foorab')]
- failures = ['-f', '-foo', '-fo', '-foo b', '-foob', '-fooba', '-foora']
- successes = [
- ('', NS(f=None, foobar=None, foorab=None)),
- ('-f a', NS(f='a', foobar=None, foorab=None)),
- ('-fa', NS(f='a', foobar=None, foorab=None)),
- ('-foa', NS(f='oa', foobar=None, foorab=None)),
- ('-fooa', NS(f='ooa', foobar=None, foorab=None)),
- ('-foobar a', NS(f=None, foobar='a', foorab=None)),
- ('-foorab a', NS(f=None, foobar=None, foorab='a')),
- ]
- class TestOptionalsSingleDashAmbiguous(ParserTestCase):
- """Test Optionals that partially match but are not subsets"""
- argument_signatures = [Sig('-foobar'), Sig('-foorab')]
- failures = ['-f', '-f a', '-fa', '-foa', '-foo', '-fo', '-foo b']
- successes = [
- ('', NS(foobar=None, foorab=None)),
- ('-foob a', NS(foobar='a', foorab=None)),
- ('-foor a', NS(foobar=None, foorab='a')),
- ('-fooba a', NS(foobar='a', foorab=None)),
- ('-foora a', NS(foobar=None, foorab='a')),
- ('-foobar a', NS(foobar='a', foorab=None)),
- ('-foorab a', NS(foobar=None, foorab='a')),
- ]
- class TestOptionalsNumeric(ParserTestCase):
- """Test an Optional with a short opt string"""
- argument_signatures = [Sig('-1', dest='one')]
- failures = ['-1', 'a', '-1 --foo', '-1 -y', '-1 -1', '-1 -2']
- successes = [
- ('', NS(one=None)),
- ('-1 a', NS(one='a')),
- ('-1a', NS(one='a')),
- ('-1-2', NS(one='-2')),
- ]
- class TestOptionalsDoubleDash(ParserTestCase):
- """Test an Optional with a double-dash option string"""
- argument_signatures = [Sig('--foo')]
- failures = ['--foo', '-f', '-f a', 'a', '--foo -x', '--foo --bar']
- successes = [
- ('', NS(foo=None)),
- ('--foo a', NS(foo='a')),
- ('--foo=a', NS(foo='a')),
- ('--foo -2.5', NS(foo='-2.5')),
- ('--foo=-2.5', NS(foo='-2.5')),
- ]
- class TestOptionalsDoubleDashPartialMatch(ParserTestCase):
- """Tests partial matching with a double-dash option string"""
- argument_signatures = [
- Sig('--badger', action='store_true'),
- Sig('--bat'),
- ]
- failures = ['--bar', '--b', '--ba', '--b=2', '--ba=4', '--badge 5']
- successes = [
- ('', NS(badger=False, bat=None)),
- ('--bat X', NS(badger=False, bat='X')),
- ('--bad', NS(badger=True, bat=None)),
- ('--badg', NS(badger=True, bat=None)),
- ('--badge', NS(badger=True, bat=None)),
- ('--badger', NS(badger=True, bat=None)),
- ]
- class TestOptionalsDoubleDashPrefixMatch(ParserTestCase):
- """Tests when one double-dash option string is a prefix of another"""
- argument_signatures = [
- Sig('--badger', action='store_true'),
- Sig('--ba'),
- ]
- failures = ['--bar', '--b', '--ba', '--b=2', '--badge 5']
- successes = [
- ('', NS(badger=False, ba=None)),
- ('--ba X', NS(badger=False, ba='X')),
- ('--ba=X', NS(badger=False, ba='X')),
- ('--bad', NS(badger=True, ba=None)),
- ('--badg', NS(badger=True, ba=None)),
- ('--badge', NS(badger=True, ba=None)),
- ('--badger', NS(badger=True, ba=None)),
- ]
- class TestOptionalsSingleDoubleDash(ParserTestCase):
- """Test an Optional with single- and double-dash option strings"""
- argument_signatures = [
- Sig('-f', action='store_true'),
- Sig('--bar'),
- Sig('-baz', action='store_const', const=42),
- ]
- failures = ['--bar', '-fbar', '-fbaz', '-bazf', '-b B', 'B']
- successes = [
- ('', NS(f=False, bar=None, baz=None)),
- ('-f', NS(f=True, bar=None, baz=None)),
- ('--ba B', NS(f=False, bar='B', baz=None)),
- ('-f --bar B', NS(f=True, bar='B', baz=None)),
- ('-f -b', NS(f=True, bar=None, baz=42)),
- ('-ba -f', NS(f=True, bar=None, baz=42)),
- ]
- class TestOptionalsAlternatePrefixChars(ParserTestCase):
- """Test an Optional with option strings with custom prefixes"""
- parser_signature = Sig(prefix_chars='+:/', add_help=False)
- argument_signatures = [
- Sig('+f', action='store_true'),
- Sig('::bar'),
- Sig('/baz', action='store_const', const=42),
- ]
- failures = ['--bar', '-fbar', '-b B', 'B', '-f', '--bar B', '-baz', '-h', '--help', '+h', '::help', '/help']
- successes = [
- ('', NS(f=False, bar=None, baz=None)),
- ('+f', NS(f=True, bar=None, baz=None)),
- ('::ba B', NS(f=False, bar='B', baz=None)),
- ('+f ::bar B', NS(f=True, bar='B', baz=None)),
- ('+f /b', NS(f=True, bar=None, baz=42)),
- ('/ba +f', NS(f=True, bar=None, baz=42)),
- ]
- class TestOptionalsAlternatePrefixCharsAddedHelp(ParserTestCase):
- """When ``-`` not in prefix_chars, default operators created for help
- should use the prefix_chars in use rather than - or --
- http://bugs.python.org/issue9444"""
- parser_signature = Sig(prefix_chars='+:/', add_help=True)
- argument_signatures = [
- Sig('+f', action='store_true'),
- Sig('::bar'),
- Sig('/baz', action='store_const', const=42),
- ]
- failures = ['--bar', '-fbar', '-b B', 'B', '-f', '--bar B', '-baz']
- successes = [
- ('', NS(f=False, bar=None, baz=None)),
- ('+f', NS(f=True, bar=None, baz=None)),
- ('::ba B', NS(f=False, bar='B', baz=None)),
- ('+f ::bar B', NS(f=True, bar='B', baz=None)),
- ('+f /b', NS(f=True, bar=None, baz=42)),
- ('/ba +f', NS(f=True, bar=None, baz=42))
- ]
- class TestOptionalsAlternatePrefixCharsMultipleShortArgs(ParserTestCase):
- """Verify that Optionals must be called with their defined prefixes"""
- parser_signature = Sig(prefix_chars='+-', add_help=False)
- argument_signatures = [
- Sig('-x', action='store_true'),
- Sig('+y', action='store_true'),
- Sig('+z', action='store_true'),
- ]
- failures = ['-w',
- '-xyz',
- '+x',
- '-y',
- '+xyz',
- ]
- successes = [
- ('', NS(x=False, y=False, z=False)),
- ('-x', NS(x=True, y=False, z=False)),
- ('+y -x', NS(x=True, y=True, z=False)),
- ('+yz -x', NS(x=True, y=True, z=True)),
- ]
- class TestOptionalsShortLong(ParserTestCase):
- """Test a combination of single- and double-dash option strings"""
- argument_signatures = [
- Sig('-v', '--verbose', '-n', '--noisy', action='store_true'),
- ]
- failures = ['--x --verbose', '-N', 'a', '-v x']
- successes = [
- ('', NS(verbose=False)),
- ('-v', NS(verbose=True)),
- ('--verbose', NS(verbose=True)),
- ('-n', NS(verbose=True)),
- ('--noisy', NS(verbose=True)),
- ]
- class TestOptionalsDest(ParserTestCase):
- """Tests various means of setting destination"""
- argument_signatures = [Sig('--foo-bar'), Sig('--baz', dest='zabbaz')]
- failures = ['a']
- successes = [
- ('--foo-bar f', NS(foo_bar='f', zabbaz=None)),
- ('--baz g', NS(foo_bar=None, zabbaz='g')),
- ('--foo-bar h --baz i', NS(foo_bar='h', zabbaz='i')),
- ('--baz j --foo-bar k', NS(foo_bar='k', zabbaz='j')),
- ]
- class TestOptionalsDefault(ParserTestCase):
- """Tests specifying a default for an Optional"""
- argument_signatures = [Sig('-x'), Sig('-y', default=42)]
- failures = ['a']
- successes = [
- ('', NS(x=None, y=42)),
- ('-xx', NS(x='x', y=42)),
- ('-yy', NS(x=None, y='y')),
- ]
- class TestOptionalsNargsDefault(ParserTestCase):
- """Tests not specifying the number of args for an Optional"""
- argument_signatures = [Sig('-x')]
- failures = ['a', '-x']
- successes = [
- ('', NS(x=None)),
- ('-x a', NS(x='a')),
- ]
- class TestOptionalsNargs1(ParserTestCase):
- """Tests specifying the 1 arg for an Optional"""
- argument_signatures = [Sig('-x', nargs=1)]
- failures = ['a', '-x']
- successes = [
- ('', NS(x=None)),
- ('-x a', NS(x=['a'])),
- ]
- class TestOptionalsNargs3(ParserTestCase):
- """Tests specifying the 3 args for an Optional"""
- argument_signatures = [Sig('-x', nargs=3)]
- failures = ['a', '-x', '-x a', '-x a b', 'a -x', 'a -x b']
- successes = [
- ('', NS(x=None)),
- ('-x a b c', NS(x=['a', 'b', 'c'])),
- ]
- class TestOptionalsNargsOptional(ParserTestCase):
- """Tests specifying an Optional arg for an Optional"""
- argument_signatures = [
- Sig('-w', nargs='?'),
- Sig('-x', nargs='?', const=42),
- Sig('-y', nargs='?', default='spam'),
- Sig('-z', nargs='?', type=int, const='42', default='84'),
- ]
- failures = ['2']
- successes = [
- ('', NS(w=None, x=None, y='spam', z=84)),
- ('-w', NS(w=None, x=None, y='spam', z=84)),
- ('-w 2', NS(w='2', x=None, y='spam', z=84)),
- ('-x', NS(w=None, x=42, y='spam', z=84)),
- ('-x 2', NS(w=None, x='2', y='spam', z=84)),
- ('-y', NS(w=None, x=None, y=None, z=84)),
- ('-y 2', NS(w=None, x=None, y='2', z=84)),
- ('-z', NS(w=None, x=None, y='spam', z=42)),
- ('-z 2', NS(w=None, x=None, y='spam', z=2)),
- ]
- class TestOptionalsNargsZeroOrMore(ParserTestCase):
- """Tests specifying an args for an Optional that accepts zero or more"""
- argument_signatures = [
- Sig('-x', nargs='*'),
- Sig('-y', nargs='*', default='spam'),
- ]
- failures = ['a']
- successes = [
- ('', NS(x=None, y='spam')),
- ('-x', NS(x=[], y='spam')),
- ('-x a', NS(x=['a'], y='spam')),
- ('-x a b', NS(x=['a', 'b'], y='spam')),
- ('-y', NS(x=None, y=[])),
- ('-y a', NS(x=None, y=['a'])),
- ('-y a b', NS(x=None, y=['a', 'b'])),
- ]
- class TestOptionalsNargsOneOrMore(ParserTestCase):
- """Tests specifying an args for an Optional that accepts one or more"""
- argument_signatures = [
- Sig('-x', nargs='+'),
- Sig('-y', nargs='+', default='spam'),
- ]
- failures = ['a', '-x', '-y', 'a -x', 'a -y b']
- successes = [
- ('', NS(x=None, y='spam')),
- ('-x a', NS(x=['a'], y='spam')),
- ('-x a b', NS(x=['a', 'b'], y='spam')),
- ('-y a', NS(x=None, y=['a'])),
- ('-y a b', NS(x=None, y=['a', 'b'])),
- ]
- class TestOptionalsChoices(ParserTestCase):
- """Tests specifying the choices for an Optional"""
- argument_signatures = [
- Sig('-f', choices='abc'),
- Sig('-g', type=int, choices=range(5))]
- failures = ['a', '-f d', '-fad', '-ga', '-g 6']
- successes = [
- ('', NS(f=None, g=None)),
- ('-f a', NS(f='a', g=None)),
- ('-f c', NS(f='c', g=None)),
- ('-g 0', NS(f=None, g=0)),
- ('-g 03', NS(f=None, g=3)),
- ('-fb -g4', NS(f='b', g=4)),
- ]
- class TestOptionalsRequired(ParserTestCase):
- """Tests the an optional action that is required"""
- argument_signatures = [
- Sig('-x', type=int, required=True),
- ]
- failures = ['a', '']
- successes = [
- ('-x 1', NS(x=1)),
- ('-x42', NS(x=42)),
- ]
- class TestOptionalsActionStore(ParserTestCase):
- """Tests the store action for an Optional"""
- argument_signatures = [Sig('-x', action='store')]
- failures = ['a', 'a -x']
- successes = [
- ('', NS(x=None)),
- ('-xfoo', NS(x='foo')),
- ]
- class TestOptionalsActionStoreConst(ParserTestCase):
- """Tests the store_const action for an Optional"""
- argument_signatures = [Sig('-y', action='store_const', const=object)]
- failures = ['a']
- successes = [
- ('', NS(y=None)),
- ('-y', NS(y=object)),
- ]
- class TestOptionalsActionStoreFalse(ParserTestCase):
- """Tests the store_false action for an Optional"""
- argument_signatures = [Sig('-z', action='store_false')]
- failures = ['a', '-za', '-z a']
- successes = [
- ('', NS(z=True)),
- ('-z', NS(z=False)),
- ]
- class TestOptionalsActionStoreTrue(ParserTestCase):
- """Tests the store_true action for an Optional"""
- argument_signatures = [Sig('--apple', action='store_true')]
- failures = ['a', '--apple=b', '--apple b']
- successes = [
- ('', NS(apple=False)),
- ('--apple', NS(apple=True)),
- ]
- class TestOptionalsActionAppend(ParserTestCase):
- """Tests the append action for an Optional"""
- argument_signatures = [Sig('--baz', action='append')]
- failures = ['a', '--baz', 'a --baz', '--baz a b']
- successes = [
- ('', NS(baz=None)),
- ('--baz a', NS(baz=['a'])),
- ('--baz a --baz b', NS(baz=['a', 'b'])),
- ]
- class TestOptionalsActionAppendWithDefault(ParserTestCase):
- """Tests the append action for an Optional"""
- argument_signatures = [Sig('--baz', action='append', default=['X'])]
- failures = ['a', '--baz', 'a --baz', '--baz a b']
- successes = [
- ('', NS(baz=['X'])),
- ('--baz a', NS(baz=['X', 'a'])),
- ('--baz a --baz b', NS(baz=['X', 'a', 'b'])),
- ]
- class TestOptionalsActionAppendConst(ParserTestCase):
- """Tests the append_const action for an Optional"""
- argument_signatures = [
- Sig('-b', action='append_const', const=Exception),
- Sig('-c', action='append', dest='b'),
- ]
- failures = ['a', '-c', 'a -c', '-bx', '-b x']
- successes = [
- ('', NS(b=None)),
- ('-b', NS(b=[Exception])),
- ('-b -cx -b -cyz', NS(b=[Exception, 'x', Exception, 'yz'])),
- ]
- class TestOptionalsActionAppendConstWithDefault(ParserTestCase):
- """Tests the append_const action for an Optional"""
- argument_signatures = [
- Sig('-b', action='append_const', const=Exception, default=['X']),
- Sig('-c', action='append', dest='b'),
- ]
- failures = ['a', '-c', 'a -c', '-bx', '-b x']
- successes = [
- ('', NS(b=['X'])),
- ('-b', NS(b=['X', Exception])),
- ('-b -cx -b -cyz', NS(b=['X', Exception, 'x', Exception, 'yz'])),
- ]
- class TestOptionalsActionCount(ParserTestCase):
- """Tests the count action for an Optional"""
- argument_signatures = [Sig('-x', action='count')]
- failures = ['a', '-x a', '-x b', '-x a -x b']
- successes = [
- ('', NS(x=None)),
- ('-x', NS(x=1)),
- ]
- # ================
- # Positional tests
- # ================
- class TestPositionalsNargsNone(ParserTestCase):
- """Test a Positional that doesn't specify nargs"""
- argument_signatures = [Sig('foo')]
- failures = ['', '-x', 'a b']
- successes = [
- ('a', NS(foo='a')),
- ]
- class TestPositionalsNargs1(ParserTestCase):
- """Test a Positional that specifies an nargs of 1"""
- argument_signatures = [Sig('foo', nargs=1)]
- failures = ['', '-x', 'a b']
- successes = [
- ('a', NS(foo=['a'])),
- ]
- class TestPositionalsNargs2(ParserTestCase):
- """Test a Positional that specifies an nargs of 2"""
- argument_signatures = [Sig('foo', nargs=2)]
- failures = ['', 'a', '-x', 'a b c']
- successes = [
- ('a b', NS(foo=['a', 'b'])),
- ]
- class TestPositionalsNargsZeroOrMore(ParserTestCase):
- """Test a Positional that specifies unlimited nargs"""
- argument_signatures = [Sig('foo', nargs='*')]
- failures = ['-x']
- successes = [
- ('', NS(foo=[])),
- ('a', NS(foo=['a'])),
- ('a b', NS(foo=['a', 'b'])),
- ]
- class TestPositionalsNargsZeroOrMoreDefault(ParserTestCase):
- """Test a Positional that specifies unlimited nargs and a default"""
- argument_signatures = [Sig('foo', nargs='*', default='bar')]
- failures = ['-x']
- successes = [
- ('', NS(foo='bar')),
- ('a', NS(foo=['a'])),
- ('a b', NS(foo=['a', 'b'])),
- ]
- class TestPositionalsNargsOneOrMore(ParserTestCase):
- """Test a Positional that specifies one or more nargs"""
- argument_signatures = [Sig('foo', nargs='+')]
- failures = ['', '-x']
- successes = [
- ('a', NS(foo=['a'])),
- ('a b', NS(foo=['a', 'b'])),
- ]
- class TestPositionalsNargsOptional(ParserTestCase):
- """Tests an Optional Positional"""
- argument_signatures = [Sig('foo', nargs='?')]
- failures = ['-x', 'a b']
- successes = [
- ('', NS(foo=None)),
- ('a', NS(foo='a')),
- ]
- class TestPositionalsNargsOptionalDefault(ParserTestCase):
- """Tests an Optional Positional with a default value"""
- argument_signatures = [Sig('foo', nargs='?', default=42)]
- failures = ['-x', 'a b']
- successes = [
- ('', NS(foo=42)),
- ('a', NS(foo='a')),
- ]
- class TestPositionalsNargsOptionalConvertedDefault(ParserTestCase):
- """Tests an Optional Positional with a default value
- that needs to be converted to the appropriate type.
- """
- argument_signatures = [
- Sig('foo', nargs='?', type=int, default='42'),
- ]
- failures = ['-x', 'a b', '1 2']
- successes = [
- ('', NS(foo=42)),
- ('1', NS(foo=1)),
- ]
- class TestPositionalsNargsNoneNone(ParserTestCase):
- """Test two Positionals that don't specify nargs"""
- argument_signatures = [Sig('foo'), Sig('bar')]
- failures = ['', '-x', 'a', 'a b c']
- successes = [
- ('a b', NS(foo='a', bar='b')),
- ]
- class TestPositionalsNargsNone1(ParserTestCase):
- """Test a Positional with no nargs followed by one with 1"""
- argument_signatures = [Sig('foo'), Sig('bar', nargs=1)]
- failures = ['', '--foo', 'a', 'a b c']
- successes = [
- ('a b', NS(foo='a', bar=['b'])),
- ]
- class TestPositionalsNargs2None(ParserTestCase):
- """Test a Positional with 2 nargs followed by one with none"""
- argument_signatures = [Sig('foo', nargs=2), Sig('bar')]
- failures = ['', '--foo', 'a', 'a b', 'a b c d']
- successes = [
- ('a b c', NS(foo=['a', 'b'], bar='c')),
- ]
- class TestPositionalsNargsNoneZeroOrMore(ParserTestCase):
- """Test a Positional with no nargs followed by one with unlimited"""
- argument_signatures = [Sig('foo'), Sig('bar', nargs='*')]
- failures = ['', '--foo']
- successes = [
- ('a', NS(foo='a', bar=[])),
- ('a b', NS(foo='a', bar=['b'])),
- ('a b c', NS(foo='a', bar=['b', 'c'])),
- ]
- class TestPositionalsNargsNoneOneOrMore(ParserTestCase):
- """Test a Positional with no nargs followed by one with one or more"""
- argument_signatures = [Sig('foo'), Sig('bar', nargs='+')]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo='a', bar=['b'])),
- ('a b c', NS(foo='a', bar=['b', 'c'])),
- ]
- class TestPositionalsNargsNoneOptional(ParserTestCase):
- """Test a Positional with no nargs followed by one with an Optional"""
- argument_signatures = [Sig('foo'), Sig('bar', nargs='?')]
- failures = ['', '--foo', 'a b c']
- successes = [
- ('a', NS(foo='a', bar=None)),
- ('a b', NS(foo='a', bar='b')),
- ]
- class TestPositionalsNargsZeroOrMoreNone(ParserTestCase):
- """Test a Positional with unlimited nargs followed by one with none"""
- argument_signatures = [Sig('foo', nargs='*'), Sig('bar')]
- failures = ['', '--foo']
- successes = [
- ('a', NS(foo=[], bar='a')),
- ('a b', NS(foo=['a'], bar='b')),
- ('a b c', NS(foo=['a', 'b'], bar='c')),
- ]
- class TestPositionalsNargsOneOrMoreNone(ParserTestCase):
- """Test a Positional with one or more nargs followed by one with none"""
- argument_signatures = [Sig('foo', nargs='+'), Sig('bar')]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo=['a'], bar='b')),
- ('a b c', NS(foo=['a', 'b'], bar='c')),
- ]
- class TestPositionalsNargsOptionalNone(ParserTestCase):
- """Test a Positional with an Optional nargs followed by one with none"""
- argument_signatures = [Sig('foo', nargs='?', default=42), Sig('bar')]
- failures = ['', '--foo', 'a b c']
- successes = [
- ('a', NS(foo=42, bar='a')),
- ('a b', NS(foo='a', bar='b')),
- ]
- class TestPositionalsNargs2ZeroOrMore(ParserTestCase):
- """Test a Positional with 2 nargs followed by one with unlimited"""
- argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='*')]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo=['a', 'b'], bar=[])),
- ('a b c', NS(foo=['a', 'b'], bar=['c'])),
- ]
- class TestPositionalsNargs2OneOrMore(ParserTestCase):
- """Test a Positional with 2 nargs followed by one with one or more"""
- argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='+')]
- failures = ['', '--foo', 'a', 'a b']
- successes = [
- ('a b c', NS(foo=['a', 'b'], bar=['c'])),
- ]
- class TestPositionalsNargs2Optional(ParserTestCase):
- """Test a Positional with 2 nargs followed by one optional"""
- argument_signatures = [Sig('foo', nargs=2), Sig('bar', nargs='?')]
- failures = ['', '--foo', 'a', 'a b c d']
- successes = [
- ('a b', NS(foo=['a', 'b'], bar=None)),
- ('a b c', NS(foo=['a', 'b'], bar='c')),
- ]
- class TestPositionalsNargsZeroOrMore1(ParserTestCase):
- """Test a Positional with unlimited nargs followed by one with 1"""
- argument_signatures = [Sig('foo', nargs='*'), Sig('bar', nargs=1)]
- failures = ['', '--foo', ]
- successes = [
- ('a', NS(foo=[], bar=['a'])),
- ('a b', NS(foo=['a'], bar=['b'])),
- ('a b c', NS(foo=['a', 'b'], bar=['c'])),
- ]
- class TestPositionalsNargsOneOrMore1(ParserTestCase):
- """Test a Positional with one or more nargs followed by one with 1"""
- argument_signatures = [Sig('foo', nargs='+'), Sig('bar', nargs=1)]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo=['a'], bar=['b'])),
- ('a b c', NS(foo=['a', 'b'], bar=['c'])),
- ]
- class TestPositionalsNargsOptional1(ParserTestCase):
- """Test a Positional with an Optional nargs followed by one with 1"""
- argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs=1)]
- failures = ['', '--foo', 'a b c']
- successes = [
- ('a', NS(foo=None, bar=['a'])),
- ('a b', NS(foo='a', bar=['b'])),
- ]
- class TestPositionalsNargsNoneZeroOrMore1(ParserTestCase):
- """Test three Positionals: no nargs, unlimited nargs and 1 nargs"""
- argument_signatures = [
- Sig('foo'),
- Sig('bar', nargs='*'),
- Sig('baz', nargs=1),
- ]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo='a', bar=[], baz=['b'])),
- ('a b c', NS(foo='a', bar=['b'], baz=['c'])),
- ]
- class TestPositionalsNargsNoneOneOrMore1(ParserTestCase):
- """Test three Positionals: no nargs, one or more nargs and 1 nargs"""
- argument_signatures = [
- Sig('foo'),
- Sig('bar', nargs='+'),
- Sig('baz', nargs=1),
- ]
- failures = ['', '--foo', 'a', 'b']
- successes = [
- ('a b c', NS(foo='a', bar=['b'], baz=['c'])),
- ('a b c d', NS(foo='a', bar=['b', 'c'], baz=['d'])),
- ]
- class TestPositionalsNargsNoneOptional1(ParserTestCase):
- """Test three Positionals: no nargs, optional narg and 1 nargs"""
- argument_signatures = [
- Sig('foo'),
- Sig('bar', nargs='?', default=0.625),
- Sig('baz', nargs=1),
- ]
- failures = ['', '--foo', 'a']
- successes = [
- ('a b', NS(foo='a', bar=0.625, baz=['b'])),
- ('a b c', NS(foo='a', bar='b', baz=['c'])),
- ]
- class TestPositionalsNargsOptionalOptional(ParserTestCase):
- """Test two optional nargs"""
- argument_signatures = [
- Sig('foo', nargs='?'),
- Sig('bar', nargs='?', default=42),
- ]
- failures = ['--foo', 'a b c']
- successes = [
- ('', NS(foo=None, bar=42)),
- ('a', NS(foo='a', bar=42)),
- ('a b', NS(foo='a', bar='b')),
- ]
- class TestPositionalsNargsOptionalZeroOrMore(ParserTestCase):
- """Test an Optional narg followed by unlimited nargs"""
- argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs='*')]
- failures = ['--foo']
- successes = [
- ('', NS(foo=None, bar=[])),
- ('a', NS(foo='a', bar=[])),
- ('a b', NS(foo='a', bar=['b'])),
- ('a b c', NS(foo='a', bar=['b', 'c'])),
- ]
- class TestPositionalsNargsOptionalOneOrMore(ParserTestCase):
- """Test an Optional narg followed by one or more nargs"""
- argument_signatures = [Sig('foo', nargs='?'), Sig('bar', nargs='+')]
- failures = ['', '--foo']
- successes = [
- ('a', NS(foo=None, bar=['a'])),
- ('a b', NS(foo='a', bar=['b'])),
- ('a b c', NS(foo='a', bar=['b', 'c'])),
- ]
- class TestPositionalsChoicesString(ParserTestCase):
- """Test a set of single-character choices"""
- argument_signatures = [Sig('spam', choices=set('abcdefg'))]
- failures = ['', '--foo', 'h', '42', 'ef']
- successes = [
- ('a', NS(spam='a')),
- ('g', NS(spam='g')),
- ]
- class TestPositionalsChoicesInt(ParserTestCase):
- """Test a set of integer choices"""
- argument_signatures = [Sig('spam', type=int, choices=range(20))]
- failures = ['', '--foo', 'h', '42', 'ef']
- successes = [
- ('4', NS(spam=4)),
- ('15', NS(spam=15)),
- ]
- class TestPositionalsActionAppend(ParserTestCase):
- """Test the 'append' action"""
- argument_signatures = [
- Sig('spam', action='append'),
- Sig('spam', action='append', nargs=2),
- ]
- failures = ['', '--foo', 'a', 'a b', 'a b c d']
- successes = [
- ('a b c', NS(spam=['a', ['b', 'c']])),
- ]
- # ========================================
- # Combined optionals and positionals tests
- # ========================================
- class TestOptionalsNumericAndPositionals(ParserTestCase):
- """Tests negative number args when numeric options are present"""
- argument_signatures = [
- Sig('x', nargs='?'),
- Sig('-4', dest='y', action='store_true'),
- ]
- failures = ['-2', '-315']
- successes = [
- ('', NS(x=None, y=False)),
- ('a', NS(x='a', y=False)),
- ('-4', NS(x=None, y=True)),
- ('-4 a', NS(x='a', y=True)),
- ]
- class TestOptionalsAlmostNumericAndPositionals(ParserTestCase):
- """Tests negative number args when almost numeric options are present"""
- argument_signatures = [
- Sig('x', nargs='?'),
- Sig('-k4', dest='y', action='store_true'),
- ]
- failures = ['-k3']
- successes = [
- ('', NS(x=None, y=False)),
- ('-2', NS(x='-2', y=False)),
- ('a', NS(x='a', y=False)),
- ('-k4', NS(x=None, y=True)),
- ('-k4 a', NS(x='a', y=True)),
- ]
- class TestEmptyAndSpaceContainingArguments(ParserTestCase):
- argument_signatures = [
- Sig('x', nargs='?'),
- Sig('-y', '--yyy', dest='y'),
- ]
- failures = ['-y']
- successes = [
- ([''], NS(x='', y=None)),
- (['a badger'], NS(x='a badger', y=None)),
- (['-a badger'], NS(x='-a badger', y=None)),
- (['-y', ''], NS(x=None, y='')),
- (['-y', 'a badger'], NS(x=None, y='a badger')),
- (['-y', '-a badger'], NS(x=None, y='-a badger')),
- (['--yyy=a badger'], NS(x=None, y='a badger')),
- (['--yyy=-a badger'], NS(x=None, y='-a badger')),
- ]
- class TestPrefixCharacterOnlyArguments(ParserTestCase):
- parser_signature = Sig(prefix_chars='-+')
- argument_signatures = [
- Sig('-', dest='x', nargs='?', const='badger'),
- Sig('+', dest='y', type=int, default=42),
- Sig('-+-', dest='z', action='store_true'),
- ]
- failures = ['-y', '+ -']
- successes = [
- ('', NS(x=None, y=42, z=False)),
- ('-', NS(x='badger', y=42, z=False)),
- ('- X', NS(x='X', y=42, z=False)),
- ('+ -3', NS(x=None, y=-3, z=False)),
- ('-+-', NS(x=None, y=42, z=True)),
- ('- ===', NS(x='===', y=42, z=False)),
- ]
- class TestNargsZeroOrMore(ParserTestCase):
- """Tests specifying an args for an Optional that accepts zero or more"""
- argument_signatures = [Sig('-x', nargs='*'), Sig('y', nargs='*')]
- failures = []
- successes = [
- ('', NS(x=None, y=[])),
- ('-x', NS(x=[], y=[])),
- ('-x a', NS(x=['a'], y=[])),
- ('-x a -- b', NS(x=['a'], y=['b'])),
- ('a', NS(x=None, y=['a'])),
- ('a -x', NS(x=[], y=['a'])),
- ('a -x b', NS(x=['b'], y=['a'])),
- ]
- class TestNargsRemainder(ParserTestCase):
- """Tests specifying a positional with nargs=REMAINDER"""
- argument_signatures = [Sig('x'), Sig('y', nargs='...'), Sig('-z')]
- failures = ['', '-z', '-z Z']
- successes = [
- ('X', NS(x='X', y=[], z=None)),
- ('-z Z X', NS(x='X', y=[], z='Z')),
- ('X A B -z Z', NS(x='X', y=['A', 'B', '-z', 'Z'], z=None)),
- ('X Y --foo', NS(x='X', y=['Y', '--foo'], z=None)),
- ]
- class TestOptionLike(ParserTestCase):
- """Tests options that may or may not be arguments"""
- argument_signatures = [
- Sig('-x', type=float),
- Sig('-3', type=float, dest='y'),
- Sig('z', nargs='*'),
- ]
- failures = ['-x', '-y2.5', '-xa', '-x -a',
- '-x -3', '-x -3.5', '-3 -3.5',
- '-x -2.5', '-x -2.5 a', '-3 -.5',
- 'a x -1', '-x -1 a', '-3 -1 a']
- successes = [
- ('', NS(x=None, y=None, z=[])),
- ('-x 2.5', NS(x=2.5, y=None, z=[])),
- ('-x 2.5 a', NS(x=2.5, y=None, z=['a'])),
- ('-3.5', NS(x=None, y=0.5, z=[])),
- ('-3-.5', NS(x=None, y=-0.5, z=[])),
- ('-3 .5', NS(x=None, y=0.5, z=[])),
- ('a -3.5', NS(x=None, y=0.5, z=['a'])),
- ('a', NS(x=None, y=None, z=['a'])),
- ('a -x 1', NS(x=1.0, y=None, z=['a'])),
- ('-x 1 a', NS(x=1.0, y=None, z=['a'])),
- ('-3 1 a', NS(x=None, y=1.0, z=['a'])),
- ]
- class TestDefaultSuppress(ParserTestCase):
- """Test actions with suppressed defaults"""
- argument_signatures = [
- Sig('foo', nargs='?', default=argparse.SUPPRESS),
- Sig('bar', nargs='*', default=argparse.SUPPRESS),
- Sig('--baz', action='store_true', default=argparse.SUPPRESS),
- ]
- failures = ['-x']
- successes = [
- ('', NS()),
- ('a', NS(foo='a')),
- ('a b', NS(foo='a', bar=['b'])),
- ('--baz', NS(baz=True)),
- ('a --baz', NS(foo='a', baz=True)),
- ('--baz a b', NS(foo='a', bar=['b'], baz=True)),
- ]
- class TestParserDefaultSuppress(ParserTestCase):
- """Test actions with a parser-level default of SUPPRESS"""
- parser_signature = Sig(argument_default=argparse.SUPPRESS)
- argument_signatures = [
- Sig('foo', nargs='?'),
- Sig('bar', nargs='*'),
- Sig('--baz', action='store_true'),
- ]
- failures = ['-x']
- successes = [
- ('', NS()),
- ('a', NS(foo='a')),
- ('a b', NS(foo='a', bar=['b'])),
- ('--baz', NS(baz=True)),
- ('a --baz', NS(foo='a', baz=True)),
- ('--baz a b', NS(foo='a', bar=['b'], baz=True)),
- ]
- class TestParserDefault42(ParserTestCase):
- """Test actions with a parser-level default of 42"""
- parser_signature = Sig(argument_default=42, version='1.0')
- argument_signatures = [
- Sig('foo', nargs='?'),
- Sig('bar', nargs='*'),
- Sig('--baz', action='store_true'),
- ]
- failures = ['-x']
- successes = [
- ('', NS(foo=42, bar=42, baz=42)),
- ('a', NS(foo='a', bar=42, baz=42)),
- ('a b', NS(foo='a', bar=['b'], baz=42)),
- ('--baz', NS(foo=42, bar=42, baz=True)),
- ('a --baz', NS(foo='a', bar=42, baz=True)),
- ('--baz a b', NS(foo='a', bar=['b'], baz=True)),
- ]
- class TestArgumentsFromFile(TempDirMixin, ParserTestCase):
- """Test reading arguments from a file"""
- def setUp(self):
- super(TestArgumentsFromFile, self).setUp()
- file_texts = [
- ('hello', 'hello world!\n'),
- ('recursive', '-a\n'
- 'A\n'
- '@hello'),
- ('invalid', '@no-such-path\n'),
- ]
- for path, text in file_texts:
- file = open(path, 'w')
- file.write(text)
- file.close()
- parser_signature = Sig(fromfile_prefix_chars='@')
- argument_signatures = [
- Sig('-a'),
- Sig('x'),
- Sig('y', nargs='+'),
- ]
- failures = ['', '-b', 'X', '@invalid', '@missing']
- successes = [
- ('X Y', NS(a=None, x='X', y=['Y'])),
- ('X -a A Y Z', NS(a='A', x='X', y=['Y', 'Z'])),
- ('@hello X', NS(a=None, x='hello world!', y=['X'])),
- ('X @hello', NS(a=None, x='X', y=['hello world!'])),
- ('-a B @recursive Y Z', NS(a='A', x='hello world!', y=['Y', 'Z'])),
- ('X @recursive Z -a B', NS(a='B', x='X', y=['hello world!', 'Z'])),
- ]
- class TestArgumentsFromFileConverter(TempDirMixin, ParserTestCase):
- """Test reading arguments from a file"""
- def setUp(self):
- super(TestArgumentsFromFileConverter, self).setUp()
- file_texts = [
- ('hello', 'hello world!\n'),
- ]
- for path, text in file_texts:
- file = open(path, 'w')
- file.write(text)
- file.close()
- class FromFileConverterArgumentParser(ErrorRaisingArgumentParser):
- def convert_arg_line_to_args(self, arg_line):
- for arg in arg_line.split():
- if not arg.strip():
- continue
- yield arg
- parser_class = FromFileConverterArgumentParser
- parser_signature = Sig(fromfile_prefix_chars='@')
- argument_signatures = [
- Sig('y', nargs='+'),
- ]
- failures = []
- successes = [
- ('@hello X', NS(y=['hello', 'world!', 'X'])),
- ]
- # =====================
- # Type conversion tests
- # =====================
- class TestFileTypeRepr(TestCase):
- def test_r(self):
- type = argparse.FileType('r')
- self.assertEqual("FileType('r')", repr(type))
- def test_wb_1(self):
- type = argparse.FileType('wb', 1)
- self.assertEqual("FileType('wb', 1)", repr(type))
- class RFile(object):
- seen = {}
- def __init__(self, name):
- self.name = name
- __hash__ = None
- def __eq__(self, other):
- if other in self.seen:
- text = self.seen[other]
- else:
- text = self.seen[other] = other.read()
- other.close()
- if not isinstance(text, str):
- text = text.decode('ascii')
- return self.name == other.name == text
- class TestFileTypeR(TempDirMixin, ParserTestCase):
- """Test the FileType option/argument type for reading files"""
- def setUp(self):
- super(TestFileTypeR, self).setUp()
- for file_name in ['foo', 'bar']:
- file = open(os.path.join(self.temp_dir, file_name), 'w')
- file.write(file_name)
- file.close()
- self.create_readonly_file('readonly')
- argument_signatures = [
- Sig('-x', type=argparse.FileType()),
- Sig('spam', type=argparse.FileType('r')),
- ]
- failures = ['-x', '-x bar', 'non-existent-file.txt']
- successes = [
- ('foo', NS(x=None, spam=RFile('foo'))),
- ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))),
- ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))),
- ('-x - -', NS(x=sys.stdin, spam=sys.stdin)),
- ('readonly', NS(x=None, spam=RFile('readonly'))),
- ]
- class TestFileTypeRB(TempDirMixin, ParserTestCase):
- """Test the FileType option/argument type for reading files"""
- def setUp(self):
- super(TestFileTypeRB, self).setUp()
- for file_name in ['foo', 'bar']:
- file = open(os.path.join(self.temp_dir, file_name), 'w')
- file.write(file_name)
- file.close()
- argument_signatures = [
- Sig('-x', type=argparse.FileType('rb')),
- Sig('spam', type=argparse.FileType('rb')),
- ]
- failures = ['-x', '-x bar']
- successes = [
- ('foo', NS(x=None, spam=RFile('foo'))),
- ('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))),
- ('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))),
- ('-x - -', NS(x=sys.stdin, spam=sys.stdin)),
- ]
- class WFile(object):
- seen = set()
- 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.'
- if 'b' in other.mode:
- text = text.encode('ascii')
- other.write(text)
- other.close()
- self.seen.add(other)
- return self.name == other.name
- class TestFileTypeW(TempDirMixin, ParserTestCase):
- """Test the FileType option/argument type for writing files"""
- def setUp(self):
- super(TestFileTypeW, self).setUp()
- self.create_readonly_file('readonly')
- argument_signatures = [
- Sig('-x', type=argparse.FileType('w')),
- Sig('spam', type=argparse.FileType('w')),
- ]
- failures = ['-x', '-x bar']
- failures = ['-x', '-x bar', 'readonly']
- successes = [
- ('foo', NS(x=None, spam=WFile('foo'))),
- ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))),
- ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))),
- ('-x - -', NS(x=sys.stdout, spam=sys.stdout)),
- ]
- class TestFileTypeWB(TempDirMixin, ParserTestCase):
- argument_signatures = [
- Sig('-x', type=argparse.FileType('wb')),
- Sig('spam', type=argparse.FileType('wb')),
- ]
- failures = ['-x', '-x bar']
- successes = [
- ('foo', NS(x=None, spam=WFile('foo'))),
- ('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))),
- ('bar -x foo', NS(x=WFile('foo'), spam=WFile('bar'))),
- ('-x - -', NS(x=sys.stdout, spam=sys.stdout)),
- ]
- class TestTypeCallable(ParserTestCase):
- """Test some callables as option/argument types"""
- argument_signatures = [
- Sig('--eggs', type=complex),
- Sig('spam', type=float),
- ]
- failures = ['a', '42j', '--eggs a', '--eggs 2i']
- successes = [
- ('--eggs=42 42', NS(eggs=42, spam=42.0)),
- ('--eggs 2j -- -1.5', NS(eggs=2j, spam=-1.5)),
- ('1024.675', NS(eggs=None, spam=1024.675)),
- ]
- class TestTypeUserDefined(ParserTestCase):
- """Test a user-defined option/argument type"""
- class MyType(TestCase):
- def __init__(self, value):
- self.value = value
- __hash__ = None
- def __eq__(self, other):
- return (type(self), self.value) == (type(other), other.value)
- argument_signatures = [
- Sig('-x', type=MyType),
- Sig('spam', type=MyType),
- ]
- failures = []
- successes = [
- ('a -x b', NS(x=MyType('b'), spam=MyType('a'))),
- ('-xf g', NS(x=MyType('f'), spam=MyType('g'))),
- ]
- class TestTypeClassicClass(ParserTestCase):
- """Test a classic class type"""
- class C:
- def __init__(self, value):
- self.value = value
- __hash__ = None
- def __eq__(self, other):
- return (type(self), self.value) == (type(other), other.value)
- argument_signatures = [
- Sig('-x', type=C),
- Sig('spam', type=C),
- ]
- failures = []
- successes = [
- ('a -x b', NS(x=C('b'), spam=C('a'))),
- ('-xf g', NS(x=C('f'), spam=C('g'))),
- ]
- class TestTypeRegistration(TestCase):
- """Test a user-defined type by registering it"""
- def test(self):
- def get_my_type(string):
- return '…
Large files files are truncated, but you can click here to view the full file