def test_instantiate_broken_ref(self): @configurable(typename='drawer') def broken(): raise errors.ComplexInstantiationError('broken') manager = central.ConfigManager([{ 'one': basics.HardCodedConfigSection({ 'class': drawer, 'content': basics.HardCodedConfigSection({'class': broken}) }), 'multi': basics.HardCodedConfigSection({ 'class': drawer, 'contents': [basics.HardCodedConfigSection({'class': broken})] }), }]) self.check_error( "Failed instantiating section 'one':\n" "Instantiating reference 'content' pointing at None:\n" "Failed instantiating section None:\n" "Failed instantiating section None: exception caught from 'pkgcore.test.config.test_central.broken':\n" "'broken', callable unset!", manager.collapse_named_section('one').instantiate) self.check_error( "Failed instantiating section 'multi':\n" "Instantiating reference 'contents' pointing at None:\n" "Failed instantiating section None:\n" "Failed instantiating section None: exception caught from 'pkgcore.test.config.test_central.broken':\n" "'broken', callable unset!", manager.collapse_named_section('multi').instantiate)
def test_broken_default(self): def broken(): raise errors.ComplexInstantiationError('broken') manager = central.ConfigManager([{ 'thing': basics.HardCodedConfigSection({ 'class': drawer, 'default': True, 'content': basics.HardCodedConfigSection({'class': 'spork'}) }), 'thing2': basics.HardCodedConfigSection({ 'class': broken, 'default': True }) }]) self.check_error( "Collapsing defaults for 'drawer':\n" "Collapsing section named 'thing':\n" "Failed collapsing section key 'content':\n" "Failed converting argument 'class' to callable:\n" "'spork' is not callable", manager.get_default, 'drawer') self.check_error( "Collapsing defaults for 'broken':\n" "Collapsing section named 'thing':\n" "Failed collapsing section key 'content':\n" "Failed converting argument 'class' to callable:\n" "'spork' is not callable", manager.get_default, 'broken')
def test_self_inherit(self): section = basics.HardCodedConfigSection({'inherit': ['self']}) manager = central.ConfigManager([{ 'self': basics.ConfigSectionFromStringDict({ 'class': 'pkgcore.test.config.test_central.drawer', 'inherit': 'self' }), }], [RemoteSource()]) self.check_error( "Collapsing section named 'self':\n" "Self-inherit 'self' cannot be found", self.get_config_obj, manager, 'drawer', 'self') self.check_error("Self-inherit 'self' cannot be found", manager.collapse_section, [section]) manager = central.ConfigManager([{ 'self': basics.HardCodedConfigSection({ 'inherit': ['self'], }) }, { 'self': basics.HardCodedConfigSection({ 'inherit': ['self'], }) }, { 'self': basics.HardCodedConfigSection({'class': drawer}) }]) self.assertTrue(manager.collapse_named_section('self')) self.assertTrue(manager.collapse_section([section]))
def test_raises(self): def myrepo(): raise ValueError('I raised') manager = central.ConfigManager([{ 'myrepo': basics.HardCodedConfigSection({'class': myrepo}) }]) self.check_error( "Failed instantiating section 'myrepo':\n" "Failed instantiating section 'myrepo': exception caught from 'pkgcore.test.config.test_central.myrepo':\n" "I raised", self.get_config_obj, manager, 'myrepo', 'myrepo') manager = central.ConfigManager( [{ 'myrepo': basics.HardCodedConfigSection({'class': myrepo}) }], debug=True) self.check_error( "Failed instantiating section 'myrepo':\n" "Failed instantiating section 'myrepo': exception caught from 'pkgcore.test.config.test_central.myrepo':\n" "I raised", self.get_config_obj, manager, 'myrepo', 'myrepo', klass=errors.ConfigurationError)
def test_list_prepend(self): @configurable({'seq': 'list'}) def seq(seq): return seq manager = central.ConfigManager([{ 'inh': basics.HardCodedConfigSection({ 'inherit': ['sect'], 'seq.prepend': ['pre'], }), 'sect': basics.HardCodedConfigSection({ 'inherit': ['base'], 'seq': ['1', '2'], }) }, { 'base': basics.HardCodedConfigSection({ 'class': seq, 'seq.prepend': ['-1'], 'seq.append': ['post'], }) }]) self.assertEqual(['-1', 'post'], manager.objects.seq['base']) self.assertEqual(['1', '2'], manager.objects.seq['sect']) self.assertEqual(['pre', '1', '2'], manager.objects.seq['inh'])
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_reinstantiate_after_raise(self): # The most likely bug this tests for is attempting to # reprocess already processed section_ref args. spork = object() @configurable({'thing': 'ref:spork'}) def myrepo(thing): self.assertIdentical(thing, spork) raise errors.ComplexInstantiationError('I suck') @configurable(typename='spork') def spork_producer(): return spork manager = central.ConfigManager( [{'spork': basics.HardCodedConfigSection({ 'class': myrepo, 'thing': basics.HardCodedConfigSection({ 'class': spork_producer, }), })}]) spork = manager.collapse_named_section('spork') for i in range(3): self.check_error( "Failed instantiating section 'spork':\n" "Failed instantiating section 'spork': exception caught from 'pkgcore.test.config.test_central.myrepo':\n" "'I suck', callable unset!", spork.instantiate) for i in range(3): self.check_error( "Failed instantiating section 'spork':\n" "Failed instantiating section 'spork': exception caught from 'pkgcore.test.config.test_central.myrepo':\n" "'I suck', callable unset!", manager.collapse_named_section('spork').instantiate)
def test_str_prepend(self): @configurable({'string': 'str'}) def sect(string): return string manager = central.ConfigManager([{ 'inh': basics.HardCodedConfigSection({ 'inherit': ['sect'], 'string.prepend': 'pre', }), 'sect': basics.HardCodedConfigSection({ 'inherit': ['base'], 'string': 'b', }) }, { 'base': basics.HardCodedConfigSection({ 'class': sect, 'string.prepend': 'a', 'string.append': 'c', }) }]) self.assertEqual('a c', manager.objects.sect['base']) self.assertEqual('b', manager.objects.sect['sect']) self.assertEqual('pre b', manager.objects.sect['inh'])
def test_sections(self): manager = central.ConfigManager( [{'fooinst': basics.HardCodedConfigSection({'class': repo}), 'barinst': basics.HardCodedConfigSection({'class': drawer}), }]) self.assertEqual(['barinst', 'fooinst'], sorted(manager.sections())) self.assertEqual(manager.objects.drawer.keys(), ['barinst']) self.assertEqual(manager.objects.drawer, {'barinst': (None, None)})
def test_typecheck(self): @configurable({'myrepo': 'ref:repo'}, typename='repo') def reporef(myrepo=None): return myrepo @configurable({'myrepo': 'refs:repo'}, typename='repo') def reporefs(myrepo=None): return myrepo @configurable(typename='repo') def myrepo(): return 'repo!' manager = central.ConfigManager([{ 'myrepo': basics.HardCodedConfigSection({'class': myrepo}), 'drawer': basics.HardCodedConfigSection({'class': drawer}), 'right': basics.AutoConfigSection({ 'class': reporef, 'myrepo': 'myrepo' }), 'wrong': basics.AutoConfigSection({ 'class': reporef, 'myrepo': 'drawer' }), }]) self.check_error( "Collapsing section named 'wrong':\n" "Failed collapsing section key 'myrepo':\n" "reference 'drawer' should be of type 'repo', got 'drawer'", self.get_config_obj, manager, 'repo', 'wrong') self.assertEqual('repo!', manager.objects.repo['right']) manager = central.ConfigManager([{ 'myrepo': basics.HardCodedConfigSection({'class': myrepo}), 'drawer': basics.HardCodedConfigSection({'class': drawer}), 'right': basics.AutoConfigSection({ 'class': reporefs, 'myrepo': 'myrepo' }), 'wrong': basics.AutoConfigSection({ 'class': reporefs, 'myrepo': 'drawer' }), }]) self.check_error( "Collapsing section named 'wrong':\n" "Failed collapsing section key 'myrepo':\n" "reference 'drawer' should be of type 'repo', got 'drawer'", self.get_config_obj, manager, 'repo', 'wrong') self.assertEqual(['repo!'], manager.objects.repo['right'])
def test_serialise(self): nest = basics.HardCodedConfigSection({'class': pseudospork}) self.assertOut( ["'spork' {", ' # typename of this section: multi', ' class pkgcore.test.scripts.test_pconfig.multi;', ' # type: bool', ' boolean True;', ' # type: callable', ' callable pkgcore.test.scripts.test_pconfig.multi;', ' # type: lazy_ref:spork', ' lazy_ref {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' };', ' # type: lazy_refs:spork', ' lazy_refs {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' } {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' };', ' # type: list', " list 'a' 'b\\' \"c';", ' # type: ref:spork', ' ref {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' };', ' # type: refs:spork', ' refs {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' } {', ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', ' };', ' # type: str', ' string \'it is a "stringy" \\\'string\\\'\';', ' # type: str', " unknown 'random';", '}', ''], spork=basics.HardCodedConfigSection({ 'class': multi, 'string': 'it is a "stringy" \'string\'', 'boolean': True, 'list': ['a', 'b\' "c'], 'callable': multi, 'ref': nest, 'lazy_ref': nest, 'refs': [nest, nest], 'lazy_refs': [nest, nest], 'unknown': 'random', }))
def autoloader(): return { 'autoload-sub': basics.HardCodedConfigSection({'class': autoloader}), 'spork': basics.HardCodedConfigSection({ 'class': repo, 'cache': 'test' }) }
def test_one_typename(self): self.assertOut( ["'spork' {", ' # typename of this section: spork', ' class pkgcore.test.scripts.test_pconfig.pseudospork;', '}', '', ], 'spork', spork=basics.HardCodedConfigSection({'class': pseudospork}), foon=basics.HardCodedConfigSection({'class': foon}), )
def test_inherit_only(self): manager = central.ConfigManager([{ 'source': basics.HardCodedConfigSection({ 'class': drawer, 'inherit-only': True, }), 'target': basics.HardCodedConfigSection({ 'inherit': ['source'], })}], [RemoteSource()]) self.check_error( "Collapsing section named 'source':\n" 'cannot collapse inherit-only section', manager.collapse_named_section, 'source') self.assertTrue(manager.collapse_named_section('target'))
def test_inherited_default(self): manager = central.ConfigManager([{ 'default': basics.HardCodedConfigSection({ 'default': True, 'inherit': ['basic'], }), 'uncollapsable': basics.HardCodedConfigSection({ 'default': True, 'inherit': ['spork'], 'inherit-only': True, }), 'basic': basics.HardCodedConfigSection({'class': drawer}), }], [RemoteSource()]) self.assertTrue(manager.get_default('drawer'))
def test_inherit_unknown_type(self): manager = central.ConfigManager( [{'baserepo': basics.HardCodedConfigSection({ 'cache': 'available', }), 'actual repo': basics.HardCodedConfigSection({ 'class': drawer, 'inherit': ['baserepo'], }), }]) self.check_error( "Collapsing section named 'actual repo':\n" "Type of 'cache' unknown", self.get_config_obj, manager, 'repo', 'actual repo')
def test_inherit(self): manager = central.ConfigManager( [{'baserepo': basics.HardCodedConfigSection({ 'cache': 'available', 'inherit': ['unneeded'], }), 'unneeded': basics.HardCodedConfigSection({ 'cache': 'unavailable'}), 'actual repo': basics.HardCodedConfigSection({ 'class': repo, 'inherit': ['baserepo'], }), }]) self.assertEqual('available', manager.objects.repo['actual repo'])
def test_no_class(self): manager = central.ConfigManager( [{'foo': basics.HardCodedConfigSection({})}]) self.check_error( "Collapsing section named 'foo':\n" 'no class specified', manager.collapse_named_section, 'foo')
def test_uncollapsable(self): self.assertOut('', spork=basics.HardCodedConfigSection({ 'class': foon, 'broken': True, 'inherit-only': True }))
def test_section_names(self): manager = central.ConfigManager( [{ 'thing': basics.HardCodedConfigSection({'class': drawer}), }], [RemoteSource()]) collapsed = manager.collapse_named_section('thing') self.assertEqual('thing', collapsed.name)
def test_contains(self): manager = central.ConfigManager( [{ 'spork': basics.HardCodedConfigSection({'class': drawer}) }], [RemoteSource()]) self.assertIn('spork', manager.objects.drawer) self.assertNotIn('foon', manager.objects.drawer)
def test_autoload_wrong_type(self): self.check_error( "Section 'autoload_wrong' is marked as autoload but type is " 'drawer, not configsection', central.ConfigManager, [{ 'autoload_wrong': basics.HardCodedConfigSection({'class': drawer}) }])
def test_reload(self): mod_dict = {'class': repo, 'cache': 'test'} @configurable(typename='configsection') def autoloader(): return {'spork': basics.HardCodedConfigSection(mod_dict)} manager = central.ConfigManager([{ 'autoload-sub': basics.HardCodedConfigSection({'class': autoloader}) }]) self.assertEqual(set(['autoload-sub', 'spork']), set(manager.sections())) self.assertEqual(['spork'], manager.objects.repo.keys()) collapsedspork = manager.collapse_named_section('spork') self.assertEqual('test', collapsedspork.instantiate()) mod_dict['cache'] = 'modded' self.assertIdentical(collapsedspork, manager.collapse_named_section('spork')) self.assertEqual('test', collapsedspork.instantiate()) types = manager.types manager.reload() newspork = manager.collapse_named_section('spork') self.assertNotIdentical(collapsedspork, newspork) self.assertEqual('modded', newspork.instantiate(), 'it did not throw away the cached instance') self.assertNotIdentical(types, manager.types)
def test_instantiate_default_ref(self): manager = central.ConfigManager( [{ 'spork': basics.HardCodedConfigSection({'class': drawer}) }], ) self.assertEqual((None, None), manager.collapse_named_section('spork').instantiate())
def render_value(self, central, name, arg_type): if name != 'sects': raise KeyError(name) if arg_type != 'repr': raise errors.ConfigurationError('%r unsupported' % (arg_type,)) return 'refs', [ ['spork', basics.HardCodedConfigSection({'foo': 'bar'})], None, None]
def test_uncollapsable(self): self.assertOut( ["section foon:", " Collapsing section named 'foon'", " cannot collapse inherit-only section" "", "", "section spork:", " Collapsing section named 'spork'", " type pkgcore.test.scripts.test_pconfig.spork needs settings for 'reff'" "", "", ], spork=basics.HardCodedConfigSection({'class': spork}), foon=basics.HardCodedConfigSection({'class': spork, 'inherit-only': True}), )
def test_unknown_type(self): manager = central.ConfigManager( [{'spork': basics.HardCodedConfigSection({'class': drawer, 'foon': None})}]) self.check_error( "Collapsing section named 'spork':\n" "Type of 'foon' unknown", manager.collapse_named_section, 'spork')
def test_dump(self): self.assertOut( ["'spork' {", ' # typename of this section: foon', ' class pkgcore.test.scripts.test_pconfig.foon;', '}', ''], spork=basics.HardCodedConfigSection({'class': foon}))
def test_parser(self): self.assertError('too few arguments', 'spork') self.assertError("argument source: couldn't find cache 'spork'", 'spork', 'spork2') self.assertError("argument target: couldn't find cache 'spork2'", 'spork', 'spork2', spork=basics.HardCodedConfigSection({'class': Cache})) self.assertError("argument target: cache 'spork2' is readonly", 'spork', 'spork2', spork=basics.HardCodedConfigSection({ 'class': Cache, }), spork2=basics.HardCodedConfigSection({ 'class': Cache, }))
def test_section_ref(self): ref = basics.HardCodedConfigSection({}) self.assertRaises(errors.ConfigurationError, basics.convert_asis, None, 42, 'ref:spoon') self.assertIdentical( ref, basics.convert_asis(None, ref, 'ref:spoon').section) self.assertEqual(('ref', ref), basics.convert_asis(None, ref, 'repr'))