コード例 #1
0
class LogPathTests(LocationTests, unittest.TestCase):

    def setUp(self):
        super().setUp()
        uid = uuid.uuid4()
        self.path = pathlib.Path(self.locn.name, f"{uid.hex}.log")
        self.manager = LogManager(
            self.path,
            defaults=[LogManager.Route(None, Logger.Level.INFO, LogAdapter(), self.path)]
        )
        self.manager.__enter__()

    def tearDown(self):
        self.manager.__exit__(None, None, None)
        super().tearDown()

    def test_register_path(self):
        d = {}
        rv = self.manager.register_endpoint(self.path, registry=d)
        self.assertIs(rv, self.path)
        self.assertIn(self.path, d)
        self.assertIsInstance(d[self.path], io.TextIOBase)

    def test_log_written(self):
        logger = self.manager.get_logger("unit.test.log")
        logger.log(logger.Level.INFO, "Info message")
        self.assertIn(self.path, self.manager.endings)
        self.assertTrue(self.path.exists())
        self.assertIn("Info message", self.path.read_text())
        self.assertTrue(any(
            self.path.resolve() == k.endpoint_name for k, v in self.manager.pairings
        ), self.manager.pairings)
コード例 #2
0
class EndpointRegistrationTests(unittest.TestCase):

    def setUp(self):
        self.manager = LogManager()
        self.assertIn(sys.stderr, self.manager.endings.values())

    def tearDown(self):
        self.manager.loggers.clear()
        self.manager.routing.clear()
        self.manager.endings.clear()

    def test_register_stderr(self):
        d = {}
        rv = self.manager.register_endpoint(sys.stderr, registry=d)
        self.assertEqual(d, {sys.stderr.name: sys.stderr})

    def test_register_stream(self):
        stream = io.StringIO()
        d = {}
        rv = self.manager.register_endpoint(stream, registry=d)
        self.assertEqual(d, {id(stream): stream})