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