示例#1
0
    def test_bool_type(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument(
            "--testing", action=arghparse.StoreBool, default=None)

        for raw_val in ("n", "no", "false"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = parser.parse_args(['--testing=' + allowed])
                self.assertEqual(
                    namespace.testing, False,
                    msg="for --testing=%s, got %r, expected False" %
                        (allowed, namespace.testing))

        for raw_val in ("y", "yes", "true"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = parser.parse_args(['--testing=' + allowed])
                self.assertEqual(
                    namespace.testing, True,
                    msg="for --testing=%s, got %r, expected False" %
                        (allowed, namespace.testing))

        try:
            parser.parse_args(["--testing=invalid"])
        except argparse_helpers.Error:
            pass
        else:
            self.fail("no error message thrown for --testing=invalid")
示例#2
0
    def test_bool_type(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument("--testing",
                            action=arghparse.StoreBool,
                            default=None)

        for raw_val in ("n", "no", "false"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = parser.parse_args(['--testing=' + allowed])
                self.assertEqual(
                    namespace.testing,
                    False,
                    msg="for --testing=%s, got %r, expected False" %
                    (allowed, namespace.testing))

        for raw_val in ("y", "yes", "true"):
            for allowed in (raw_val.upper(), raw_val.lower()):
                namespace = parser.parse_args(['--testing=' + allowed])
                self.assertEqual(
                    namespace.testing,
                    True,
                    msg="for --testing=%s, got %r, expected False" %
                    (allowed, namespace.testing))

        try:
            parser.parse_args(["--testing=invalid"])
        except argparse_helpers.Error:
            pass
        else:
            self.fail("no error message thrown for --testing=invalid")
示例#3
0
    def test_debug(self):
        # debug passed
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=True))
        namespace = parser.parse_args(["--debug"])
        assert parser.debug is False
        assert namespace.debug is True

        # debug not passed
        namespace = parser.parse_args([])
        assert parser.debug is False
        assert namespace.debug is False

        # debug passed in sys.argv -- early debug attr on the parser instance is set
        with mock.patch('sys.argv', ['script', '--debug']):
            parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=True))
            assert parser.debug is True
示例#4
0
    def test_extend_comma_toggle_action(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument('--testing', action='extend_comma_toggle')
        parser.add_argument('--testing-nargs',
                            nargs='+',
                            action='extend_comma_toggle')

        test_values = (
            ('', ([], [])),
            (',', ([], [])),
            (',,', ([], [])),
            ('a', ([], ['a'])),
            ('a,-b,-c,d', (['b', 'c'], ['a', 'd'])),
        )
        for raw_val, expected in test_values:
            namespace = parser.parse_args([
                '--testing=' + raw_val,
                '--testing-nargs',
                raw_val,
                raw_val,
            ])
            self.assertEqual(namespace.testing, expected)
            self.assertEqual(namespace.testing_nargs,
                             (expected[0] * 2, expected[1] * 2))

        # start with negated arg
        namespace = parser.parse_args(['--testing=-a'])
        self.assertEqual(namespace.testing, (['a'], []))
示例#5
0
    def test_debug(self):
        # debug passed
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=True))
        namespace = parser.parse_args(["--debug"])
        assert parser.debug is False
        assert namespace.debug is True

        # debug not passed
        namespace = parser.parse_args([])
        assert parser.debug is False
        assert namespace.debug is False

        # debug passed in sys.argv -- early debug attr on the parser instance is set
        with mock.patch('sys.argv', ['script', '--debug']):
            parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=True))
            assert parser.debug is True
示例#6
0
    def test_debug_disabled(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=False))

        # ensure the option isn't there if disabled
        with pytest.raises(argparse_helpers.Error):
            namespace = parser.parse_args(["--debug"])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.debug is False
        # but namespace attribute doesn't
        assert not hasattr(namespace, 'debug')
示例#7
0
    def test_debug_disabled(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser(debug=False))

        # ensure the option isn't there if disabled
        with pytest.raises(argparse_helpers.Error):
            namespace = parser.parse_args(["--debug"])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.debug is False
        # but namespace attribute doesn't
        assert not hasattr(namespace, 'debug')
示例#8
0
    def test_verbosity_disabled(self):
        parser = argparse_helpers.mangle_parser(
            arghparse.ArgumentParser(quiet=False, verbose=False))

        # ensure the options aren't there if disabled
        for args in ('-q', '--quiet', '-v', '--verbose'):
            with pytest.raises(argparse_helpers.Error):
                namespace = parser.parse_args([args])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.verbosity == 0
        # but namespace attribute doesn't
        assert not hasattr(namespace, 'verbosity')
示例#9
0
    def test_verbosity_disabled(self):
        parser = argparse_helpers.mangle_parser(
            arghparse.ArgumentParser(quiet=False, verbose=False))

        # ensure the options aren't there if disabled
        for args in ('-q', '--quiet', '-v', '--verbose'):
            with pytest.raises(argparse_helpers.Error):
                namespace = parser.parse_args([args])

        namespace = parser.parse_args([])
        # parser attribute still exists
        assert parser.verbosity == 0
        # but namespace attribute doesn't
        assert not hasattr(namespace, 'verbosity')
示例#10
0
 def test_verbosity(self):
     values = (
         ([], 0),
         (['-q'], -1),
         (['--quiet'], -1),
         (['-v'], 1),
         (['--verbose'], 1),
         (['-q', '-v'], 0),
         (['--quiet', '--verbose'], 0),
         (['-q', '-q'], -2),
         (['-v', '-v'], 2),
     )
     for args, val in values:
         with mock.patch('sys.argv', ['script'] + args):
             parser = argparse_helpers.mangle_parser(
                 arghparse.ArgumentParser(quiet=True, verbose=True))
             namespace = parser.parse_args(args)
             assert parser.verbosity == val, '{} failed'.format(args)
             assert namespace.verbosity == val, '{} failed'.format(args)
