| OLD | NEW |
| 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 # BSD-style license that can be found in the LICENSE file. |
| 4 # | 4 # |
| 5 # For now we have to use this trampoline to turn --dart-flags command line | 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 | 6 # switch into env variable DART_FLAGS. Eventually, DumpRenderTree should |
| 7 # support --dart-flags and this hack may go away. | 7 # support --dart-flags and this hack may go away. |
| 8 # | 8 # |
| 9 # Expected invocation: python drt-trampoline.py <path to DRT> <DRT command line> | 9 # Expected invocation: python drt-trampoline.py <path to DRT> <DRT command line> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 is_png = False | 27 is_png = False |
| 28 | 28 |
| 29 # parse arguments, filtering out flags, and selecting the input test file | 29 # parse arguments, filtering out flags, and selecting the input test file |
| 30 for arg in command_line: | 30 for arg in command_line: |
| 31 if arg.startswith(DART_FLAGS_PREFIX): | 31 if arg.startswith(DART_FLAGS_PREFIX): |
| 32 env = dict(os.environ.items()) | 32 env = dict(os.environ.items()) |
| 33 env['DART_FLAGS'] = arg[len(DART_FLAGS_PREFIX):] | 33 env['DART_FLAGS'] = arg[len(DART_FLAGS_PREFIX):] |
| 34 elif arg.startswith(OUT_EXPECTATION_PREFIX): | 34 elif arg.startswith(OUT_EXPECTATION_PREFIX): |
| 35 out_expected_file = arg[len(OUT_EXPECTATION_PREFIX):] | 35 out_expected_file = arg[len(OUT_EXPECTATION_PREFIX):] |
| 36 if out_expected_file.endswith('.png'): | 36 if out_expected_file.endswith('.png'): |
| 37 cmd.append('--pixel-tests') | |
| 38 cmd.append('--notree') | 37 cmd.append('--notree') |
| 39 is_png = True | 38 is_png = True |
| 40 elif not out_expected_file.endswith('.txt'): | 39 elif not out_expected_file.endswith('.txt'): |
| 41 raise Exception( | 40 raise Exception( |
| 42 'Bad file expectation (%s), ' % out_expected_file | 41 'Bad file expectation (%s), ' % out_expected_file |
| 43 + 'please specify either a .txt or a .png file') | 42 + 'please specify either a .txt or a .png file') |
| 44 elif arg.endswith('.html'): | 43 elif arg.endswith('.html'): |
| 45 test_file = arg | 44 test_file = arg |
| 46 cmd.append(arg) | |
| 47 else: | 45 else: |
| 48 cmd.append(arg) | 46 cmd.append(arg) |
| 49 | 47 |
| 48 if is_png: |
| 49 # pixel tests are specified by running DRT "foo.html'-p" |
| 50 cmd.append(test_file + "'-p") |
| 51 else: |
| 52 cmd.append(test_file) |
| 50 | 53 |
| 51 stdout = subprocess.PIPE if out_expected_file else None | 54 stdout = subprocess.PIPE if out_expected_file else None |
| 52 p = subprocess.Popen(cmd, env=env, stdout=stdout) | 55 p = subprocess.Popen(cmd, env=env, stdout=stdout) |
| 53 output, error = p.communicate() | 56 output, error = p.communicate() |
| 54 if p.returncode != 0: | 57 if p.returncode != 0: |
| 55 raise Exception('Failed to run command. return code=%s' % p.returncode) | 58 raise Exception('Failed to run command. return code=%s' % p.returncode) |
| 56 | 59 |
| 57 if out_expected_file: | 60 if out_expected_file: |
| 58 # Compare output to the given expectation file. | 61 # Compare output to the given expectation file. |
| 59 expectation = None | 62 expectation = None |
| 60 if is_png: | 63 if is_png: |
| 61 # DRT prints the image to STDOUT, but includes extra text that we trim: | 64 # DRT prints the image to STDOUT, but includes extra text that we trim: |
| 62 # - 4 header lines | 65 # - several header lines until a line saying 'Content-Length:' |
| 63 # - a '#EOF\n' at the end | 66 # - a '#EOF\n' at the end |
| 64 output = output.replace('\n', '_', 3) | 67 last_header_line = output.find('Content-Length:') |
| 65 output = output[output.find('\n') + 1: -len('#EOF\n')] | 68 start_pos = output.find('\n', last_header_line) + 1 |
| 69 output = output[start_pos : -len('#EOF\n')] |
| 66 if os.path.exists(out_expected_file): | 70 if os.path.exists(out_expected_file): |
| 67 with open(out_expected_file, 'r') as f: | 71 with open(out_expected_file, 'r') as f: |
| 68 expectation = f.read() | 72 expectation = f.read() |
| 69 else: | 73 else: |
| 70 # Instructions on how to create the expectation will be printed below | 74 # Instructions on how to create the expectation will be printed below |
| 71 # (outout != expectation) | 75 # (outout != expectation) |
| 72 print 'File %s was not found' % out_expected_file | 76 print 'File %s was not found' % out_expected_file |
| 73 expectation = None | 77 expectation = None |
| 74 | 78 |
| 75 # Report test status using the format test.dart expects to see from DRT. | 79 # Report test status using the format test.dart expects to see from DRT. |
| 76 print 'Content-Type: text/plain' | 80 print 'Content-Type: text/plain' |
| 77 if expectation == output: | 81 if expectation == output: |
| 78 print 'PASS' | 82 print 'PASS' |
| 79 print 'Expectation matches' | 83 print 'Expectation matches' |
| 80 else: | 84 else: |
| 81 # Generate a temporary file in the same place as the .html file: | 85 # Generate a temporary file in the same place as the .html file: |
| 82 out_file = test_file[:test_file.rfind('.html')] + out_expected_file[-4:] | 86 out_file = test_file[:test_file.rfind('.html')] + out_expected_file[-4:] |
| 83 with open(out_file, 'w') as f: | 87 with open(out_file, 'w') as f: |
| 84 f.write(output) | 88 f.write(output) |
| 85 print 'FAIL' | 89 print 'FAIL' |
| 86 print 'Expectation didn\'t match. Update expectations by running:\n' | 90 print 'Expectation didn\'t match.\n' |
| 91 if len(output) == 0: |
| 92 print ('\033[31mERROR\033[0m: DumpRenderTree generated an empty pixel ' |
| 93 'output! This is commonly an error in executing DumpRenderTree, and' |
| 94 ' not that expectations are out of date.\n') |
| 95 print 'You can update expectations by running:\n' |
| 87 print 'cp %s %s\n' % (out_file, out_expected_file) | 96 print 'cp %s %s\n' % (out_file, out_expected_file) |
| 88 print '#EOF' | 97 print '#EOF' |
| 89 | 98 |
| 90 if __name__ == '__main__': | 99 if __name__ == '__main__': |
| 91 try: | 100 try: |
| 92 sys.exit(main(sys.argv)) | 101 sys.exit(main(sys.argv)) |
| 93 except StandardError as e: | 102 except StandardError as e: |
| 94 print 'Fail: ' + str(e) | 103 print 'Fail: ' + str(e) |
| 95 sys.exit(1) | 104 sys.exit(1) |
| OLD | NEW |