Exemplo n.º 1
0
    def test_filtered(self):
        stub_stdouts(self)
        parser = ArgumentParser()
        with self.assertRaises(SystemExit):
            parser.error('some random other error')

        self.assertIn('some random other error', sys.stderr.getvalue())
Exemplo n.º 2
0
 def test_integration_invalid_default_value(self):
     argparser = ArgumentParser(prog='prog', add_help=False)
     with self.assertRaises(ValueError):
         argparser.add_argument('-p',
                                '--params',
                                action=StoreCommaDelimitedList,
                                default='123')
Exemplo n.º 3
0
    def test_filtered(self):
        stub_stdouts(self)
        parser = ArgumentParser()
        with self.assertRaises(SystemExit):
            parser.error('some random other error')

        self.assertIn('some random other error', sys.stderr.getvalue())
Exemplo n.º 4
0
    def test_integration_set_limit(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument(
            '-p', '--params', action=StoreDelimitedListBase, maxlen=1)

        parsed, extras = argparser.parse_known_args(['-p', '3,4,5'])
        self.assertEqual(parsed.params, ['3'])
Exemplo n.º 5
0
    def test_integration_optional_kwargs(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument(
            '-p', '--params', action=StoreDelimitedListBase, sep='.')

        parsed, extras = argparser.parse_known_args(['-p', '3,4.5'])
        self.assertEqual(parsed.params, ['3,4', '5'])
Exemplo n.º 6
0
    def test_integration_set_limit(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument('-p',
                               '--params',
                               action=StoreDelimitedListBase,
                               maxlen=1)

        parsed, extras = argparser.parse_known_args(['-p', '3,4,5'])
        self.assertEqual(parsed.params, ['3'])
Exemplo n.º 7
0
    def test_integration_optional_kwargs(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument('-p',
                               '--params',
                               action=StoreDelimitedListBase,
                               sep='.')

        parsed, extras = argparser.parse_known_args(['-p', '3,4.5'])
        self.assertEqual(parsed.params, ['3,4', '5'])
Exemplo n.º 8
0
    def test_integration_optional(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument(
            '-p', '--params', action=StoreCommaDelimitedList, required=False)

        parsed, extras = argparser.parse_known_args(['-p', '3,4'])
        self.assertEqual(parsed.params, ['3', '4'])

        parsed, extras = argparser.parse_known_args()
        self.assertEqual(parsed.params, None)
Exemplo n.º 9
0
 def argparser_factory(self):
     argparser = ArgumentParser(
         prog=self.prog,
         description=self.description,
     )
     setattr(argparser, ATTR_ROOT_PKG, self.package_name)
     return argparser
Exemplo n.º 10
0
    def argparser_factory(self):
        """
        Produces argparser for this type of Runtime.
        """

        return ArgumentParser(
            prog=self.prog, description=self.__doc__, add_help=False,
        )
Exemplo n.º 11
0
    def test_integration_edge_cases(self):
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-p',
                               '--params',
                               action=StoreCommaDelimitedList,
                               default=['1', '2'])

        parsed, extras = argparser.parse_known_args(['--params=1,2'])
        self.assertEqual(parsed.params, ['1', '2'])

        parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3'])
        self.assertEqual(parsed.params, ['8', '3'])

        parsed, extras = argparser.parse_known_args(['--params', ''])
        self.assertEqual(parsed.params, [])

        parsed, extras = argparser.parse_known_args(['--params='])
        self.assertEqual(parsed.params, [])

        parsed, extras = argparser.parse_known_args(['--params=,'])
        self.assertEqual(parsed.params, [''])

        parsed, extras = argparser.parse_known_args(['--params=1,'])
        self.assertEqual(parsed.params, ['1'])
Exemplo n.º 12
0
    def test_integration_edge_cases(self):
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument(
            '-p', '--params', action=StoreCommaDelimitedList,
            default=['1', '2'])

        parsed, extras = argparser.parse_known_args(['--params=1,2'])
        self.assertEqual(parsed.params, ['1', '2'])

        parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3'])
        self.assertEqual(parsed.params, ['8', '3'])

        parsed, extras = argparser.parse_known_args(['--params', ''])
        self.assertEqual(parsed.params, [])

        parsed, extras = argparser.parse_known_args(['--params='])
        self.assertEqual(parsed.params, [])

        parsed, extras = argparser.parse_known_args(['--params=,'])
        self.assertEqual(parsed.params, [''])

        parsed, extras = argparser.parse_known_args(['--params=1,'])
        self.assertEqual(parsed.params, ['1'])
Exemplo n.º 13
0
    def test_integration_optional(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument('-p',
                               '--params',
                               action=StoreCommaDelimitedList,
                               required=False)

        parsed, extras = argparser.parse_known_args(['-p', '3,4'])
        self.assertEqual(parsed.params, ['3', '4'])

        parsed, extras = argparser.parse_known_args()
        self.assertEqual(parsed.params, None)
Exemplo n.º 14
0
    def test_integration(self):
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument(
            '-p', '--params', action=StoreCommaDelimitedList,
            default=('1', '2',))

        parsed, extras = argparser.parse_known_args(['-p', '3,4'])
        self.assertEqual(parsed.params, ['3', '4'])

        parsed, extras = argparser.parse_known_args(['-p', '34'])
        self.assertEqual(parsed.params, ['34'])

        parsed, extras = argparser.parse_known_args([])
        self.assertEqual(parsed.params, ('1', '2'))
Exemplo n.º 15
0
    def test_integration(self):
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-p',
                               '--params',
                               action=StoreCommaDelimitedList,
                               default=(
                                   '1',
                                   '2',
                               ))

        parsed, extras = argparser.parse_known_args(['-p', '3,4'])
        self.assertEqual(parsed.params, ['3', '4'])

        parsed, extras = argparser.parse_known_args(['-p', '34'])
        self.assertEqual(parsed.params, ['34'])

        parsed, extras = argparser.parse_known_args([])
        self.assertEqual(parsed.params, ('1', '2'))
Exemplo n.º 16
0
 def test_integration_invalid_default_value(self):
     argparser = ArgumentParser(prog='prog', add_help=False)
     with self.assertRaises(ValueError):
         argparser.add_argument(
             '-p', '--params', action=StoreCommaDelimitedList,
             default='123')
Exemplo n.º 17
0
 def test_error(self):
     stub_stdouts(self)
     parser = ArgumentParser()
     parser.error(_('too few arguments'))
     self.assertEqual('', sys.stdout.getvalue())
     self.assertEqual('', sys.stderr.getvalue())
Exemplo n.º 18
0
 def setUp(self):
     self.parser = ArgumentParser()
     init_argparser_common(self.parser)
Exemplo n.º 19
0
    def test_deprecation(self):
        import warnings
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-n', '--normal', action='store')
        argparser.add_argument(
            '-d', '--deprecated', action='store', deprecation=True)
        argparser.add_argument(
            '--bye', action=StoreDelimitedListBase, deprecation='bye')

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # test that they store stuff
            args = argparser.parse_args(['-n', 'hello'])
            self.assertEqual(args.normal, 'hello')
            args = argparser.parse_args(['-d', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--deprecated', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--bye', 'hello,goodbye'])
            self.assertEqual(args.bye, ['hello', 'goodbye'])

        # and the warnings are triggered
        self.assertEqual(
            "option '-d' is deprecated", str(w[0].message))
        self.assertEqual(
            "option '--deprecated' is deprecated", str(w[1].message))
        self.assertEqual(
            "option '--bye' is deprecated: bye", str(w[2].message))

        stream = StringIO()
        argparser.print_help(file=stream)
        # deprecated options are not visible on help
        self.assertNotIn("--deprecated", stream.getvalue())
        self.assertNotIn("--bye", stream.getvalue())
        self.assertIn("--normal", stream.getvalue())
Exemplo n.º 20
0
 def test_error(self):
     stub_stdouts(self)
     parser = ArgumentParser()
     parser.error(_('too few arguments'))
     self.assertEqual('', sys.stdout.getvalue())
     self.assertEqual('', sys.stderr.getvalue())
Exemplo n.º 21
0
    def test_integration_choices_in_list(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument(
            '-p', '--params', choices=['1', '2', '3'],
            action=StoreDelimitedListBase)

        parsed, extras = argparser.parse_known_args(['-p', '3'])
        self.assertEqual(parsed.params, ['3'])
        parsed, extras = argparser.parse_known_args(['-p', '3,2'])
        self.assertEqual(parsed.params, ['3', '2'])
        parsed, extras = argparser.parse_known_args(['-p', '3,2,1'])
        self.assertEqual(parsed.params, ['3', '2', '1'])
        parsed, extras = argparser.parse_known_args(['-p', '3,3,3'])
        self.assertEqual(parsed.params, ['3', '3', '3'])

        stub_stdouts(self)

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['-p', '3,2,1,0'])

        self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue())

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['-p', '0'])

        argparser.add_argument(
            '--dot', choices=['a', 'b', 'c'],
            action=StoreDelimitedListBase, sep='.')
        parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c'])
        self.assertEqual(parsed.dot, ['a', 'b', 'c'])

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['--dot', 'a,b,c'])
Exemplo n.º 22
0
    def test_integration_choices_in_list(self):
        argparser = ArgumentParser(prog='prog', add_help=False)

        argparser.add_argument('-p',
                               '--params',
                               choices=['1', '2', '3'],
                               action=StoreDelimitedListBase)

        parsed, extras = argparser.parse_known_args(['-p', '3'])
        self.assertEqual(parsed.params, ['3'])
        parsed, extras = argparser.parse_known_args(['-p', '3,2'])
        self.assertEqual(parsed.params, ['3', '2'])
        parsed, extras = argparser.parse_known_args(['-p', '3,2,1'])
        self.assertEqual(parsed.params, ['3', '2', '1'])
        parsed, extras = argparser.parse_known_args(['-p', '3,3,3'])
        self.assertEqual(parsed.params, ['3', '3', '3'])

        stub_stdouts(self)

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['-p', '3,2,1,0'])

        self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue())

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['-p', '0'])

        argparser.add_argument('--dot',
                               choices=['a', 'b', 'c'],
                               action=StoreDelimitedListBase,
                               sep='.')
        parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c'])
        self.assertEqual(parsed.dot, ['a', 'b', 'c'])

        with self.assertRaises(SystemExit):
            argparser.parse_known_args(['--dot', 'a,b,c'])
