check-webkit-style can't handle qt-arm, qt-win, qt-mac, qt-5.0, qt-5.0-wk1 and qt...
[WebKit-https.git] / Tools / Scripts / webkitpy / style / checkers / test_expectations_unittest.py
1 #!/usr/bin/python
2 # Copyright (C) 2010 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import os
31 import sys
32 import unittest
33
34 from test_expectations import TestExpectationsChecker
35 from webkitpy.common.host_mock import MockHost
36
37
38 class ErrorCollector(object):
39     """An error handler class for unit tests."""
40
41     def __init__(self):
42         self._errors = []
43         self.turned_off_filtering = False
44
45     def turn_off_line_filtering(self):
46         self.turned_off_filtering = True
47
48     def __call__(self, lineno, category, confidence, message):
49         self._errors.append('%s  [%s] [%d]' % (message, category, confidence))
50         return True
51
52     def get_errors(self):
53         return ''.join(self._errors)
54
55     def reset_errors(self):
56         self._errors = []
57         self.turned_off_filtering = False
58
59
60 class TestExpectationsTestCase(unittest.TestCase):
61     """TestCase for test_expectations.py"""
62
63     def setUp(self):
64         self._error_collector = ErrorCollector()
65         self._test_file = 'passes/text.html'
66
67     def _expect_port_for_expectations_path(self, expected_port_implementation, expectations_path):
68         host = MockHost()
69         checker = TestExpectationsChecker(expectations_path, ErrorCollector(), host=host)
70         port = checker._determine_port_from_expectations_path(host, expectations_path)
71         if port:
72             self.assertTrue(port.name().startswith(expected_port_implementation))
73         else:
74             self.assertEquals(None, expected_port_implementation)
75
76     def test_determine_port_from_expectations_path(self):
77         self._expect_port_for_expectations_path(None, '/')
78         self._expect_port_for_expectations_path(None, 'LayoutTests/chromium-mac/TestExpectations')
79         self._expect_port_for_expectations_path('chromium', 'LayoutTests/platform/chromium/TestExpectations')
80         self._expect_port_for_expectations_path(None, '/mock-checkout/LayoutTests/platform/win/TestExpectations')
81         self._expect_port_for_expectations_path('win', 'LayoutTests/platform/win/TestExpectations')
82         self._expect_port_for_expectations_path('efl', 'LayoutTests/platform/efl/TestExpectations')
83         self._expect_port_for_expectations_path('efl', 'LayoutTests/platform/efl-wk1/TestExpectations')
84         self._expect_port_for_expectations_path('efl', 'LayoutTests/platform/efl-wk2/TestExpectations')
85         self._expect_port_for_expectations_path('qt', 'LayoutTests/platform/qt-win/TestExpectations')
86         # FIXME: check-webkit-style doesn't know how to create port objects for all Qt version (4.8, 5.0) and
87         # will only check files based on the installed version of Qt.
88         #self._expect_port_for_expectations_path('qt', 'LayoutTests/platform/qt-5.0-wk2/TestExpectations')
89
90     def assert_lines_lint(self, lines, should_pass, expected_output=None):
91         self._error_collector.reset_errors()
92
93         host = MockHost()
94         checker = TestExpectationsChecker('test/TestExpectations',
95                                           self._error_collector, host=host)
96
97         # We should have failed to find a valid port object for that path.
98         self.assertEquals(checker._port_obj, None)
99
100         # Now use a test port so we can check the lines.
101         checker._port_obj = host.port_factory.get('test-mac-leopard')
102         checker.check_test_expectations(expectations_str='\n'.join(lines),
103                                         tests=[self._test_file])
104         checker.check_tabs(lines)
105         if should_pass:
106             self.assertEqual('', self._error_collector.get_errors())
107         elif expected_output:
108             self.assertEquals(expected_output, self._error_collector.get_errors())
109         else:
110             self.assertNotEquals('', self._error_collector.get_errors())
111         self.assertTrue(self._error_collector.turned_off_filtering)
112
113     def test_valid_expectations(self):
114         self.assert_lines_lint(["crbug.com/1234 [ Mac ] passes/text.html [ Pass Failure ]"], should_pass=True)
115
116     def test_invalid_expectations(self):
117         self.assert_lines_lint(["Bug(me) passes/text.html [ Give Up]"], should_pass=False)
118
119     def test_tab(self):
120         self.assert_lines_lint(["\twebkit.org/b/1 passes/text.html [ Pass ]"], should_pass=False, expected_output="Line contains tab character.  [whitespace/tab] [5]")