| 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 | |
| 30 from webkitpy.common.system.logtesting import TestLogStream | 29 from webkitpy.common.system.logtesting import TestLogStream |
| 31 from webkitpy.common.system import logutils | 30 from webkitpy.common.system import logutils |
| 32 | 31 |
| 33 | 32 |
| 34 class GetLoggerTest(unittest.TestCase): | 33 class GetLoggerTest(unittest.TestCase): |
| 35 | |
| 36 """Tests get_logger().""" | 34 """Tests get_logger().""" |
| 37 | 35 |
| 38 def test_get_logger_in_webkitpy(self): | 36 def test_get_logger_in_webkitpy(self): |
| 39 logger = logutils.get_logger(__file__) | 37 logger = logutils.get_logger(__file__) |
| 40 self.assertEqual(logger.name, "webkitpy.common.system.logutils_unittest"
) | 38 self.assertEqual(logger.name, "webkitpy.common.system.logutils_unittest"
) |
| 41 | 39 |
| 42 def test_get_logger_not_in_webkitpy(self): | 40 def test_get_logger_not_in_webkitpy(self): |
| 43 # Temporarily change the working directory so that we | 41 # Temporarily change the working directory so that we |
| 44 # can test get_logger() for a path outside of webkitpy. | 42 # can test get_logger() for a path outside of webkitpy. |
| 45 working_directory = os.getcwd() | 43 working_directory = os.getcwd() |
| 46 root_dir = "/" | 44 root_dir = "/" |
| 47 os.chdir(root_dir) | 45 os.chdir(root_dir) |
| 48 | 46 |
| 49 logger = logutils.get_logger("/Tools/Scripts/test-webkitpy") | 47 logger = logutils.get_logger("/Tools/Scripts/test-webkitpy") |
| 50 self.assertEqual(logger.name, "test-webkitpy") | 48 self.assertEqual(logger.name, "test-webkitpy") |
| 51 | 49 |
| 52 logger = logutils.get_logger("/Tools/Scripts/test-webkitpy.py") | 50 logger = logutils.get_logger("/Tools/Scripts/test-webkitpy.py") |
| 53 self.assertEqual(logger.name, "test-webkitpy") | 51 self.assertEqual(logger.name, "test-webkitpy") |
| 54 | 52 |
| 55 os.chdir(working_directory) | 53 os.chdir(working_directory) |
| 56 | 54 |
| 57 | 55 |
| 58 class ConfigureLoggingTestBase(unittest.TestCase): | 56 class ConfigureLoggingTestBase(unittest.TestCase): |
| 59 | |
| 60 """Base class for configure_logging() unit tests.""" | 57 """Base class for configure_logging() unit tests.""" |
| 61 | 58 |
| 62 def _logging_level(self): | 59 def _logging_level(self): |
| 63 raise Exception("Not implemented.") | 60 raise Exception("Not implemented.") |
| 64 | 61 |
| 65 def setUp(self): | 62 def setUp(self): |
| 66 log_stream = TestLogStream(self) | 63 log_stream = TestLogStream(self) |
| 67 | 64 |
| 68 # Use a logger other than the root logger or one prefixed with | 65 # Use a logger other than the root logger or one prefixed with |
| 69 # "webkitpy." so as not to conflict with test-webkitpy logging. | 66 # "webkitpy." so as not to conflict with test-webkitpy logging. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 80 logger=logger, | 77 logger=logger, |
| 81 stream=log_stream) | 78 stream=log_stream) |
| 82 self._log = logger | 79 self._log = logger |
| 83 self._log_stream = log_stream | 80 self._log_stream = log_stream |
| 84 | 81 |
| 85 def tearDown(self): | 82 def tearDown(self): |
| 86 """Reset logging to its original state. | 83 """Reset logging to its original state. |
| 87 | 84 |
| 88 This method ensures that the logging configuration set up | 85 This method ensures that the logging configuration set up |
| 89 for a unit test does not affect logging in other unit tests. | 86 for a unit test does not affect logging in other unit tests. |
| 90 | |
| 91 """ | 87 """ |
| 92 logger = self._log | 88 logger = self._log |
| 93 for handler in self._handlers: | 89 for handler in self._handlers: |
| 94 logger.removeHandler(handler) | 90 logger.removeHandler(handler) |
| 95 | 91 |
| 96 def _assert_log_messages(self, messages): | 92 def _assert_log_messages(self, messages): |
| 97 """Assert that the logged messages equal the given messages.""" | 93 """Assert that the logged messages equal the given messages.""" |
| 98 self._log_stream.assertMessages(messages) | 94 self._log_stream.assertMessages(messages) |
| 99 | 95 |
| 100 | 96 |
| 101 class ConfigureLoggingTest(ConfigureLoggingTestBase): | 97 class ConfigureLoggingTest(ConfigureLoggingTestBase): |
| 102 | |
| 103 """Tests configure_logging() with the default logging level.""" | 98 """Tests configure_logging() with the default logging level.""" |
| 104 | 99 |
| 105 def _logging_level(self): | 100 def _logging_level(self): |
| 106 return None | 101 return None |
| 107 | 102 |
| 108 def test_info_message(self): | 103 def test_info_message(self): |
| 109 self._log.info("test message") | 104 self._log.info("test message") |
| 110 self._assert_log_messages(["test message\n"]) | 105 self._assert_log_messages(["test message\n"]) |
| 111 | 106 |
| 112 def test_debug_message(self): | 107 def test_debug_message(self): |
| 113 self._log.debug("test message") | 108 self._log.debug("test message") |
| 114 self._assert_log_messages([]) | 109 self._assert_log_messages([]) |
| 115 | 110 |
| 116 def test_below_threshold_message(self): | 111 def test_below_threshold_message(self): |
| 117 # We test the boundary case of a logging level equal to 19. | 112 # We test the boundary case of a logging level equal to 19. |
| 118 # In practice, we will probably only be calling log.debug(), | 113 # In practice, we will probably only be calling log.debug(), |
| 119 # which corresponds to a logging level of 10. | 114 # which corresponds to a logging level of 10. |
| 120 level = logging.INFO - 1 # Equals 19. | 115 level = logging.INFO - 1 # Equals 19. |
| 121 self._log.log(level, "test message") | 116 self._log.log(level, "test message") |
| 122 self._assert_log_messages([]) | 117 self._assert_log_messages([]) |
| 123 | 118 |
| 124 def test_two_messages(self): | 119 def test_two_messages(self): |
| 125 self._log.info("message1") | 120 self._log.info("message1") |
| 126 self._log.info("message2") | 121 self._log.info("message2") |
| 127 self._assert_log_messages(["message1\n", | 122 self._assert_log_messages(["message1\n", "message2\n"]) |
| 128 "message2\n"]) | |
| 129 | 123 |
| 130 | 124 |
| 131 class ConfigureLoggingVerboseTest(ConfigureLoggingTestBase): | 125 class ConfigureLoggingVerboseTest(ConfigureLoggingTestBase): |
| 132 | 126 |
| 133 def _logging_level(self): | 127 def _logging_level(self): |
| 134 return logging.DEBUG | 128 return logging.DEBUG |
| 135 | 129 |
| 136 def test_info_message(self): | 130 def test_info_message(self): |
| 137 self._log.info("test message") | 131 self._log.info("test message") |
| 138 self._assert_log_messages(["unittest: [INFO] test message\n"]) | 132 self._assert_log_messages(["unittest: [INFO] test message\n"]) |
| 139 | 133 |
| 140 def test_debug_message(self): | 134 def test_debug_message(self): |
| 141 self._log.debug("test message") | 135 self._log.debug("test message") |
| 142 self._assert_log_messages(["unittest: [DEBUG] test message\n"]) | 136 self._assert_log_messages(["unittest: [DEBUG] test message\n"]) |
| 143 | 137 |
| 144 | 138 |
| 145 class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase): | 139 class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase): |
| 146 | |
| 147 """Tests configure_logging() with a custom logging level.""" | 140 """Tests configure_logging() with a custom logging level.""" |
| 148 | 141 |
| 149 _level = 36 | 142 _level = 36 |
| 150 | 143 |
| 151 def _logging_level(self): | 144 def _logging_level(self): |
| 152 return self._level | 145 return self._level |
| 153 | 146 |
| 154 def test_logged_message(self): | 147 def test_logged_message(self): |
| 155 self._log.log(self._level, "test message") | 148 self._log.log(self._level, "test message") |
| 156 self._assert_log_messages(["test message\n"]) | 149 self._assert_log_messages(["test message\n"]) |
| 157 | 150 |
| 158 def test_below_threshold_message(self): | 151 def test_below_threshold_message(self): |
| 159 self._log.log(self._level - 1, "test message") | 152 self._log.log(self._level - 1, "test message") |
| 160 self._assert_log_messages([]) | 153 self._assert_log_messages([]) |
| OLD | NEW |