Exemplo n.º 23
0
    def test_deprecation(self):
        import warnings
        argparser = ArgumentParser(prog='prog', add_help=False)
        argparser.add_argument('-n', '--normal', action='store')
        argparser.add_argument('-d',
                               '--deprecated',
                               action='store',
                               deprecation=True)
        argparser.add_argument('--bye',
                               action=StoreDelimitedListBase,
                               deprecation='bye')

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            # test that they store stuff
            args = argparser.parse_args(['-n', 'hello'])
            self.assertEqual(args.normal, 'hello')
            args = argparser.parse_args(['-d', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--deprecated', 'hello'])
            self.assertEqual(args.deprecated, 'hello')
            args = argparser.parse_args(['--bye', 'hello,goodbye'])
            self.assertEqual(args.bye, ['hello', 'goodbye'])

        # and the warnings are triggered
        self.assertEqual("option '-d' is deprecated", str(w[0].message))
        self.assertEqual("option '--deprecated' is deprecated",
                         str(w[1].message))
        self.assertEqual("option '--bye' is deprecated: bye",
                         str(w[2].message))

        stream = StringIO()
        argparser.print_help(file=stream)
        # deprecated options are not visible on help
        self.assertNotIn("--deprecated", stream.getvalue())
        self.assertNotIn("--bye", stream.getvalue())
        self.assertIn("--normal", stream.getvalue())
Exemplo n.º 24
0
class TestCommonArgparser(unittest.TestCase):
    """
    Test out some non-trivial parsing options in the common parser
    """

    def setUp(self):
        self.parser = ArgumentParser()
        init_argparser_common(self.parser)

    def parse(self, args):
        return self.parser.parse_known_args(args)[0]

    def test_parse_default_coverage_type(self):
        self.assertIs(
            DEFAULT_COVER_REPORT_TYPE_OPTIONS,
            self.parse([]).cover_report_types,
        )
        # default always show this
        self.assertIn('text', self.parse([]).cover_report_types)
        # check for deprecated option
        self.assertIsNone(self.parse([]).coverage_type)

    def test_parse_default_cover_report_type_legacy(self):
        import warnings
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            self.assertEqual(
                'html',
                self.parse(['--coverage-type=html']).coverage_type)

            self.assertIn("will be removed by", str(w[0].message))
            # a somewhat more complete test
            config = {}
            driver = cli.KarmaDriver()
            with pretty_logging(
                    logger='calmjs.dev', stream=mocks.StringIO()) as log:
                # emulate the parsing into a spec
                spec = Spec(**vars(self.parse(['--coverage-type=html'])))
                driver._apply_coverage_reporters(spec, config)

        self.assertIn("WARNING", log.getvalue())
        self.assertIn("'coverage_type' is deprecated", log.getvalue())
        self.assertEqual({
            'type': 'html',
            'dir': realpath('coverage'),
        }, config['coverageReporter'])

    def test_parse_default_cover_report_type_legacy_default(self):
        import warnings
        with warnings.catch_warnings(record=True):
            warnings.simplefilter('always')
            self.assertEqual(
                'default',
                self.parse(['--coverage-type=default']).coverage_type)

            # a somewhat more complete test
            config = {}
            driver = cli.KarmaDriver()
            with pretty_logging(
                    logger='calmjs.dev', stream=mocks.StringIO()) as log:
                # emulate the parsing into a spec
                spec = Spec(**vars(self.parse(['--coverage-type=default'])))
                driver._apply_coverage_reporters(spec, config)

        self.assertNotIn("WARNING", log.getvalue())
        self.assertNotIn("'coverage_type' is deprecated", log.getvalue())
        # the default value actually is supported
        self.assertEqual(
            sorted(DEFAULT_COVER_REPORT_TYPE_OPTIONS),
            sorted([
                r['type'] for r in config['coverageReporter']['reporters']]),
        )

    def test_parse_default_cover_report_type_specified(self):
        self.assertEqual(
            ['html'],
            self.parse(['--cover-report-type=html']).cover_report_types)

    def test_parse_default_cover_report_type_specified_multiple(self):
        self.assertEqual(
            ['html', 'text'],
            self.parse(['--cover-report-type=html,text']).cover_report_types)

    def test_parse_default_cover_report_type_bad(self):
        stub_stdouts(self)
        with self.assertRaises(SystemExit):
            self.parse(['--cover-report-type=bad'])