Home | History | Annotate | Download | only in checkers
      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 import os
     30 import sys
     31 import webkitpy.thirdparty.unittest2 as unittest
     32 
     33 from test_expectations import TestExpectationsChecker
     34 from webkitpy.common.host_mock import MockHost
     35 
     36 
     37 class ErrorCollector(object):
     38     """An error handler class for unit tests."""
     39 
     40     def __init__(self):
     41         self._errors = []
     42         self.turned_off_filtering = False
     43 
     44     def turn_off_line_filtering(self):
     45         self.turned_off_filtering = True
     46 
     47     def __call__(self, lineno, category, confidence, message):
     48         self._errors.append('%s  [%s] [%d]' % (message, category, confidence))
     49         return True
     50 
     51     def get_errors(self):
     52         return ''.join(self._errors)
     53 
     54     def reset_errors(self):
     55         self._errors = []
     56         self.turned_off_filtering = False
     57 
     58 
     59 class TestExpectationsTestCase(unittest.TestCase):
     60     """TestCase for test_expectations.py"""
     61 
     62     def setUp(self):
     63         self._error_collector = ErrorCollector()
     64         self._test_file = 'passes/text.html'
     65 
     66     def assert_lines_lint(self, lines, should_pass, expected_output=None):
     67         self._error_collector.reset_errors()
     68 
     69         host = MockHost()
     70         checker = TestExpectationsChecker('test/TestExpectations',
     71                                           self._error_collector, host=host)
     72 
     73         # We should have a valid port, but override it with a test port so we
     74         # can check the lines.
     75         self.assertIsNotNone(checker._port_obj)
     76         checker._port_obj = host.port_factory.get('test-mac-leopard')
     77 
     78         checker.check_test_expectations(expectations_str='\n'.join(lines),
     79                                         tests=[self._test_file])
     80         checker.check_tabs(lines)
     81         if should_pass:
     82             self.assertEqual('', self._error_collector.get_errors())
     83         elif expected_output:
     84             self.assertEqual(expected_output, self._error_collector.get_errors())
     85         else:
     86             self.assertNotEquals('', self._error_collector.get_errors())
     87 
     88         # Note that a patch might change a line that introduces errors elsewhere, but we
     89         # don't want to lint the whole file (it can unfairly punish patches for pre-existing errors).
     90         # We rely on a separate lint-webkitpy step on the bots to keep the whole file okay.
     91         # FIXME: See https://bugs.webkit.org/show_bug.cgi?id=104712 .
     92         self.assertFalse(self._error_collector.turned_off_filtering)
     93 
     94     def test_valid_expectations(self):
     95         self.assert_lines_lint(["crbug.com/1234 [ Mac ] passes/text.html [ Pass Failure ]"], should_pass=True)
     96 
     97     def test_invalid_expectations(self):
     98         self.assert_lines_lint(["Bug(me) passes/text.html [ Give Up]"], should_pass=False)
     99 
    100     def test_tab(self):
    101         self.assert_lines_lint(["\twebkit.org/b/1 passes/text.html [ Pass ]"], should_pass=False, expected_output="Line contains tab character.  [whitespace/tab] [5]")
    102