|
Line 0
a/Tools/Scripts/webkitpy/w3c/conformance_checker.py_sec1
|
|
|
1 |
# Copyright (c) 2015, Canon 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 |
| 5 |
# are met: |
| 6 |
# 1. Redistributions of source code must retain the above copyright |
| 7 |
# notice, this list of conditions and the following disclaimer. |
| 8 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 9 |
# notice, this list of conditions and the following disclaimer in the |
| 10 |
# documentation and/or other materials provided with the distribution. |
| 11 |
# 3. Neither the name of Canon Inc. nor the names of |
| 12 |
# its contributors may be used to endorse or promote products derived |
| 13 |
# from this software without specific prior written permission. |
| 14 |
# |
| 15 |
# THIS SOFTWARE IS PROVIDED BY CANON INC. AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 16 |
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 |
# DISCLAIMED. IN NO EVENT SHALL CANON INC. AND ITS CONTRIBUTORS BE LIABLE FOR |
| 19 |
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 |
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 |
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 |
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
|
| 26 |
""" |
| 27 |
This script evaluates W3C tests conformance by: |
| 28 |
- importing W3C tests as WebKit tests |
| 29 |
- generating expected.txt files (using run-webkit-tests) |
| 30 |
- computing whether a js test is passing or not by checking PASS/FAIL assertions outputted within the expected.txt files |
| 31 |
""" |
| 32 |
|
| 33 |
import argparse |
| 34 |
import json |
| 35 |
import logging |
| 36 |
import optparse |
| 37 |
import sys |
| 38 |
|
| 39 |
from webkitpy.common.host import Host |
| 40 |
from webkitpy.common.system.executive import Executive |
| 41 |
from webkitpy.layout_tests.layout_package import json_results_generator |
| 42 |
from webkitpy.layout_tests.models.test_expectations import TestExpectations |
| 43 |
from webkitpy.port import configuration_options, platform_options |
| 44 |
from webkitpy.w3c.test_importer import TestImporter |
| 45 |
from webkitpy.w3c.test_importer import parse_args as parse_importer_args |
| 46 |
from webkitpy.w3c.test_downloader import TestDownloader |
| 47 |
|
| 48 |
_log = logging.getLogger(__name__) |
| 49 |
|
| 50 |
|
| 51 |
def create_argument_parser(): |
| 52 |
parser = argparse.ArgumentParser() |
| 53 |
|
| 54 |
for opt in platform_options(use_globs=False): |
| 55 |
parser.add_argument(opt.get_opt_string(), action=opt.action, dest=opt.dest, const=getattr(opt, 'const'), help=getattr(opt, 'help')) |
| 56 |
parser.add_argument('--dump-render-tree', '-1', dest='webkit_test_runner', action='store_false', default=True, |
| 57 |
help='Set WebKit architecture WebKit1 or WebKit2 (default WK2).') |
| 58 |
|
| 59 |
parser.add_argument('--debug', dest='configuration', action='store_const', const='Debug', default='Release', |
| 60 |
help='Use "Debug" configuration. Default is "Release"') |
| 61 |
parser.add_argument('-q', dest='quiet', action='store_true', default=False, |
| 62 |
help='Print minimal log.') |
| 63 |
parser.add_argument('-v', dest='verbose', action='store_true', default=False, |
| 64 |
help='Print maximal log.') |
| 65 |
parser.add_argument('-t', '--test-path', action='append', dest='test_paths', default=[], |
| 66 |
help='Import and run only tests in the supplied subdirectory of the w3c_test_directory. Can be supplied multiple times to give multiple paths') |
| 67 |
parser.add_argument('-s', '--skip-refresh', action='store_false', default=True, dest='refresh', |
| 68 |
help='Disable refresh of tests (git checkout + import). By default, tests are refreshed.') |
| 69 |
parser.add_argument('--no-fetch', action='store_false', dest='fetch', default=True, |
| 70 |
help='Do not fetch the CSS/WPT repositories. By default, repositories are fetched.') |
| 71 |
|
| 72 |
return parser |
| 73 |
|
| 74 |
|
| 75 |
class ConformanceChecker(object): |
| 76 |
|
| 77 |
def __init__(self, port, options): |
| 78 |
self._port = port |
| 79 |
self._filesystem = port.host.filesystem |
| 80 |
self._options = options |
| 81 |
self._conformance_results = None |
| 82 |
|
| 83 |
self.WPT_TESTSUITE = 'web-platform-tests' |
| 84 |
self.layout_w3c_directory = self._filesystem.join('imported', 'w3c') |
| 85 |
|
| 86 |
self.results_directory = self._port.w3c_conformance_results_directory() |
| 87 |
self.layout_tests_result_directory = self._filesystem.join(self.results_directory, 'layout-test-results') |
| 88 |
self.imported_tests_directory = self._filesystem.join(self.results_directory, 'imported-tests') |
| 89 |
self.runned_tests_directory = self._filesystem.join(self.results_directory, 'runned-tests') |
| 90 |
|
| 91 |
self.init_test_importer() |
| 92 |
|
| 93 |
def _print_rwt_output_line(self, line): |
| 94 |
if not self._options.verbose: |
| 95 |
if line.startswith('['): |
| 96 |
tokens = line.split(' ') |
| 97 |
sys.stdout.write('\r' + tokens[0] + ' ' + tokens[1].replace(self.runned_tests_directory, '')[1:] + ' ') |
| 98 |
sys.stdout.flush() |
| 99 |
elif line == '': |
| 100 |
sys.stdout.write('\n') |
| 101 |
else: |
| 102 |
sys.stdout.write(line) |
| 103 |
|
| 104 |
def run_tests(self, tests): |
| 105 |
run_webkit_args = [self._port.path_to_script('run-webkit-tests'), '--no-retry-failures', '--no-show-results', '--full-results-html'] |
| 106 |
|
| 107 |
test_list_filename = self._filesystem.join(self.runned_tests_directory, 'test_list.txt') |
| 108 |
self._filesystem.write_text_file(test_list_filename, '\n'.join(tests)) |
| 109 |
run_webkit_args.append('--test-list=' + test_list_filename) |
| 110 |
|
| 111 |
if self._filesystem.exists(self.layout_tests_result_directory): |
| 112 |
self._filesystem.rmtree(self.layout_tests_result_directory) |
| 113 |
run_webkit_args.append('--results-directory=' + self.layout_tests_result_directory) |
| 114 |
|
| 115 |
run_webkit_args.append('--wptserver-doc-root=' + self._filesystem.join(self.runned_tests_directory, self.WPT_TESTSUITE)) |
| 116 |
|
| 117 |
if self._options.configuration: |
| 118 |
run_webkit_args.append('--target=' + self._options.configuration) |
| 119 |
if self._options.platform: |
| 120 |
run_webkit_args.append('--platform=' + self._options.platform) |
| 121 |
if not self._options.webkit_test_runner: |
| 122 |
run_webkit_args.append('-1') |
| 123 |
if not self._options.verbose: |
| 124 |
run_webkit_args.append('-q') |
| 125 |
|
| 126 |
process = Executive().popen(run_webkit_args, stdout=Executive.PIPE, stderr=Executive.STDOUT) |
| 127 |
while True: |
| 128 |
line = process.stdout.readline() |
| 129 |
self._print_rwt_output_line(line) |
| 130 |
if line == '': |
| 131 |
break |
| 132 |
process.wait() |
| 133 |
|
| 134 |
def init_test_importer(self): |
| 135 |
args = ['-d', self.imported_tests_directory, '--ignore-expectations'] |
| 136 |
|
| 137 |
for test_path in self._options.test_paths: |
| 138 |
args.append('-t') |
| 139 |
args.append(test_path) |
| 140 |
|
| 141 |
if not self._options.fetch: |
| 142 |
args.append('--no-fetch') |
| 143 |
|
| 144 |
if self._options.verbose: |
| 145 |
args.append('-v') |
| 146 |
elif self._options.quiet: |
| 147 |
logging.getLogger('webkitpy.w3c.test_importer').setLevel(logging.WARN) |
| 148 |
logging.getLogger('webkitpy.w3c.test_converter').setLevel(logging.WARN) |
| 149 |
logging.getLogger('webkitpy.w3c.test_downloader').setLevel(logging.WARN) |
| 150 |
|
| 151 |
options, args = parse_importer_args(args) |
| 152 |
self.test_importer = TestImporter(self._port.host, None, options) |
| 153 |
|
| 154 |
def test_suites(self): |
| 155 |
return self._options.test_paths if self._options.test_paths else self.test_importer.test_downloader().all_test_paths() |
| 156 |
|
| 157 |
def check_conformance(self): |
| 158 |
if self._options.refresh is True: |
| 159 |
_log.info('Importing tests...') |
| 160 |
self._filesystem.rmtree(self.imported_tests_directory) |
| 161 |
self.test_importer.do_import() |
| 162 |
# FIXME: We should install WPT modules in imported tests directory to not reinstall them for each run. |
| 163 |
else: |
| 164 |
_log.info('Skipping test import...') |
| 165 |
|
| 166 |
# Regenerate runned_test_directory to start in a clean state (no -expected.txt files in particular). |
| 167 |
self._filesystem.rmtree(self.runned_tests_directory) |
| 168 |
self._filesystem.copytree(self.imported_tests_directory, self.runned_tests_directory) |
| 169 |
|
| 170 |
_log.info('Running tests...') |
| 171 |
tests = self._port.tests([self._filesystem.join(self.runned_tests_directory, test_suite) for test_suite in self.test_suites()]) |
| 172 |
self.run_tests(tests) |
| 173 |
|
| 174 |
_log.info('Parsing test results...') |
| 175 |
json_file_path = self._filesystem.join(self.layout_tests_result_directory, 'full_results.json') |
| 176 |
if not self._filesystem.exists(json_file_path): |
| 177 |
_log.error('Cannot read run-webkit-tests result file "%s". run-webkit-tests may have run into some errors. Use -v option\nAborting...' % json_file_path) |
| 178 |
return False |
| 179 |
self._conformance_results = TestResultsConformanceParser(self._port, self.runned_tests_directory).compute_conformance_results(tests, json_file_path) |
| 180 |
|
| 181 |
_log.info('Generating conformance results...') |
| 182 |
self._filesystem.maybe_make_directory(self.results_directory) |
| 183 |
self.write_conformance_results() |
| 184 |
|
| 185 |
_log.info('Generating expectation updates...') |
| 186 |
self._filesystem.write_text_file(self._filesystem.join(self.results_directory, 'TestExpectations'), '\n'.join(self.test_expectation_updates())) |
| 187 |
self._filesystem.write_text_file(self._filesystem.join(self.results_directory, 'ImportExpectations'), '\n'.join(self.import_expectation_updates())) |
| 188 |
return True |
| 189 |
|
| 190 |
def print_summary_to_console(self): |
| 191 |
num_pass = num_partial = num_fail = num_error = 0 |
| 192 |
for key, value in self._conformance_results.iteritems(): |
| 193 |
result = value['status'] |
| 194 |
if result == 'PASS': |
| 195 |
num_pass += 1 |
| 196 |
elif result == 'PARTIAL': |
| 197 |
num_partial += 1 |
| 198 |
elif result == 'FAIL': |
| 199 |
num_fail += 1 |
| 200 |
else: |
| 201 |
num_error += 1 |
| 202 |
_log.info('Run ' + str(num_pass + num_fail + num_partial + num_error) + ' tests') |
| 203 |
_log.info('Pass: ' + str(num_pass)) |
| 204 |
_log.info('Partial: ' + str(num_partial)) |
| 205 |
_log.info('Fail: ' + str(num_fail)) |
| 206 |
_log.info('Error: ' + str(num_error)) |
| 207 |
|
| 208 |
def import_expectation_updates(self): |
| 209 |
import_expectations = [] |
| 210 |
for test_suite in self.test_suites(): |
| 211 |
import_expectations.append('# %s [ Pass ]' % self._filesystem.join(self.layout_w3c_directory, test_suite)) |
| 212 |
|
| 213 |
import_expectations.append('') |
| 214 |
|
| 215 |
for test in self._conformance_results.keys(): |
| 216 |
status = self._conformance_results[test]['status'] |
| 217 |
if not(status == 'PASS' or status == 'PARTIAL'): |
| 218 |
import_expectations.append('%s [ Skip ]' % test) |
| 219 |
return import_expectations |
| 220 |
|
| 221 |
def test_expectation_updates(self): |
| 222 |
test_expectations = ['# W3C test infrastructure folders'] |
| 223 |
for test_repository in self.test_importer.test_downloader().test_repositories: |
| 224 |
for path in test_repository['paths_to_import']: |
| 225 |
file_path = self._filesystem.join(self.runned_tests_directory, path) |
| 226 |
if (self._filesystem.isdir(file_path)): |
| 227 |
test_expectations.append('%s [ Skip ]' % self._filesystem.join(self.layout_w3c_directory, path)) |
| 228 |
|
| 229 |
test_expectations.append('# Partially passing tests') |
| 230 |
for test in self._conformance_results.keys(): |
| 231 |
if self._conformance_results[test]['status'] == 'PARTIAL': |
| 232 |
test_expectations.append('%s [ Pass ]' % self._filesystem.join(self.layout_w3c_directory, test)) |
| 233 |
return test_expectations |
| 234 |
|
| 235 |
def write_conformance_results(self): |
| 236 |
_JSON_PREFIX = 'ADD_CONFORMANCE_RESULTS(' |
| 237 |
_JSON_SUFFIX = ');' |
| 238 |
|
| 239 |
test_run_info = {} |
| 240 |
test_run_info['port_name'] = self._port.port_name |
| 241 |
test_run_info['test_repositories'] = self.test_importer.test_downloader().test_repositories |
| 242 |
test_run_info['results_directory'] = self._filesystem.relpath(self.layout_tests_result_directory, self.results_directory) |
| 243 |
test_run_info['tests_directory'] = self._filesystem.relpath(self.runned_tests_directory, self.results_directory) |
| 244 |
|
| 245 |
results = {} |
| 246 |
results['conformance_results'] = self._conformance_results |
| 247 |
results.update(test_run_info) |
| 248 |
|
| 249 |
json_string = json.dumps(results, indent=4, sort_keys=True, separators=(',', ':')) |
| 250 |
json_string = _JSON_PREFIX + json_string + _JSON_SUFFIX |
| 251 |
self._filesystem.write_text_file(self._filesystem.join(self.results_directory, 'w3c_conformance_results.json'), json_string) |
| 252 |
|
| 253 |
results_file = self._port.path_from_webkit_base('LayoutTests', 'imported', 'w3c', 'resources', 'w3c_conformance_results.html') |
| 254 |
if not self._filesystem.exists(results_file): |
| 255 |
_log.warning('w3c_conformance_results.html template file cannot be found and copied to conformance result directory') |
| 256 |
return |
| 257 |
self._filesystem.copyfile(results_file, self._filesystem.join(self.results_directory, 'w3c_conformance_results.html')) |
| 258 |
|
| 259 |
|
| 260 |
class TestResultsConformanceParser(object): |
| 261 |
def __init__(self, port, base_directory): |
| 262 |
self._port = port |
| 263 |
self._filesystem = port.host.filesystem |
| 264 |
self._base_directory = base_directory |
| 265 |
|
| 266 |
def _get_test_results(self, tests, test_dict, path): |
| 267 |
for key in test_dict.keys(): |
| 268 |
_path = self._filesystem.join(path, key) |
| 269 |
_potential_test = test_dict[key] |
| 270 |
# Is it a test entry or a sub-directory |
| 271 |
if 'report' in _potential_test: |
| 272 |
# Store the test |
| 273 |
tests[_path] = _potential_test |
| 274 |
else: |
| 275 |
self._get_test_results(tests, _potential_test, _path) |
| 276 |
|
| 277 |
def _js_test_conformance(self, expected_text): |
| 278 |
# Test is said to PASS if at least one line is PASS and no line is FAIL |
| 279 |
hasFail = False |
| 280 |
hasPass = False |
| 281 |
for line in expected_text.splitlines(): |
| 282 |
if line.startswith('PASS'): |
| 283 |
hasPass = True |
| 284 |
elif line.startswith('FAIL'): |
| 285 |
hasFail = True |
| 286 |
if hasPass: |
| 287 |
return 'PASS' if not hasFail else 'PARTIAL' |
| 288 |
if hasFail: |
| 289 |
return 'FAIL' |
| 290 |
return 'ERROR' |
| 291 |
|
| 292 |
def _conformance_status(self, test_name, test_results): |
| 293 |
if not test_name in test_results: |
| 294 |
return {'type': 'reftest', 'status': 'PASS'} |
| 295 |
|
| 296 |
test_result = test_results[test_name] |
| 297 |
test_type = 'jstest' if ('is_missing_text' in test_result and test_result['is_missing_text']) else 'reftest' |
| 298 |
|
| 299 |
reason = test_result['actual'] |
| 300 |
if reason == 'CRASH' or reason == 'TIMEOUT': |
| 301 |
return {'type': test_type, 'status': reason} |
| 302 |
|
| 303 |
report = test_result['report'] |
| 304 |
if report == 'MISSING': |
| 305 |
text = self._port.expected_text(test_name) |
| 306 |
if not(text is None or len(text) == 0): |
| 307 |
return {'type': test_type, 'status': self._js_test_conformance(text)} |
| 308 |
elif report == 'REGRESSION': |
| 309 |
return {'type': test_type, 'status': 'FAIL'} |
| 310 |
|
| 311 |
return {'type': test_type, 'status': 'ERROR'} |
| 312 |
|
| 313 |
def w3c_test_name(self, test): |
| 314 |
return self._filesystem.relpath(test, self._base_directory) |
| 315 |
|
| 316 |
def compute_conformance_results(self, tests, json_results_filepath): |
| 317 |
test_results = {} |
| 318 |
results_json = json_results_generator.load_json(self._filesystem, json_results_filepath) |
| 319 |
self._get_test_results(test_results, results_json['tests'], self._filesystem.sep if self._filesystem.isabs(self._base_directory) else '') |
| 320 |
|
| 321 |
results = {} |
| 322 |
for test_name in tests: |
| 323 |
results[self.w3c_test_name(test_name)] = self._conformance_status(test_name, test_results) |
| 324 |
return results |
| 325 |
|
| 326 |
|
| 327 |
def configure_logging(): |
| 328 |
class LogHandler(logging.StreamHandler): |
| 329 |
def format(self, record): |
| 330 |
if record.levelno > logging.INFO: |
| 331 |
return "%s: %s" % (record.levelname, record.getMessage()) |
| 332 |
return record.getMessage() |
| 333 |
|
| 334 |
logger = logging.getLogger() |
| 335 |
logger.setLevel(logging.INFO) |
| 336 |
handler = LogHandler() |
| 337 |
handler.setLevel(logging.INFO) |
| 338 |
logger.addHandler(handler) |
| 339 |
return handler |
| 340 |
|
| 341 |
|
| 342 |
def main(argv, stdout, stderr): |
| 343 |
configure_logging() |
| 344 |
host = Host() |
| 345 |
parser = create_argument_parser() |
| 346 |
options, args = parser.parse_known_args(argv) |
| 347 |
# FIXME: We should pass all argparse options to the port factory. |
| 348 |
port = host.port_factory.get(options.platform, optparse.Values({'configuration': options.configuration, 'webkit_test_runner': options.webkit_test_runner})) |
| 349 |
|
| 350 |
runner = ConformanceChecker(port, options) |
| 351 |
if runner.check_conformance(): |
| 352 |
runner.print_summary_to_console() |
| 353 |
_log.info("Results exported in '" + runner.results_directory + "'") |
| 354 |
|
| 355 |
|
| 356 |
if '__main__' == __name__: |
| 357 |
sys.exit(main(sys.argv[1:], sys.stdout, sys.stderr)) |