PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hooks/webkitpy/style/checkers/test_expectations.py

https://github.com/hwti/LunaSysMgr
Python | 121 lines | 74 code | 13 blank | 34 comment | 7 complexity | 18749d61e21b1de97be04c609d65f4eb MD5 | raw file
  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. """Checks WebKit style for test_expectations files."""
  29. import logging
  30. import os
  31. import re
  32. import sys
  33. from common import TabChecker
  34. from webkitpy.layout_tests import port
  35. from webkitpy.layout_tests.models import test_expectations
  36. _log = logging.getLogger(__name__)
  37. class ChromiumOptions(object):
  38. """A mock object for creating chromium port object.
  39. port.get() requires an options object which has 'chromium' attribute to create
  40. chromium port object for each platform. This class mocks such object.
  41. """
  42. def __init__(self):
  43. self.chromium = True
  44. class TestExpectationsChecker(object):
  45. """Processes test_expectations.txt lines for validating the syntax."""
  46. categories = set(['test/expectations'])
  47. def __init__(self, file_path, handle_style_error):
  48. self._file_path = file_path
  49. self._handle_style_error = handle_style_error
  50. self._handle_style_error.turn_off_line_filtering()
  51. self._tab_checker = TabChecker(file_path, handle_style_error)
  52. self._output_regex = re.compile('Line:(?P<line>\d+)\s*(?P<message>.+)')
  53. # Determining the port of this expectations.
  54. try:
  55. port_name = self._file_path.split(os.sep)[-2]
  56. if port_name == "chromium":
  57. options = ChromiumOptions()
  58. self._port_obj = port.get(port_name=None, options=options)
  59. else:
  60. self._port_obj = port.get(port_name=port_name)
  61. except:
  62. # Using 'test' port when we couldn't determine the port for this
  63. # expectations.
  64. _log.warn("Could not determine the port for %s. "
  65. "Using 'test' port, but platform-specific expectations "
  66. "will fail the check." % self._file_path)
  67. self._port_obj = port.get('test')
  68. # Suppress error messages of test_expectations module since they will be
  69. # reported later.
  70. log = logging.getLogger("webkitpy.layout_tests.layout_package."
  71. "test_expectations")
  72. log.setLevel(logging.CRITICAL)
  73. def _handle_error_message(self, lineno, message, confidence):
  74. pass
  75. def check_test_expectations(self, expectations_str, tests=None, overrides=None):
  76. err = None
  77. expectations = None
  78. try:
  79. expectations = test_expectations.TestExpectations(
  80. port=self._port_obj, expectations=expectations_str, tests=tests,
  81. test_config=self._port_obj.test_configuration(),
  82. is_lint_mode=True, overrides=overrides)
  83. except test_expectations.ParseError, error:
  84. err = error
  85. if err:
  86. level = 2
  87. if err.fatal:
  88. level = 5
  89. for error in err.errors:
  90. matched = self._output_regex.match(error)
  91. if matched:
  92. lineno, message = matched.group('line', 'message')
  93. self._handle_style_error(int(lineno), 'test/expectations', level, message)
  94. def check_tabs(self, lines):
  95. self._tab_checker.check(lines)
  96. def check(self, lines):
  97. overrides = self._port_obj.test_expectations_overrides()
  98. expectations = '\n'.join(lines)
  99. self.check_test_expectations(expectations_str=expectations,
  100. tests=None,
  101. overrides=overrides)
  102. # Warn tabs in lines as well
  103. self.check_tabs(lines)