summaryrefslogtreecommitdiffstats
path: root/Tools/Scripts/webkitpy/common/system/logutils_unittest.py
blob: b77c28419c83e84916cabde0838a25a81d6b4224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Unit tests for logutils.py."""

import logging
import os
import unittest

from webkitpy.common.system.logtesting import LogTesting
from webkitpy.common.system.logtesting import TestLogStream
import webkitpy.common.system.logutils as logutils


class GetLoggerTest(unittest.TestCase):

    """Tests get_logger()."""

    def test_get_logger_in_webkitpy(self):
        logger = logutils.get_logger(__file__)
        self.assertEquals(logger.name, "webkitpy.common.system.logutils_unittest")

    def test_get_logger_not_in_webkitpy(self):
        # Temporarily change the working directory so that we
        # can test get_logger() for a path outside of webkitpy.
        working_directory = os.getcwd()
        root_dir = "/"
        os.chdir(root_dir)

        logger = logutils.get_logger("/Tools/Scripts/test-webkitpy")
        self.assertEquals(logger.name, "test-webkitpy")

        logger = logutils.get_logger("/Tools/Scripts/test-webkitpy.py")
        self.assertEquals(logger.name, "test-webkitpy")

        os.chdir(working_directory)


class ConfigureLoggingTestBase(unittest.TestCase):

    """Base class for configure_logging() unit tests."""

    def _logging_level(self):
        raise Exception("Not implemented.")

    def setUp(self):
        log_stream = TestLogStream(self)

        # Use a logger other than the root logger or one prefixed with
        # "webkitpy." so as not to conflict with test-webkitpy logging.
        logger = logging.getLogger("unittest")

        # Configure the test logger not to pass messages along to the
        # root logger.  This prevents test messages from being
        # propagated to loggers used by test-webkitpy logging (e.g.
        # the root logger).
        logger.propagate = False

        logging_level = self._logging_level()
        self._handlers = logutils.configure_logging(logging_level=logging_level,
                                                    logger=logger,
                                                    stream=log_stream)
        self._log = logger
        self._log_stream = log_stream

    def tearDown(self):
        """Reset logging to its original state.

        This method ensures that the logging configuration set up
        for a unit test does not affect logging in other unit tests.

        """
        logger = self._log
        for handler in self._handlers:
            logger.removeHandler(handler)

    def _assert_log_messages(self, messages):
        """Assert that the logged messages equal the given messages."""
        self._log_stream.assertMessages(messages)


class ConfigureLoggingTest(ConfigureLoggingTestBase):

    """Tests configure_logging() with the default logging level."""

    def _logging_level(self):
        return None

    def test_info_message(self):
        self._log.info("test message")
        self._assert_log_messages(["unittest: [INFO] test message\n"])

    def test_below_threshold_message(self):
        # We test the boundary case of a logging level equal to 19.
        # In practice, we will probably only be calling log.debug(),
        # which corresponds to a logging level of 10.
        level = logging.INFO - 1  # Equals 19.
        self._log.log(level, "test message")
        self._assert_log_messages([])

    def test_two_messages(self):
        self._log.info("message1")
        self._log.info("message2")
        self._assert_log_messages(["unittest: [INFO] message1\n",
                                   "unittest: [INFO] message2\n"])


class ConfigureLoggingCustomLevelTest(ConfigureLoggingTestBase):

    """Tests configure_logging() with a custom logging level."""

    _level = 36

    def _logging_level(self):
        return self._level

    def test_logged_message(self):
        self._log.log(self._level, "test message")
        self._assert_log_messages(["unittest: [Level 36] test message\n"])

    def test_below_threshold_message(self):
        self._log.log(self._level - 1, "test message")
        self._assert_log_messages([])