|
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 """Unit tests for logutils.py.""" |
|
24 |
|
25 import logging |
|
26 import os |
|
27 import unittest |
|
28 |
|
29 from webkitpy.common.system.logtesting import LogTesting |
|
30 from webkitpy.common.system.logtesting import TestLogStream |
|
31 import webkitpy.common.system.logutils as 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.assertEquals(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("/WebKitTools/Scripts/test-webkitpy") |
|
50 self.assertEquals(logger.name, "test-webkitpy") |
|
51 |
|
52 logger = logutils.get_logger("/WebKitTools/Scripts/test-webkitpy.py") |
|
53 self.assertEquals(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(["unittest: [INFO] test message\n"]) |
|
111 |
|
112 def test_below_threshold_message(self): |
|
113 # We test the boundary case of a logging level equal to 19. |
|
114 # In practice, we will probably only be calling log.debug(), |
|
115 # which corresponds to a logging level of 10. |
|
116 level = logging.INFO - 1 # Equals 19. |
|
117 self._log.log(level, "test message") |
|
118 self._assert_log_messages([]) |
|
119 |
|
120 def test_two_messages(self): |
|
121 self._log.info("message1") |
|
122 self._log.info("message2") |
|
123 self._assert_log_messages(["unittest: [INFO] message1\n", |
|
124 "unittest: [INFO] message2\n"]) |
|
125 |
|
126 |
|
127 class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase): |
|
128 |
|
129 """Tests configure_logging() with a custom logging level.""" |
|
130 |
|
131 _level = 36 |
|
132 |
|
133 def _logging_level(self): |
|
134 return self._level |
|
135 |
|
136 def test_logged_message(self): |
|
137 self._log.log(self._level, "test message") |
|
138 self._assert_log_messages(["unittest: [Level 36] test message\n"]) |
|
139 |
|
140 def test_below_threshold_message(self): |
|
141 self._log.log(self._level - 1, "test message") |
|
142 self._assert_log_messages([]) |