summaryrefslogtreecommitdiffstats
path: root/WebKitTools/Scripts/webkitpy/style/main_unittest.py
blob: fe448f5cd019379622a4d6cd2ce5f7b6d6daef00 (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
# 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 main.py."""

import os
import unittest

from main import change_directory
from webkitpy.style_references import LogTesting


class ChangeDirectoryTest(unittest.TestCase):

    """Tests change_directory()."""

    _original_directory = "/original"
    _checkout_root = "/WebKit"

    class _MockOs(object):

        """A mock os module for unit testing."""

        def __init__(self, test_case):
            self._test_case = test_case
            self._current_directory = \
                ChangeDirectoryTest._original_directory

        def chdir(self, current_directory):
            self._current_directory = current_directory

        def assertCurrentDirectory(self, expected_directory):
            self._test_case.assertEquals(expected_directory,
                                         self._current_directory)

    def setUp(self):
        self._log = LogTesting.setUp(self)
        self._mock_os = self._MockOs(self)

    def tearDown(self):
        self._log.tearDown()

    # This method is a convenient wrapper for change_working_directory() that
    # passes the mock_os for this unit testing class.
    def _change_directory(self, paths, checkout_root):
        return change_directory(paths=paths,
                                checkout_root=checkout_root,
                                mock_os=self._mock_os)

    def _assert_result(self, actual_return_value, expected_return_value,
                       expected_log_messages, expected_current_directory):
        self.assertEquals(actual_return_value, expected_return_value)
        self._log.assertMessages(expected_log_messages)
        self._mock_os.assertCurrentDirectory(expected_current_directory)

    def test_checkout_root_none_paths_none(self):
        self.assertRaises(Exception, self._change_directory,
                          checkout_root=None, paths=None)
        self._log.assertMessages([])
        self._mock_os.assertCurrentDirectory(self._original_directory)

    def test_checkout_root_none(self):
        paths = self._change_directory(checkout_root=None,
                                       paths=["path1"])
        log_messages = [
"""WARNING: WebKit checkout root not found:
  Path-dependent style checks may not work correctly.
  See the help documentation for more info.
"""]
        self._assert_result(paths, ["path1"], log_messages,
                            self._original_directory)

    def test_paths_none(self):
        paths = self._change_directory(checkout_root=self._checkout_root,
                                       paths=None)
        self._assert_result(paths, None, [], self._checkout_root)

    def test_paths_convertible(self):
        paths=["/WebKit/foo1.txt",
               "/WebKit/foo2.txt"]
        paths = self._change_directory(checkout_root=self._checkout_root,
                                       paths=paths)
        self._assert_result(paths, ["foo1.txt", "foo2.txt"], [],
                            self._checkout_root)

    def test_with_scm_paths_unconvertible(self):
        paths=["/WebKit/foo1.txt",
               "/outside/foo2.txt"]
        paths = self._change_directory(checkout_root=self._checkout_root,
                                       paths=paths)
        log_messages = [
"""WARNING: Path-dependent style checks may not work correctly:

  One of the given paths is outside the WebKit checkout of the current
  working directory:

    Path: /outside/foo2.txt
    Checkout root: /WebKit

  Pass only files below the checkout root to ensure correct results.
  See the help documentation for more info.

"""]
        self._assert_result(paths, paths, log_messages,
                            self._original_directory)