Home | History | Annotate | Download | only in system
      1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek (at] webkit.org)
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions
      5 # are met:
      6 # 1.  Redistributions of source code must retain the above copyright
      7 #     notice, this list of conditions and the following disclaimer.
      8 # 2.  Redistributions in binary form must reproduce the above copyright
      9 #     notice, this list of conditions and the following disclaimer in the
     10 #     documentation and/or other materials provided with the distribution.
     11 #
     12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
     13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
     16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     22 
     23 """Unit tests for logutils.py."""
     24 
     25 import logging
     26 import os
     27 import webkitpy.thirdparty.unittest2 as unittest
     28 
     29 from webkitpy.common.system.logtesting import LogTesting
     30 from webkitpy.common.system.logtesting import TestLogStream
     31 from webkitpy.common.system import logutils
     32 
     33 
     34 class GetLoggerTest(unittest.TestCase):
     35 
     36     """Tests get_logger()."""
     37 
     38     def test_get_logger_in_webkitpy(self):
     39         logger = logutils.get_logger(__file__)
     40         self.assertEqual(logger.name, "webkitpy.common.system.logutils_unittest")
     41 
     42     def test_get_logger_not_in_webkitpy(self):
     43         # Temporarily change the working directory so that we
     44         # can test get_logger() for a path outside of webkitpy.
     45         working_directory = os.getcwd()
     46         root_dir = "/"
     47         os.chdir(root_dir)
     48 
     49         logger = logutils.get_logger("/Tools/Scripts/test-webkitpy")
     50         self.assertEqual(logger.name, "test-webkitpy")
     51 
     52         logger = logutils.get_logger("/Tools/Scripts/test-webkitpy.py")
     53         self.assertEqual(logger.name, "test-webkitpy")
     54 
     55         os.chdir(working_directory)
     56 
     57 
     58 class ConfigureLoggingTestBase(unittest.TestCase):
     59 
     60     """Base class for configure_logging() unit tests."""
     61 
     62     def _logging_level(self):
     63         raise Exception("Not implemented.")
     64 
     65     def setUp(self):
     66         log_stream = TestLogStream(self)
     67 
     68         # Use a logger other than the root logger or one prefixed with
     69         # "webkitpy." so as not to conflict with test-webkitpy logging.
     70         logger = logging.getLogger("unittest")
     71 
     72         # Configure the test logger not to pass messages along to the
     73         # root logger.  This prevents test messages from being
     74         # propagated to loggers used by test-webkitpy logging (e.g.
     75         # the root logger).
     76         logger.propagate = False
     77 
     78         logging_level = self._logging_level()
     79         self._handlers = logutils.configure_logging(logging_level=logging_level,
     80                                                     logger=logger,
     81                                                     stream=log_stream)
     82         self._log = logger
     83         self._log_stream = log_stream
     84 
     85     def tearDown(self):
     86         """Reset logging to its original state.
     87 
     88         This method ensures that the logging configuration set up
     89         for a unit test does not affect logging in other unit tests.
     90 
     91         """
     92         logger = self._log
     93         for handler in self._handlers:
     94             logger.removeHandler(handler)
     95 
     96     def _assert_log_messages(self, messages):
     97         """Assert that the logged messages equal the given messages."""
     98         self._log_stream.assertMessages(messages)
     99 
    100 
    101 class ConfigureLoggingTest(ConfigureLoggingTestBase):
    102 
    103     """Tests configure_logging() with the default logging level."""
    104 
    105     def _logging_level(self):
    106         return None
    107 
    108     def test_info_message(self):
    109         self._log.info("test message")
    110         self._assert_log_messages(["test message\n"])
    111 
    112     def test_debug_message(self):
    113         self._log.debug("test message")
    114         self._assert_log_messages([])
    115 
    116     def test_below_threshold_message(self):
    117         # We test the boundary case of a logging level equal to 19.
    118         # In practice, we will probably only be calling log.debug(),
    119         # which corresponds to a logging level of 10.
    120         level = logging.INFO - 1  # Equals 19.
    121         self._log.log(level, "test message")
    122         self._assert_log_messages([])
    123 
    124     def test_two_messages(self):
    125         self._log.info("message1")
    126         self._log.info("message2")
    127         self._assert_log_messages(["message1\n",
    128                                    "message2\n"])
    129 
    130 
    131 class ConfigureLoggingVerboseTest(ConfigureLoggingTestBase):
    132     def _logging_level(self):
    133         return logging.DEBUG
    134 
    135     def test_info_message(self):
    136         self._log.info("test message")
    137         self._assert_log_messages(["unittest: [INFO] test message\n"])
    138 
    139     def test_debug_message(self):
    140         self._log.debug("test message")
    141         self._assert_log_messages(["unittest: [DEBUG] test message\n"])
    142 
    143 class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase):
    144 
    145     """Tests configure_logging() with a custom logging level."""
    146 
    147     _level = 36
    148 
    149     def _logging_level(self):
    150         return self._level
    151 
    152     def test_logged_message(self):
    153         self._log.log(self._level, "test message")
    154         self._assert_log_messages(["test message\n"])
    155 
    156     def test_below_threshold_message(self):
    157         self._log.log(self._level - 1, "test message")
    158         self._assert_log_messages([])
    159