test_expectations_unittest.py 5.8 KB

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