def test_debug(self):
        # Hack: we can modify this from inside the callback.
        confdict = {}

        def callback(option, opt_str, value, parser):
            section = parser.values.config.collapse_named_section('sect')
            # It would actually be better if debug was True here.
            # We check for an unintended change, not ideal behaviour.
            self.assertFalse(section.debug)
            confdict['sect'] = section

        parser = commandline.OptionParser(option_list=[
            optparse.Option('-a', action='callback', callback=callback)])
        parser = helpers.mangle_parser(parser)
        values = parser.get_default_values()
        values._config = mk_config([{
            'sect': basics.HardCodedConfigSection({'class': sect})}])

        values, args = parser.parse_args(['-a', '--debug'], values)
        self.assertFalse(args)
        self.assertTrue(values.debug)
        self.assertTrue(confdict['sect'].debug)

        values = parser.get_default_values()
        values._config = mk_config([{
            'sect': basics.HardCodedConfigSection({'class': sect})}])
        values, args = parser.parse_args(['-a'], values)
        self.assertFalse(args)
        self.assertFalse(values.debug)
        self.assertFalse(confdict['sect'].debug)
    def test_bool_type(self):
        parser = helpers.mangle_parser(commandline.OptionParser())
        parser.add_option(
            "--testing", action='store', type='bool', default=None)

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

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

        try:
            parser.parse_args(["--testing=invalid"])
        except helpers.Error:
            pass
        else:
            self.fail("no error message thrown for --testing=invalid")
 def test_optparse_values_leak(self):
     # This makes sure nothing keeps a reference to the optparse
     # "values" object. This is somewhat important because that
     # "values" object has a reference to the config central
     # object, which keeps a ton of things alive.
     parser = commandline.OptionParser()
     values, args = parser.parse_args([])
     valuesref = weakref.ref(values)
     del values
     self.assertIdentical(None, valuesref())
 def test_optparse_parser_leak(self):
     # This makes sure there is no global reference to the parser.
     # That is not usually critical, but an extra safety net in case
     # the parser (incorrectly) keeps important references around.
     # (at some point it kept the values object alive).
     # This test would fail if standard_option_list was used.
     parser = commandline.OptionParser()
     values, args = parser.parse_args([])
     parserref = weakref.ref(parser)
     del parser
     # XXX I think this is a bug: the values object has a strong
     # ref to the parser via _raise_error...
     del values
     # This is necessary because the parser and its options have
     # cyclical references to each other.
     gc.collect()
     self.assertIdentical(None, parserref())
Exemple #5
0
    def test_config_callback(self):
        @configurable(typename='foon')
        def test():
            return 'foon!'

        parser = helpers.mangle_parser(commandline.OptionParser())
        parser.add_option('--spork',
                          action='callback',
                          callback=commandline.config_callback,
                          type='string',
                          callback_args=('foon', ))
        parser.add_option('--foon',
                          action='callback',
                          callback=commandline.config_callback,
                          type='string',
                          callback_args=('foon', 'utensil'))
        values = parser.get_default_values()
        values._config = mk_config([{
            'afoon':
            basics.HardCodedConfigSection({'class': test})
        }])

        values, args = parser.parse_args(['--spork', 'afoon'], values)
        self.assertEqual('foon!', values.spork)

        try:
            parser.parse_args(['--spork', 'nofoon'], values)
        except helpers.Error as e:
            self.assertEqual(
                "'nofoon' is not a valid foon for --spork "
                "(valid values: 'afoon')", str(e))
        else:
            self.fail('no exception raised')

        try:
            parser.parse_args(['--foon', 'nofoon'], values)
        except helpers.Error as e:
            self.assertEqual(
                "'nofoon' is not a valid utensil for --foon "
                "(valid values: 'afoon')", str(e))
        else:
            self.fail('no exception raised')
 def test_empty_sequence(self):
     parser = helpers.mangle_parser(commandline.OptionParser(
         option_list=[optparse.Option('--seq', action='append')]))
     options, values = parser.parse_args([])
     self.assertEqual([], list(options.seq))