示例#11
0
 def test_verbosity(self):
     values = (
         ([], 0),
         (['-q'], -1),
         (['--quiet'], -1),
         (['-v'], 1),
         (['--verbose'], 1),
         (['-q', '-v'], 0),
         (['--quiet', '--verbose'], 0),
         (['-q', '-q'], -2),
         (['-v', '-v'], 2),
     )
     for args, val in values:
         with mock.patch('sys.argv', ['script'] + args):
             parser = argparse_helpers.mangle_parser(
                 arghparse.ArgumentParser(quiet=True, verbose=True))
             namespace = parser.parse_args(args)
             assert parser.verbosity == val, '{} failed'.format(args)
             assert namespace.verbosity == val, '{} failed'.format(args)
示例#12
0
    def test_extend_comma_action(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument('--testing', action='extend_comma')
        parser.add_argument('--testing-nargs', nargs='+', action='extend_comma')

        test_values = (
            ('', []),
            (',', []),
            (',,', []),
            ('a', ['a']),
            ('a,b,-c', ['a', 'b', '-c']),
        )
        for raw_val, expected in test_values:
            namespace = parser.parse_args([
                '--testing=' + raw_val,
                '--testing-nargs', raw_val, raw_val,
                ])
            self.assertEqual(namespace.testing, expected)
            self.assertEqual(namespace.testing_nargs, expected * 2)
示例#13
0
    def test_help(self, capsys):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        with mock.patch('subprocess.Popen') as popen:
            # --help long option tries man page first before falling back to help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(['--help'])
            popen.assert_called_once()
            assert popen.call_args[0][0][0] == 'man'
            captured = capsys.readouterr()
            assert captured.out.strip().startswith('usage: ')
            popen.reset_mock()

            # -h short option just displays the regular help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(['-h'])
            popen.assert_not_called()
            captured = capsys.readouterr()
            assert captured.out.strip().startswith('usage: ')
            popen.reset_mock()
示例#14
0
    def test_help(self, capsys):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        with mock.patch('subprocess.Popen') as popen:
            # --help long option tries man page first before falling back to help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(['--help'])
            assert popen.call_count == 1
            assert popen.call_args[0][0][0] == 'man'
            captured = capsys.readouterr()
            assert captured.out.strip().startswith('usage: ')
            popen.reset_mock()

            # -h short option just displays the regular help output
            with pytest.raises(argparse_helpers.Exit):
                namespace = parser.parse_args(['-h'])
            assert not popen.called
            captured = capsys.readouterr()
            assert captured.out.strip().startswith('usage: ')
            popen.reset_mock()
示例#15
0
    def test_extend_comma_toggle_action(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument('--testing', action='extend_comma_toggle')
        parser.add_argument('--testing-nargs', nargs='+', action='extend_comma_toggle')

        test_values = (
            ('', ((), ())),
            (',', ((), ())),
            (',,', ((), ())),
            ('a', ((), ('a',))),
            ('a,-b,-c,d', (('b', 'c'), ('a', 'd'))),
        )
        for raw_val, expected in test_values:
            namespace = parser.parse_args([
                '--testing=' + raw_val,
                '--testing-nargs', raw_val, raw_val,
                ])
            self.assertEqual(namespace.testing, expected)
            self.assertEqual(namespace.testing_nargs, (tuple(expected[0] * 2), tuple(expected[1] * 2)))

        # start with negated arg
        namespace = parser.parse_args(['--testing=-a'])
        self.assertEqual(namespace.testing, (('a',), ()))
示例#16
0
    def test_extend_comma_action(self):
        parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
        parser.add_argument('--testing', action='extend_comma')
        parser.add_argument('--testing-nargs',
                            nargs='+',
                            action='extend_comma')

        test_values = (
            ('', []),
            (',', []),
            (',,', []),
            ('a', ['a']),
            ('a,b,-c', ['a', 'b', '-c']),
        )
        for raw_val, expected in test_values:
            namespace = parser.parse_args([
                '--testing=' + raw_val,
                '--testing-nargs',
                raw_val,
                raw_val,
            ])
            self.assertEqual(namespace.testing, expected)
            self.assertEqual(namespace.testing_nargs, expected * 2)
示例#17
0
 def setup_method(self, method):
     self.parser = argparse_helpers.mangle_parser(arghparse.ArgumentParser())
示例#18
0
 def test_debug_disabled(self):
     # ensure the option isn't there if disabled.
     parser = argparse_helpers.mangle_parser(
         arghparse.ArgumentParser(debug=False))
     namespace = parser.parse_args([])
     assert not hasattr(namespace, 'debug')
示例#19
0
 def __setup_csv_actions_parser(self):
     self.csv_parser = argparse_helpers.mangle_parser(arghparse.CsvActionsParser())
示例#20
0
 def __setup_optionals_parser(self):
     self.optionals_parser = argparse_helpers.mangle_parser(
         arghparse.OptionalsParser())
示例#21
0
 def __setup(self):
     self.parser = argparse_helpers.mangle_parser(
         arghparse.CopyableParser())
示例#22
0
 def setup_method(self, method):
     self.parser = argparse_helpers.mangle_parser(
         arghparse.ArgumentParser())
示例#23
0
 def __setup_csv_actions_parser(self):
     self.csv_parser = argparse_helpers.mangle_parser(
         arghparse.CsvActionsParser())
示例#24
0
 def __setup(self):
     self.parser = argparse_helpers.mangle_parser(arghparse.CopyableParser())
示例#25
0
 def __setup_optionals_parser(self):
     self.optionals_parser = argparse_helpers.mangle_parser(arghparse.OptionalsParser())