diff options
Diffstat (limited to 'lib/fixtures')
-rw-r--r-- | lib/fixtures/_fixtures/logger.py | 17 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_logger.py | 23 |
2 files changed, 38 insertions, 2 deletions
diff --git a/lib/fixtures/_fixtures/logger.py b/lib/fixtures/_fixtures/logger.py index 4b85b22..a00bba4 100644 --- a/lib/fixtures/_fixtures/logger.py +++ b/lib/fixtures/_fixtures/logger.py @@ -13,7 +13,7 @@ # license you chose for the specific language governing permissions and # limitations under that license. -from logging import StreamHandler, getLogger, INFO, Formatter +from logging import StreamHandler, getLogger, INFO, Formatter, Handler from cStringIO import StringIO from testtools.content import Content @@ -82,3 +82,18 @@ class FakeLogger(Fixture): LoggerFixture = FakeLogger + + +class MementoHandler(Handler): + """A handler class which stores logging records in a list. + + From http://nessita.pastebin.com/mgc85uQT + """ + def __init__(self, *args, **kwargs): + """Create the instance, and add a records attribute.""" + Handler.__init__(self, *args, **kwargs) + self.records = [] + + def emit(self, record): + """Just add the record to self.records.""" + self.records.append(record) diff --git a/lib/fixtures/tests/_fixtures/test_logger.py b/lib/fixtures/tests/_fixtures/test_logger.py index 9b88b5f..b1e4615 100644 --- a/lib/fixtures/tests/_fixtures/test_logger.py +++ b/lib/fixtures/tests/_fixtures/test_logger.py @@ -18,7 +18,11 @@ import logging from testtools import TestCase from cStringIO import StringIO -from fixtures import FakeLogger, TestWithFixtures +from fixtures import ( + FakeLogger, + TestWithFixtures, + ) +from fixtures._fixtures.logger import MementoHandler class FakeLoggerTest(TestCase, TestWithFixtures): @@ -90,3 +94,20 @@ class FakeLoggerTest(TestCase, TestWithFixtures): self.assertEqual("some message\n", content.as_text()) # A new one returns the new output: self.assertEqual("", fixture.getDetails()[detail_name].as_text()) + + +class TestMementoHandler(TestCase): + + def test_initialy_no_records(self): + handler = MementoHandler() + self.assertEqual([], handler.records) + + def test_emit_stored_in_records(self): + handler = MementoHandler() + marker = object() + handler.emit(marker) + self.assertEqual([marker], handler.records) + + def test_is_log_handler(self): + handler = MementoHandler() + self.assertIsInstance(handler, logging.Handler) |