| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 # for details. All rights reserved. Use of this source code is governed by a | |
| 3 # BSD-style license that can be found in the LICENSE file. | |
| 4 # | |
| 5 # For now we have to use this trampoline to turn --dart-flags command line | |
| 6 # switch into env variable DART_FLAGS. Eventually, DumpRenderTree should | |
| 7 # support --dart-flags and this hack may go away. | |
| 8 # | |
| 9 # Expected invocation: python drt-trampoline.py <path to DRT> <DRT command line> | |
| 10 | |
| 11 import optparse | |
| 12 import os | |
| 13 import signal | |
| 14 import subprocess | |
| 15 import sys | |
| 16 | |
| 17 def parse_options(argv): | |
| 18 parser = optparse.OptionParser() | |
| 19 parser.add_option('--dart-flags', | |
| 20 metavar='FLAGS', | |
| 21 dest='dart_flags') | |
| 22 parser.add_option('--out-expectation', | |
| 23 metavar='FILE', | |
| 24 dest='out_expected_file') | |
| 25 parser.add_option('--package-root', | |
| 26 metavar='DIRECTORY', | |
| 27 dest='dart_package_root') | |
| 28 parser.add_option('--no-timeout', | |
| 29 action='store_true') | |
| 30 return parser.parse_args(args=argv) | |
| 31 | |
| 32 | |
| 33 def main(argv): | |
| 34 drt_path = argv[1] | |
| 35 (options, arguments) = parse_options(argv[2:]) | |
| 36 | |
| 37 cmd = [drt_path] | |
| 38 | |
| 39 env = None | |
| 40 test_file = None | |
| 41 dart_flags = options.dart_flags | |
| 42 out_expected_file = options.out_expected_file | |
| 43 dart_package_root = options.dart_package_root | |
| 44 is_png = False | |
| 45 | |
| 46 if dart_flags: | |
| 47 if not env: | |
| 48 env = dict(os.environ.items()) | |
| 49 env['DART_FLAGS'] = dart_flags | |
| 50 | |
| 51 if dart_package_root: | |
| 52 if not env: | |
| 53 env = dict(os.environ.items()) | |
| 54 absolute_path = os.path.abspath(dart_package_root) | |
| 55 absolute_path = absolute_path.replace(os.path.sep, '/') | |
| 56 if not absolute_path.startswith('/'): | |
| 57 # Happens on Windows for C:\packages | |
| 58 absolute_path = '/%s' % absolute_path | |
| 59 env['DART_PACKAGE_ROOT'] = 'file://%s' % absolute_path | |
| 60 | |
| 61 if out_expected_file: | |
| 62 if out_expected_file.endswith('.png'): | |
| 63 cmd.append('--notree') | |
| 64 is_png = True | |
| 65 elif not out_expected_file.endswith('.txt'): | |
| 66 raise Exception( | |
| 67 'Bad file expectation (%s) please specify either a .txt or a .png file' | |
| 68 % out_expected_file) | |
| 69 | |
| 70 if options.no_timeout: | |
| 71 cmd.append('--no-timeout') | |
| 72 | |
| 73 for arg in arguments: | |
| 74 if '.html' in arg: | |
| 75 test_file = arg | |
| 76 else: | |
| 77 cmd.append(arg) | |
| 78 | |
| 79 if is_png: | |
| 80 # pixel tests are specified by running DRT "foo.html'-p" | |
| 81 cmd.append(test_file + "'-p") | |
| 82 else: | |
| 83 cmd.append(test_file) | |
| 84 | |
| 85 stdout = subprocess.PIPE if out_expected_file else None | |
| 86 p = subprocess.Popen(cmd, env=env, stdout=stdout) | |
| 87 | |
| 88 def signal_handler(signal, frame): | |
| 89 p.terminate() | |
| 90 sys.exit(0) | |
| 91 | |
| 92 # SIGINT is Ctrl-C. | |
| 93 signal.signal(signal.SIGINT, signal_handler) | |
| 94 # SIGTERM is sent by test.dart when a process times out. | |
| 95 signal.signal(signal.SIGTERM, signal_handler) | |
| 96 output, error = p.communicate() | |
| 97 signal.signal(signal.SIGINT, signal.SIG_DFL) | |
| 98 signal.signal(signal.SIGTERM, signal.SIG_DFL) | |
| 99 | |
| 100 if p.returncode != 0: | |
| 101 raise Exception('Failed to run command. return code=%s' % p.returncode) | |
| 102 | |
| 103 if out_expected_file: | |
| 104 # Compare output to the given expectation file. | |
| 105 expectation = None | |
| 106 if is_png: | |
| 107 # DRT prints the image to STDOUT, but includes extra text that we trim: | |
| 108 # - several header lines until a line saying 'Content-Length:' | |
| 109 # - a '#EOF\n' at the end | |
| 110 last_header_line = output.find('Content-Length:') | |
| 111 start_pos = output.find('\n', last_header_line) + 1 | |
| 112 output = output[start_pos : -len('#EOF\n')] | |
| 113 if os.path.exists(out_expected_file): | |
| 114 with open(out_expected_file, 'r') as f: | |
| 115 expectation = f.read() | |
| 116 else: | |
| 117 # Instructions on how to create the expectation will be printed below | |
| 118 # (outout != expectation) | |
| 119 print 'File %s was not found' % out_expected_file | |
| 120 expectation = None | |
| 121 | |
| 122 # Report test status using the format test.dart expects to see from DRT. | |
| 123 print 'Content-Type: text/plain' | |
| 124 if expectation == output: | |
| 125 print 'PASS' | |
| 126 print 'Expectation matches' | |
| 127 else: | |
| 128 # Generate a temporary file in the same place as the .html file: | |
| 129 out_file = test_file[:test_file.rfind('.html')] + out_expected_file[-4:] | |
| 130 with open(out_file, 'w') as f: | |
| 131 f.write(output) | |
| 132 print 'FAIL' | |
| 133 print 'Expectation didn\'t match.\n' | |
| 134 if len(output) == 0: | |
| 135 print ('\033[31mERROR\033[0m: DumpRenderTree generated an empty pixel ' | |
| 136 'output! This is commonly an error in executing DumpRenderTree, and' | |
| 137 ' not that expectations are out of date.\n') | |
| 138 print 'You can update expectations by running:\n' | |
| 139 print 'cp %s %s\n' % (out_file, out_expected_file) | |
| 140 print '#EOF' | |
| 141 | |
| 142 if __name__ == '__main__': | |
| 143 try: | |
| 144 sys.exit(main(sys.argv)) | |
| 145 except StandardError as e: | |
| 146 print 'Fail: ' + str(e) | |
| 147 sys.exit(1) | |
| OLD | NEW |