1 # Copyright (C) 2010 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 """Checks WebKit style for test_expectations files."""
36 from common import TabChecker
37 from webkitpy.style_references import port
38 from webkitpy.style_references import test_expectations
40 _log = logging.getLogger("webkitpy.style.checkers.test_expectations")
43 class ChromiumOptions(object):
44 """A mock object for creating chromium port object.
46 port.get() requires an options object which has 'chromium' attribute to create
47 chromium port object for each platform. This class mocks such object.
53 class TestExpectationsChecker(object):
54 """Processes test_expectations.txt lines for validating the syntax."""
56 categories = set(['test/expectations'])
58 def __init__(self, file_path, handle_style_error):
59 self._file_path = file_path
60 self._handle_style_error = handle_style_error
61 self._tab_checker = TabChecker(file_path, handle_style_error)
62 self._output_regex = re.compile('Line:(?P<line>\d+)\s*(?P<message>.+)')
63 # Determining the port of this expectations.
65 port_name = self._file_path.split(os.sep)[-2]
66 if port_name == "chromium":
67 options = ChromiumOptions()
68 self._port_obj = port.get(port_name=None, options=options)
70 self._port_obj = port.get(port_name=port_name)
72 # Using 'test' port when we couldn't determine the port for this
74 _log.warn("Could not determine the port for %s. "
75 "Using 'test' port, but platform-specific expectations "
76 "will fail the check." % self._file_path)
77 self._port_obj = port.get('test')
78 # Suppress error messages of test_expectations module since they will be
80 log = logging.getLogger("webkitpy.layout_tests.layout_package."
82 log.setLevel(logging.CRITICAL)
84 def _handle_error_message(self, lineno, message, confidence):
87 def check_test_expectations(self, expectations_str, tests=None, overrides=None):
91 expectations = test_expectations.TestExpectationsFile(
92 port=self._port_obj, expectations=expectations_str, full_test_list=tests,
93 test_config=self._port_obj.test_configuration(),
94 is_lint_mode=True, overrides=overrides)
95 except test_expectations.ParseError, error:
102 for error in err.errors:
103 matched = self._output_regex.match(error)
105 lineno, message = matched.group('line', 'message')
106 self._handle_style_error(int(lineno), 'test/expectations', level, message)
109 def check_tabs(self, lines):
110 self._tab_checker.check(lines)
112 def check(self, lines):
113 overrides = self._port_obj.test_expectations_overrides()
114 expectations = '\n'.join(lines)
115 self.check_test_expectations(expectations_str=expectations,
118 # Warn tabs in lines as well
119 self.check_tabs(lines)