OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The PDFium Authors. All rights reserved. | 2 # Copyright 2015 The PDFium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import cStringIO | 6 import cStringIO |
7 import functools | 7 import functools |
8 import multiprocessing | 8 import multiprocessing |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
11 import re | 11 import re |
12 import shutil | 12 import shutil |
13 import subprocess | 13 import subprocess |
14 import sys | 14 import sys |
15 | 15 |
16 import common | 16 import common |
17 import pngdiffer | 17 import pngdiffer |
18 import suppressor | 18 import suppressor |
19 | 19 |
20 class KeyboardInterruptError(Exception): pass | 20 class KeyboardInterruptError(Exception): pass |
21 | 21 |
22 # Nomenclature: | 22 # Nomenclature: |
23 # x_root - "x" | 23 # x_root - "x" |
24 # x_filename - "x.ext" | 24 # x_filename - "x.ext" |
25 # x_path - "path/to/a/b/c/x.ext" | 25 # x_path - "path/to/a/b/c/x.ext" |
26 # c_dir - "path/to/a/b/c" | 26 # c_dir - "path/to/a/b/c" |
27 | 27 |
28 def test_one_file(input_filename, source_dir, working_dir, | 28 def test_one_file(input_filename, source_dir, working_dir, |
29 pdfium_test_path, image_differ, redirect_output=False): | 29 pdfium_test_path, image_differ, drmem_wrapper, |
30 redirect_output=False): | |
30 input_path = os.path.join(source_dir, input_filename) | 31 input_path = os.path.join(source_dir, input_filename) |
31 pdf_path = os.path.join(working_dir, input_filename) | 32 pdf_path = os.path.join(working_dir, input_filename) |
32 | |
33 # Remove any existing generated images from previous runs. | 33 # Remove any existing generated images from previous runs. |
34 actual_images = image_differ.GetActualFiles( | 34 actual_images = image_differ.GetActualFiles( |
35 input_filename, source_dir, working_dir) | 35 input_filename, source_dir, working_dir) |
36 for image in actual_images: | 36 for image in actual_images: |
37 if os.path.exists(image): | 37 if os.path.exists(image): |
38 os.remove(image) | 38 os.remove(image) |
39 | 39 |
40 shutil.copyfile(input_path, pdf_path) | 40 shutil.copyfile(input_path, pdf_path) |
41 sys.stdout.flush() | 41 sys.stdout.flush() |
42 error = common.RunCommand([pdfium_test_path, '--png', pdf_path], | 42 # add Dr. Memory wrapper if exist |
43 redirect_output) | 43 if drmem_wrapper: |
44 cmd_to_run = common.DrMemoryWrapper(drmem_wrapper, input_filename) | |
45 else: | |
46 cmd_to_run = [] | |
Lei Zhang
2015/11/20 00:17:47
Can DrMemoryWrapper() just return [] instead for a
zhaoqin1
2015/11/21 04:20:45
Done.
| |
47 cmd_to_run.extend([pdfium_test_path, '--png', pdf_path]) | |
48 # run test | |
49 error = common.RunCommand(cmd_to_run, redirect_output) | |
44 if error: | 50 if error: |
45 print "FAILURE: " + input_filename + "; " + str(error) | 51 print "FAILURE: " + input_filename + "; " + str(error) |
46 return False | 52 return False |
47 return not image_differ.HasDifferences(input_filename, source_dir, | 53 return not image_differ.HasDifferences(input_filename, source_dir, |
48 working_dir, redirect_output) | 54 working_dir, redirect_output) |
49 | 55 |
50 | 56 |
51 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, | 57 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, |
52 test_case): | 58 test_case): |
53 """Wrapper function to call test_one_file() and redirect output to stdout.""" | 59 """Wrapper function to call test_one_file() and redirect output to stdout.""" |
54 try: | 60 try: |
55 old_stdout = sys.stdout | 61 old_stdout = sys.stdout |
56 old_stderr = sys.stderr | 62 old_stderr = sys.stderr |
57 sys.stdout = cStringIO.StringIO() | 63 sys.stdout = cStringIO.StringIO() |
58 sys.stderr = sys.stdout | 64 sys.stderr = sys.stdout |
59 input_filename, source_dir = test_case | 65 input_filename, source_dir = test_case |
60 result = test_one_file(input_filename, source_dir, working_dir, | 66 result = test_one_file(input_filename, source_dir, working_dir, |
61 pdfium_test_path, image_differ, True); | 67 pdfium_test_path, image_differ, "", True); |
62 output = sys.stdout | 68 output = sys.stdout |
63 sys.stdout = old_stdout | 69 sys.stdout = old_stdout |
64 sys.stderr = old_stderr | 70 sys.stderr = old_stderr |
65 return (result, output.getvalue(), input_filename, source_dir) | 71 return (result, output.getvalue(), input_filename, source_dir) |
66 except KeyboardInterrupt: | 72 except KeyboardInterrupt: |
67 raise KeyboardInterruptError() | 73 raise KeyboardInterruptError() |
68 | 74 |
69 | 75 |
70 def handle_result(test_suppressor, input_filename, input_path, result, | 76 def handle_result(test_suppressor, input_filename, input_path, result, |
71 surprises, failures): | 77 surprises, failures): |
72 if test_suppressor.IsSuppressed(input_filename): | 78 if test_suppressor.IsSuppressed(input_filename): |
73 if result: | 79 if result: |
74 surprises.append(input_path) | 80 surprises.append(input_path) |
75 else: | 81 else: |
76 if not result: | 82 if not result: |
77 failures.append(input_path) | 83 failures.append(input_path) |
78 | 84 |
79 | 85 |
80 def main(): | 86 def main(): |
81 parser = optparse.OptionParser() | 87 parser = optparse.OptionParser() |
82 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 88 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
83 help='relative path from the base source directory') | 89 help='relative path from the base source directory') |
84 parser.add_option('-j', default=multiprocessing.cpu_count(), | 90 parser.add_option('-j', default=multiprocessing.cpu_count(), |
85 dest='num_workers', type='int', | 91 dest='num_workers', type='int', |
86 help='run NUM_WORKERS jobs in parallel') | 92 help='run NUM_WORKERS jobs in parallel') |
93 parser.add_option('--wrapper', default='', dest="wrapper", | |
94 help='Dr. Memory wrapper for running test under Dr. Memory') | |
87 options, args = parser.parse_args() | 95 options, args = parser.parse_args() |
88 finder = common.DirectoryFinder(options.build_dir) | 96 finder = common.DirectoryFinder(options.build_dir) |
89 pdfium_test_path = finder.ExecutablePath('pdfium_test') | 97 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
90 if not os.path.exists(pdfium_test_path): | 98 if not os.path.exists(pdfium_test_path): |
91 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path | 99 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path |
92 print "Use --build-dir to specify its location." | 100 print "Use --build-dir to specify its location." |
93 return 1 | 101 return 1 |
94 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) | 102 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
95 if not os.path.exists(working_dir): | 103 if not os.path.exists(working_dir): |
96 os.makedirs(working_dir) | 104 os.makedirs(working_dir) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 pdfium_test_path, image_differ) | 137 pdfium_test_path, image_differ) |
130 worker_results = pool.imap(worker_func, test_cases) | 138 worker_results = pool.imap(worker_func, test_cases) |
131 for worker_result in worker_results: | 139 for worker_result in worker_results: |
132 result, output, input_filename, source_dir = worker_result | 140 result, output, input_filename, source_dir = worker_result |
133 input_path = os.path.join(source_dir, input_filename) | 141 input_path = os.path.join(source_dir, input_filename) |
134 sys.stdout.write(output) | 142 sys.stdout.write(output) |
135 handle_result(test_suppressor, input_filename, input_path, result, | 143 handle_result(test_suppressor, input_filename, input_path, result, |
136 surprises, failures) | 144 surprises, failures) |
137 pool.close() | 145 pool.close() |
138 except KeyboardInterrupt: | 146 except KeyboardInterrupt: |
147 print "terminated by keyborad" | |
Lei Zhang
2015/11/20 00:17:47
typo: keyboard
zhaoqin1
2015/11/21 04:20:45
debugging statement, removed
| |
139 pool.terminate() | 148 pool.terminate() |
140 finally: | 149 finally: |
141 pool.join() | 150 pool.join() |
142 else: | 151 else: |
143 for test_case in test_cases: | 152 for test_case in test_cases: |
144 input_filename, source_dir = test_case | 153 input_filename, source_dir = test_case |
145 result = test_one_file(input_filename, source_dir, working_dir, | 154 result = test_one_file(input_filename, source_dir, working_dir, |
146 pdfium_test_path, image_differ) | 155 pdfium_test_path, image_differ, |
156 options.wrapper) | |
147 handle_result(test_suppressor, input_filename, input_path, result, | 157 handle_result(test_suppressor, input_filename, input_path, result, |
148 surprises, failures) | 158 surprises, failures) |
149 | 159 |
150 if surprises: | 160 if surprises: |
151 surprises.sort() | 161 surprises.sort() |
152 print '\n\nUnexpected Successes:' | 162 print '\n\nUnexpected Successes:' |
153 for surprise in surprises: | 163 for surprise in surprises: |
154 print surprise; | 164 print surprise; |
155 | 165 |
156 if failures: | 166 if failures: |
157 failures.sort() | 167 failures.sort() |
158 print '\n\nSummary of Failures:' | 168 print '\n\nSummary of Failures:' |
159 for failure in failures: | 169 for failure in failures: |
160 print failure | 170 print failure |
161 return 1 | 171 return 1 |
162 | 172 |
163 return 0 | 173 return 0 |
164 | 174 |
165 | 175 |
166 if __name__ == '__main__': | 176 if __name__ == '__main__': |
167 sys.exit(main()) | 177 sys.exit(main()) |
OLD | NEW |