layout_test_finder.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 errno
  29. import logging
  30. import re
  31. from webkitpy.layout_tests.models import test_expectations
  32. _log = logging.getLogger(__name__)
  33. class LayoutTestFinder(object):
  34. def __init__(self, port, options):
  35. self._port = port
  36. self._options = options
  37. self._filesystem = self._port.host.filesystem
  38. self.LAYOUT_TESTS_DIRECTORY = 'LayoutTests'
  39. def find_tests(self, options, args):
  40. paths = self._strip_test_dir_prefixes(args)
  41. if options.test_list:
  42. paths += self._strip_test_dir_prefixes(self._read_test_names_from_file(options.test_list, self._port.TEST_PATH_SEPARATOR))
  43. test_files = self._port.tests(paths)
  44. return (paths, test_files)
  45. def _strip_test_dir_prefixes(self, paths):
  46. return [self._strip_test_dir_prefix(path) for path in paths if path]
  47. def _strip_test_dir_prefix(self, path):
  48. # Handle both "LayoutTests/foo/bar.html" and "LayoutTests\foo\bar.html" if
  49. # the filesystem uses '\\' as a directory separator.
  50. if path.startswith(self.LAYOUT_TESTS_DIRECTORY + self._port.TEST_PATH_SEPARATOR):
  51. return path[len(self.LAYOUT_TESTS_DIRECTORY + self._port.TEST_PATH_SEPARATOR):]
  52. if path.startswith(self.LAYOUT_TESTS_DIRECTORY + self._filesystem.sep):
  53. return path[len(self.LAYOUT_TESTS_DIRECTORY + self._filesystem.sep):]
  54. return path
  55. def _read_test_names_from_file(self, filenames, test_path_separator):
  56. fs = self._filesystem
  57. tests = []
  58. for filename in filenames:
  59. try:
  60. if test_path_separator != fs.sep:
  61. filename = filename.replace(test_path_separator, fs.sep)
  62. file_contents = fs.read_text_file(filename).split('\n')
  63. for line in file_contents:
  64. line = self._strip_comments(line)
  65. if line:
  66. tests.append(line)
  67. except IOError, e:
  68. if e.errno == errno.ENOENT:
  69. _log.critical('')
  70. _log.critical('--test-list file "%s" not found' % file)
  71. raise
  72. return tests
  73. @staticmethod
  74. def _strip_comments(line):
  75. commentIndex = line.find('//')
  76. if commentIndex is -1:
  77. commentIndex = len(line)
  78. line = re.sub(r'\s+', ' ', line[:commentIndex].strip())
  79. if line == '':
  80. return None
  81. else:
  82. return line
  83. def skip_tests(self, paths, all_tests_list, expectations, http_tests):
  84. all_tests = set(all_tests_list)
  85. tests_to_skip = expectations.get_tests_with_result_type(test_expectations.SKIP)
  86. if self._options.skip_failing_tests:
  87. tests_to_skip.update(expectations.get_tests_with_result_type(test_expectations.FAIL))
  88. tests_to_skip.update(expectations.get_tests_with_result_type(test_expectations.FLAKY))
  89. if self._options.skipped == 'only':
  90. tests_to_skip = all_tests - tests_to_skip
  91. elif self._options.skipped == 'ignore':
  92. tests_to_skip = set()
  93. elif self._options.skipped != 'always':
  94. # make sure we're explicitly running any tests passed on the command line; equivalent to 'default'.
  95. tests_to_skip -= set(paths)
  96. # unless of course we don't want to run the HTTP tests :)
  97. if not self._options.http:
  98. tests_to_skip.update(set(http_tests))
  99. return tests_to_skip
  100. def split_into_chunks(self, test_names):
  101. """split into a list to run and a set to skip, based on --run-chunk and --run-part."""
  102. if not self._options.run_chunk and not self._options.run_part:
  103. return test_names, set()
  104. # If the user specifies they just want to run a subset of the tests,
  105. # just grab a subset of the non-skipped tests.
  106. chunk_value = self._options.run_chunk or self._options.run_part
  107. try:
  108. (chunk_num, chunk_len) = chunk_value.split(":")
  109. chunk_num = int(chunk_num)
  110. assert(chunk_num >= 0)
  111. test_size = int(chunk_len)
  112. assert(test_size > 0)
  113. except AssertionError:
  114. _log.critical("invalid chunk '%s'" % chunk_value)
  115. return (None, None)
  116. # Get the number of tests
  117. num_tests = len(test_names)
  118. # Get the start offset of the slice.
  119. if self._options.run_chunk:
  120. chunk_len = test_size
  121. # In this case chunk_num can be really large. We need
  122. # to make the slave fit in the current number of tests.
  123. slice_start = (chunk_num * chunk_len) % num_tests
  124. else:
  125. # Validate the data.
  126. assert(test_size <= num_tests)
  127. assert(chunk_num <= test_size)
  128. # To count the chunk_len, and make sure we don't skip
  129. # some tests, we round to the next value that fits exactly
  130. # all the parts.
  131. rounded_tests = num_tests
  132. if rounded_tests % test_size != 0:
  133. rounded_tests = (num_tests + test_size - (num_tests % test_size))
  134. chunk_len = rounded_tests / test_size
  135. slice_start = chunk_len * (chunk_num - 1)
  136. # It does not mind if we go over test_size.
  137. # Get the end offset of the slice.
  138. slice_end = min(num_tests, slice_start + chunk_len)
  139. tests_to_run = test_names[slice_start:slice_end]
  140. _log.debug('chunk slice [%d:%d] of %d is %d tests' % (slice_start, slice_end, num_tests, (slice_end - slice_start)))
  141. # If we reached the end and we don't have enough tests, we run some
  142. # from the beginning.
  143. if slice_end - slice_start < chunk_len:
  144. extra = chunk_len - (slice_end - slice_start)
  145. _log.debug(' last chunk is partial, appending [0:%d]' % extra)
  146. tests_to_run.extend(test_names[0:extra])
  147. return (tests_to_run, set(test_names) - set(tests_to_run))