def test_no_schema(self): sio = StringIO() with support.stderr_replaced(sio): with self.assertRaises(SystemExit) as cm: run_validator() self.assertEqual(cm.exception.code, 2) err = sio.getvalue() # Checked separately since these are included very differently # with different versions of Python's argparse module. self.assertIn('-s/--schema', err) self.assertIn(' required', err)
def test_simple_import_with_cache(self): loader = ZConfig.loader.SchemaLoader() url1 = ZConfig.url.urljoin(CONFIG_BASE, "library.xml") schema1 = loader.loadURL(url1) sio = StringIO("<schema>" " <import src='library.xml'/>" " <section type='type-a' name='section'/>" "</schema>") url2 = ZConfig.url.urljoin(CONFIG_BASE, "stringio") schema2 = loader.loadFile(sio, url2) self.assertTrue(schema1.gettype("type-a") is schema2.gettype("type-a"))
def test_includes_with_defines(self): self.schema = ZConfig.loadSchemaFile( StringIO("""\ <schema> <key name='refinner' /> <key name='refouter' /> </schema> """)) conf = self.load("outer.conf") self.assertEqual(conf.refinner, "inner") self.assertEqual(conf.refouter, "outer")
def test_simple_import(self): schema = self.load_schema_text("<schema/>") loader = self.create_config_loader(schema) config, _ = loader.loadFile( StringIO("%import ZConfig.tests.library.widget\n")) # make sure we now have a "private" schema object; the only # way to get it is from the loader itself self.assertTrue(schema is not loader.schema) # make sure component types are only found on the private schema: loader.schema.gettype("widget-b") self.assertRaises(ZConfig.SchemaError, schema.gettype, "widget-b")
def test_import_component_twice_1(self): # Make sure we can import a component twice from a schema. # This is most likely to occur when the component is imported # from each of two other components, or from the top-level # schema and a component. loader = ZConfig.loader.SchemaLoader() sio = StringIO("<schema>" " <import package='ZConfig.tests.library.widget' />" " <import package='ZConfig.tests.library.widget' />" "</schema>") schema = loader.loadFile(sio) schema.gettype("widget-a")
def test_zip_import_component_from_schema(self): sio = StringIO(''' <schema> <abstracttype name="something"/> <import package="foo.sample"/> <section name="*" attribute="something" type="something" /> </schema> ''') schema = ZConfig.loadSchemaFile(sio) t = schema.gettype("sample") self.assertFalse(t.isabstract())
def test_import_from_package_with_missing_file(self): loader = ZConfig.loader.SchemaLoader() sio = StringIO("<schema>" " <import package='ZConfig.tests.library.widget'" " file='notthere.xml' />" "</schema>") with self.assertRaises(ZConfig.SchemaResourceError) as ctx: loader.loadFile(sio) e = ctx.exception self.assertEqual(e.filename, "notthere.xml") self.assertEqual(e.package, "ZConfig.tests.library.widget") self.assertTrue(e.path) # make sure the str() doesn't raise an unexpected exception str(e)
def check_standard_stream(self, name): old_stream = getattr(sys, name) conf = self.get_config(""" <eventlog> <logfile> level info path %s </logfile> </eventlog> """ % name.upper()) self.assertTrue(conf.eventlog is not None) # The factory has already been created; make sure it picks up # the stderr we set here when we create the logger and # handlers: sio = StringIO() setattr(sys, name, sio) try: logger = conf.eventlog() finally: setattr(sys, name, old_stream) logger.warning("woohoo!") self.assertTrue(sio.getvalue().find("woohoo!") >= 0)
def test_custom_formatter(self): old_stream = sys.stdout conf = self.get_config(""" <eventlog> <logfile> formatter ZConfig.components.logger.tests.test_logger.CustomFormatter level info path STDOUT </logfile> </eventlog> """) sio = StringIO() sys.stdout = sio try: logger = conf.eventlog() finally: sys.stdout = old_stream try: raise KeyError except KeyError: logger.exception("testing a KeyError") self.assertTrue(sio.getvalue().find("KeyError") >= 0) self.assertTrue(sio.getvalue().find("Don't panic") >= 0)
def test_custom_formatter(self): clsname = __name__ + '.CustomFormatter' old_stream = sys.stdout sio = StringIO() sys.stdout = sio try: conf = self.get_config(""" <eventlog> <logfile> formatter %s level info path STDOUT </logfile> </eventlog> """ % clsname) logger = conf.eventlog() finally: sys.stdout = old_stream try: raise KeyError except KeyError: logger.exception("testing a KeyError") self.assertTrue(sio.getvalue().find("KeyError") >= 0) self.assertTrue(sio.getvalue().find("Don't panic") >= 0)
def test_missing_import(self): schema = self.load_schema_text("<schema/>") loader = self.create_config_loader(schema) self.assertRaises(ZConfig.SchemaError, loader.loadFile, StringIO("%import ZConfig.tests.missing\n"))
def test_repeated_import(self): schema = self.load_schema_text("<schema/>") loader = self.create_config_loader(schema) config, _ = loader.loadFile( StringIO("%import ZConfig.tests.library.widget\n" "%import ZConfig.tests.library.widget\n"))
def load_schema_text(self, text, url=None): sio = StringIO(text) self.schema = ZConfig.loadSchemaFile(sio, url) return self.schema
def load_config_text(self, schema, text, num_handlers=0, url=None): sio = StringIO(text) loader = self.create_config_loader(schema) self.conf, self.handlers = loader.loadFile(sio, url) self.assertEqual(len(self.handlers), num_handlers) return self.conf
def get_schema(self): if self._schema is None: sio = StringIO(self._schematext) self.__class__._schema = ZConfig.loadSchemaFile(sio) return self._schema
def test_load_from_fileobj(self): sio = StringIO("%define name value\n" "getname x $name y \n") cf = self.loadfile(sio) self.assertEqual(cf.getname, "x value y")
def loadtext(self, text): sio = StringIO(text) return self.loadfile(sio)
def get_config(self, text): conf, handler = ZConfig.loadConfigFile(self.get_schema(), StringIO(text)) self.assertTrue(not handler) return conf