lint_test_expectations.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2012 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 logging
  29. import optparse
  30. import signal
  31. import traceback
  32. from webkitpy.common.host import Host
  33. from webkitpy.layout_tests.models import test_expectations
  34. from webkitpy.port import platform_options
  35. # This mirrors what the shell normally does.
  36. INTERRUPTED_EXIT_STATUS = signal.SIGINT + 128
  37. # This is a randomly chosen exit code that can be tested against to
  38. # indicate that an unexpected exception occurred.
  39. EXCEPTIONAL_EXIT_STATUS = 254
  40. _log = logging.getLogger(__name__)
  41. def lint(host, options, logging_stream):
  42. logger = logging.getLogger()
  43. logger.setLevel(logging.INFO)
  44. handler = logging.StreamHandler(logging_stream)
  45. logger.addHandler(handler)
  46. try:
  47. ports_to_lint = [host.port_factory.get(name) for name in host.port_factory.all_port_names(options.platform)]
  48. files_linted = set()
  49. lint_failed = False
  50. for port_to_lint in ports_to_lint:
  51. expectations_dict = port_to_lint.expectations_dict()
  52. # FIXME: This won't work if multiple ports share a TestExpectations file but support different modifiers in the file.
  53. for expectations_file in expectations_dict.keys():
  54. if expectations_file in files_linted:
  55. continue
  56. try:
  57. test_expectations.TestExpectations(port_to_lint,
  58. expectations_to_lint={expectations_file: expectations_dict[expectations_file]})
  59. except test_expectations.ParseError as e:
  60. lint_failed = True
  61. _log.error('')
  62. for warning in e.warnings:
  63. _log.error(warning)
  64. _log.error('')
  65. files_linted.add(expectations_file)
  66. if lint_failed:
  67. _log.error('Lint failed.')
  68. return -1
  69. _log.info('Lint succeeded.')
  70. return 0
  71. finally:
  72. logger.removeHandler(handler)
  73. def main(argv, _, stderr):
  74. parser = optparse.OptionParser(option_list=platform_options(use_globs=True))
  75. options, _ = parser.parse_args(argv)
  76. if options.platform and 'test' in options.platform:
  77. # It's a bit lame to import mocks into real code, but this allows the user
  78. # to run tests against the test platform interactively, which is useful for
  79. # debugging test failures.
  80. from webkitpy.common.host_mock import MockHost
  81. host = MockHost()
  82. else:
  83. host = Host()
  84. try:
  85. exit_status = lint(host, options, stderr)
  86. except KeyboardInterrupt:
  87. exit_status = INTERRUPTED_EXIT_STATUS
  88. except Exception as e:
  89. print >> stderr, '\n%s raised: %s' % (e.__class__.__name__, str(e))
  90. traceback.print_exc(file=stderr)
  91. exit_status = EXCEPTIONAL_EXIT_STATUS
  92. return exit_status