| OLD | NEW |
| (Empty) |
| 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@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 import logging | |
| 24 import unittest | |
| 25 | |
| 26 from webkitpy.common.system.logtesting import TestLogStream | |
| 27 from webkitpy.common.system.logutils import configure_logging | |
| 28 | |
| 29 | |
| 30 class ConfigureLoggingTestBase(unittest.TestCase): | |
| 31 """Base class for configure_logging() unit tests.""" | |
| 32 | |
| 33 def _logging_level(self): | |
| 34 raise Exception("Not implemented.") | |
| 35 | |
| 36 def setUp(self): | |
| 37 log_stream = TestLogStream(self) | |
| 38 | |
| 39 # Use a logger other than the root logger or one prefixed with | |
| 40 # "webkitpy." so as not to conflict with test-webkitpy logging. | |
| 41 logger = logging.getLogger("unittest") | |
| 42 | |
| 43 # Configure the test logger not to pass messages along to the | |
| 44 # root logger. This prevents test messages from being | |
| 45 # propagated to loggers used by test-webkitpy logging (e.g. | |
| 46 # the root logger). | |
| 47 logger.propagate = False | |
| 48 | |
| 49 logging_level = self._logging_level() | |
| 50 self._handlers = configure_logging(logging_level=logging_level, logger=l
ogger, stream=log_stream) | |
| 51 self._log = logger | |
| 52 self._log_stream = log_stream | |
| 53 | |
| 54 def tearDown(self): | |
| 55 """Reset logging to its original state. | |
| 56 | |
| 57 This method ensures that the logging configuration set up | |
| 58 for a unit test does not affect logging in other unit tests. | |
| 59 """ | |
| 60 logger = self._log | |
| 61 for handler in self._handlers: | |
| 62 logger.removeHandler(handler) | |
| 63 | |
| 64 def _assert_log_messages(self, messages): | |
| 65 """Assert that the logged messages equal the given messages.""" | |
| 66 self._log_stream.assertMessages(messages) | |
| 67 | |
| 68 | |
| 69 class ConfigureLoggingTest(ConfigureLoggingTestBase): | |
| 70 | |
| 71 """Tests configure_logging() with the default logging level.""" | |
| 72 | |
| 73 def _logging_level(self): | |
| 74 return None | |
| 75 | |
| 76 def test_info_message(self): | |
| 77 self._log.info("test message") | |
| 78 self._assert_log_messages(["test message\n"]) | |
| 79 | |
| 80 def test_debug_message(self): | |
| 81 self._log.debug("test message") | |
| 82 self._assert_log_messages([]) | |
| 83 | |
| 84 def test_below_threshold_message(self): | |
| 85 # We test the boundary case of a logging level equal to 19. | |
| 86 # In practice, we will probably only be calling log.debug(), | |
| 87 # which corresponds to a logging level of 10. | |
| 88 level = logging.INFO - 1 # Equals 19. | |
| 89 self._log.log(level, "test message") | |
| 90 self._assert_log_messages([]) | |
| 91 | |
| 92 def test_two_messages(self): | |
| 93 self._log.info("message1") | |
| 94 self._log.info("message2") | |
| 95 self._assert_log_messages(["message1\n", | |
| 96 "message2\n"]) | |
| 97 | |
| 98 | |
| 99 class ConfigureLoggingVerboseTest(ConfigureLoggingTestBase): | |
| 100 | |
| 101 def _logging_level(self): | |
| 102 return logging.DEBUG | |
| 103 | |
| 104 def test_info_message(self): | |
| 105 self._log.info("test message") | |
| 106 self._assert_log_messages(["unittest: [INFO] test message\n"]) | |
| 107 | |
| 108 def test_debug_message(self): | |
| 109 self._log.debug("test message") | |
| 110 self._assert_log_messages(["unittest: [DEBUG] test message\n"]) | |
| 111 | |
| 112 | |
| 113 class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase): | |
| 114 | |
| 115 """Tests configure_logging() with a custom logging level.""" | |
| 116 | |
| 117 _level = 36 | |
| 118 | |
| 119 def _logging_level(self): | |
| 120 return self._level | |
| 121 | |
| 122 def test_logged_message(self): | |
| 123 self._log.log(self._level, "test message") | |
| 124 self._assert_log_messages(["test message\n"]) | |
| 125 | |
| 126 def test_below_threshold_message(self): | |
| 127 self._log.log(self._level - 1, "test message") | |
| 128 self._assert_log_messages([]) | |
| OLD | NEW |