Coverage for tests/test_redir.py: 29%
53 statements
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-29 09:56 +0000
« prev ^ index » next coverage.py v7.2.4, created at 2023-04-29 09:56 +0000
1# LSST Data Management System
2# Copyright 2018 LSST Corporation.
3#
4# This product includes software developed by the
5# LSST Project (http://www.lsst.org/).
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the LSST License Statement and
18# the GNU General Public License along with this program. If not,
19# see <http://www.lsstcorp.org/LegalNotices/>.
21"""
22This tests logging redirection to Python streams.
23"""
26import io
27import os
28import shutil
29import tempfile
30import unittest
32import lsst.log as log
33import lsst.log.utils as log_utils
36class TestRedir(unittest.TestCase):
37 class StdoutCapture(object):
38 """
39 Context manager to redirect stdout to a file.
40 """
42 def __init__(self, filename):
43 self.stdout = None
44 self.outputFilename = filename
46 def __enter__(self):
47 self.stdout = os.dup(1)
48 os.close(1)
49 os.open(self.outputFilename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
51 def __exit__(self, type, value, traceback):
52 if self.stdout is not None:
53 os.close(1)
54 os.dup(self.stdout)
55 os.close(self.stdout)
56 self.stdout = None
58 def setUp(self):
59 """Make a temporary directory and a log file in it."""
60 self.tempDir = tempfile.mkdtemp()
61 self.outputFilename = os.path.join(self.tempDir, "log.out")
62 self.stdout = None
64 def tearDown(self):
65 """Remove the temporary directory."""
66 shutil.rmtree(self.tempDir)
68 def check(self, reference):
69 """Compare the log file with the provided reference text."""
70 with open(self.outputFilename, "r") as f:
71 # strip everything up to first ] to remove timestamp and thread ID
72 lines = [ln.split("]")[-1].rstrip("\n") for ln in f.readlines()]
73 reflines = [rl for rl in reference.split("\n") if rl != ""]
74 self.maxDiff = None
75 self.assertListEqual(lines, reflines)
77 ##########################################################################
79 def testRedir(self):
80 """
81 Test redirection to stream.
82 """
83 with TestRedir.StdoutCapture(self.outputFilename):
84 log.configure()
85 dest = io.StringIO()
86 log_utils.enable_notebook_logging(dest)
87 log.log(log.getDefaultLogger().getName(), log.INFO, "This is INFO")
88 log.info(u"This is unicode INFO")
89 log.trace("This is TRACE")
90 log.debug("This is DEBUG")
91 log.warn("This is WARN")
92 log.error("This is ERROR")
93 log.fatal("This is FATAL")
94 log_utils.disable_notebook_logging()
95 log.warn("Format %d %g %s", 3, 2.71828, "foo")
96 self.assertEqual(
97 dest.getvalue(),
98 """root INFO: This is INFO
99root INFO: This is unicode INFO
100root WARN: This is WARN
101root ERROR: This is ERROR
102root FATAL: This is FATAL
103""",
104 )
105 self.check(
106 """
107root WARN: Format 3 2.71828 foo
108"""
109 )
112if __name__ == "__main__": 112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true
113 unittest.main()