def test_bad_config(self): sio = StringIO() with support.stderr_replaced(sio): res = run_validator("--schema", support.input_file("simple.xml"), support.input_file("outer.conf")) self.assertEqual(res, 1) self.assertIn("'refouter' is not a known key name", sio.getvalue())
def formatException(self, ei): """Format and return the exception information as a string. This adds helpful advice to the end of the traceback. """ import traceback sio = StringIO() traceback.print_exception(ei[0], ei[1], ei[2], file=sio) return sio.getvalue() + "... Don't panic!"
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 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)