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 |
11 import os | 11 import os |
12 import subprocess | 12 import subprocess |
13 import sys | 13 import sys |
14 | 14 |
15 DART_FLAGS_PREFIX = '--dart-flags=' | 15 DART_FLAGS_PREFIX = '--dart-flags=' |
| 16 OUT_EXPECTATION_PREFIX = '--out-expectation=' |
16 | 17 |
17 def main(argv): | 18 def main(argv): |
18 drt_path = argv[1] | 19 drt_path = argv[1] |
19 command_line = argv[2:] | 20 command_line = argv[2:] |
20 | 21 |
21 cmd = [drt_path] | 22 cmd = [drt_path] |
22 | 23 |
23 env = None | 24 env = None |
| 25 test_file = None |
| 26 out_expected_file = None |
| 27 is_png = False |
| 28 |
| 29 # parse arguments, filtering out flags, and selecting the input test file |
24 for arg in command_line: | 30 for arg in command_line: |
25 if arg.startswith(DART_FLAGS_PREFIX): | 31 if arg.startswith(DART_FLAGS_PREFIX): |
26 env = dict(os.environ.items()) | 32 env = dict(os.environ.items()) |
27 env['DART_FLAGS'] = arg[len(DART_FLAGS_PREFIX):] | 33 env['DART_FLAGS'] = arg[len(DART_FLAGS_PREFIX):] |
| 34 elif arg.startswith(OUT_EXPECTATION_PREFIX): |
| 35 out_expected_file = arg[len(OUT_EXPECTATION_PREFIX):] |
| 36 if out_expected_file.endswith('.png'): |
| 37 cmd.append('--pixel-tests') |
| 38 cmd.append('--notree') |
| 39 is_png = True |
| 40 elif not out_expected_file.endswith('.txt'): |
| 41 raise Exception( |
| 42 'Bad file expectation (%s), ' % out_expected_file |
| 43 + 'please specify either a .txt or a .png file') |
| 44 elif arg.endswith('.html'): |
| 45 test_file = arg |
| 46 cmd.append(arg) |
28 else: | 47 else: |
29 cmd.append(arg) | 48 cmd.append(arg) |
30 | 49 |
31 p = subprocess.Popen(cmd, env=env) | 50 |
| 51 p = subprocess.Popen(cmd, env=env, stdout=subprocess.PIPE) |
32 p.wait() | 52 p.wait() |
33 if p.returncode != 0: | 53 if p.returncode != 0: |
34 raise Exception('Failed to run command. return code=%s' % p.returncode) | 54 raise Exception('Failed to run command. return code=%s' % p.returncode) |
35 | 55 |
| 56 if out_expected_file: |
| 57 # Compare output to the given expectation file. |
| 58 output = None |
| 59 expectation = None |
| 60 with p.stdout as res: |
| 61 if is_png: |
| 62 # DRT prints the image to STDOUT, but includes 5 header lines. |
| 63 for i in range(4): res.readline() |
| 64 output = res.read() |
| 65 if os.path.exists(out_expected_file): |
| 66 with open(out_expected_file, 'r') as f: |
| 67 expectation = f.read() |
| 68 else: |
| 69 # Instructions on how to create the expectation will be printed below |
| 70 # (outout != expectation) |
| 71 print 'File %s was not found' % out_expected_file |
| 72 expectation = None |
| 73 |
| 74 # Report test status using the format test.dart expects to see from DRT. |
| 75 print 'Content-Type: text/plain' |
| 76 if expectation == output: |
| 77 print 'PASS' |
| 78 print 'Expectation matches' |
| 79 else: |
| 80 # Generate a temporary file in the same place as the .html file: |
| 81 out_file = test_file[:test_file.rfind('.html')] + out_expected_file[-4:] |
| 82 with open(out_file, 'w') as f: |
| 83 f.write(output) |
| 84 print 'FAIL' |
| 85 print 'Expectation didn\'t match. Update expectations by running:\n' |
| 86 print 'cp %s %s\n' % (out_file, out_expected_file) |
| 87 print '#EOF' |
| 88 else: |
| 89 # Pipe through the output for non-layout tests. |
| 90 sys.stdout.write(p.stdout.read()) |
36 | 91 |
37 if __name__ == '__main__': | 92 if __name__ == '__main__': |
38 try: | 93 try: |
39 sys.exit(main(sys.argv)) | 94 sys.exit(main(sys.argv)) |
40 except StandardError as e: | 95 except StandardError as e: |
41 print 'Fail: ' + str(e) | 96 print 'Fail: ' + str(e) |
42 sys.exit(1) | 97 sys.exit(1) |
OLD | NEW |