1# Copyright (C) 2010 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
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
12# distribution.
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.
16#
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.
28
29"""Checks WebKit style for test_expectations files."""
30
31import logging
32import os
33import re
34import sys
35
36from common import TabChecker
37from webkitpy.style_references import port
38from webkitpy.style_references import test_expectations
39
40_log = logging.getLogger("webkitpy.style.checkers.test_expectations")
41
42
43class ChromiumOptions(object):
44    """A mock object for creating chromium port object.
45
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.
48    """
49    def __init__(self):
50        self.chromium = True
51
52
53class TestExpectationsChecker(object):
54    """Processes test_expectations.txt lines for validating the syntax."""
55
56    categories = set(['test/expectations'])
57
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.
64        try:
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)
69            else:
70                self._port_obj = port.get(port_name=port_name)
71        except:
72            # Using 'test' port when we couldn't determine the port for this
73            # expectations.
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
79        # reported later.
80        log = logging.getLogger("webkitpy.layout_tests.layout_package."
81                                "test_expectations")
82        log.setLevel(logging.CRITICAL)
83
84    def _handle_error_message(self, lineno, message, confidence):
85        pass
86
87    def check_test_expectations(self, expectations_str, tests=None, overrides=None):
88        err = None
89        expectations = None
90        try:
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:
96            err = error
97
98        if err:
99            level = 2
100            if err.fatal:
101                level = 5
102            for error in err.errors:
103                matched = self._output_regex.match(error)
104                if matched:
105                    lineno, message = matched.group('line', 'message')
106                    self._handle_style_error(int(lineno), 'test/expectations', level, message)
107
108
109    def check_tabs(self, lines):
110        self._tab_checker.check(lines)
111
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,
116                                     tests=None,
117                                     overrides=overrides)
118        # Warn tabs in lines as well
119        self.check_tabs(lines)
120