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 | |
7 import functools | |
8 import multiprocessing | |
6 import optparse | 9 import optparse |
7 import os | 10 import os |
8 import re | 11 import re |
9 import shutil | 12 import shutil |
10 import subprocess | 13 import subprocess |
11 import sys | 14 import sys |
12 | 15 |
13 import common | 16 import common |
14 import pngdiffer | 17 import pngdiffer |
15 import suppressor | 18 import suppressor |
16 | 19 |
17 # Nomenclature: | 20 # Nomenclature: |
18 # x_root - "x" | 21 # x_root - "x" |
19 # x_filename - "x.ext" | 22 # x_filename - "x.ext" |
20 # x_path - "path/to/a/b/c/x.ext" | 23 # x_path - "path/to/a/b/c/x.ext" |
21 # c_dir - "path/to/a/b/c" | 24 # c_dir - "path/to/a/b/c" |
22 | 25 |
23 def test_one_file(input_filename, source_dir, working_dir, | 26 def test_one_file(input_filename, source_dir, working_dir, |
24 pdfium_test_path, image_differ): | 27 pdfium_test_path, image_differ, redirect_output=False): |
25 input_path = os.path.join(source_dir, input_filename) | 28 input_path = os.path.join(source_dir, input_filename) |
26 pdf_path = os.path.join(working_dir, input_filename) | 29 pdf_path = os.path.join(working_dir, input_filename) |
27 | 30 |
28 # Remove any existing generated images from previous runs. | 31 # Remove any existing generated images from previous runs. |
29 actual_images = image_differ.GetActualFiles( | 32 actual_images = image_differ.GetActualFiles( |
30 input_filename, source_dir, working_dir) | 33 input_filename, source_dir, working_dir) |
31 for image in actual_images: | 34 for image in actual_images: |
32 if os.path.exists(image): | 35 if os.path.exists(image): |
33 os.remove(image) | 36 os.remove(image) |
34 | 37 |
35 try: | 38 shutil.copyfile(input_path, pdf_path) |
36 shutil.copyfile(input_path, pdf_path) | 39 sys.stdout.flush() |
37 sys.stdout.flush() | 40 error = common.RunCommand([pdfium_test_path, '--png', pdf_path], |
38 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) | 41 redirect_output) |
39 except subprocess.CalledProcessError as e: | 42 if error: |
40 print "FAILURE: " + input_filename + "; " + str(e) | 43 print "FAILURE: " + input_filename + "; " + str(error) |
41 return False | 44 return False |
42 if image_differ.HasDifferences(input_filename, source_dir, working_dir): | 45 return not image_differ.HasDifferences(input_filename, source_dir, |
43 return False | 46 working_dir, redirect_output) |
44 return True | 47 |
48 | |
49 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, datum): | |
Nathan Parker
2015/10/09 19:55:04
Nit: Some comments would be useful, especially for
Lei Zhang
2015/10/09 20:14:33
I added a brief comment and renamed datum to test_
| |
50 old_stdout = sys.stdout | |
51 old_stderr = sys.stderr | |
52 sys.stdout = cStringIO.StringIO() | |
53 sys.stderr = sys.stdout | |
54 input_filename, source_dir = datum | |
55 result = test_one_file(input_filename, source_dir, working_dir, | |
56 pdfium_test_path, image_differ, True); | |
57 output = sys.stdout | |
58 sys.stdout = old_stdout | |
59 sys.stderr = old_stderr | |
60 output.seek(0) | |
61 return (result, output.read(), input_filename, source_dir) | |
Nathan Parker
2015/10/09 19:55:04
I think .getvalue() works the same as .seek(0) + .
Lei Zhang
2015/10/09 20:14:33
Done.
| |
62 | |
63 | |
64 def handle_result(test_suppressor, input_filename, input_path, result, | |
65 surprises, failures): | |
66 if test_suppressor.IsSuppressed(input_filename): | |
67 if result: | |
68 surprises.append(input_path) | |
69 else: | |
70 if not result: | |
71 failures.append(input_path) | |
45 | 72 |
46 | 73 |
47 def main(): | 74 def main(): |
48 parser = optparse.OptionParser() | 75 parser = optparse.OptionParser() |
49 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 76 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
50 help='relative path from the base source directory') | 77 help='relative path from the base source directory') |
78 parser.add_option('-j', default=multiprocessing.cpu_count(), | |
79 dest='num_workers', type='int', | |
80 help='run NUM_WORKERS jobs in parallel') | |
51 options, args = parser.parse_args() | 81 options, args = parser.parse_args() |
52 finder = common.DirectoryFinder(options.build_dir) | 82 finder = common.DirectoryFinder(options.build_dir) |
53 pdfium_test_path = finder.ExecutablePath('pdfium_test') | 83 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
54 if not os.path.exists(pdfium_test_path): | 84 if not os.path.exists(pdfium_test_path): |
55 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path | 85 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path |
56 print "Use --build-dir to specify its location." | 86 print "Use --build-dir to specify its location." |
57 return 1 | 87 return 1 |
58 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) | 88 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
59 if not os.path.exists(working_dir): | 89 if not os.path.exists(working_dir): |
60 os.makedirs(working_dir) | 90 os.makedirs(working_dir) |
61 | 91 |
62 test_suppressor = suppressor.Suppressor(finder) | 92 test_suppressor = suppressor.Suppressor(finder) |
63 image_differ = pngdiffer.PNGDiffer(finder) | 93 image_differ = pngdiffer.PNGDiffer(finder) |
64 | 94 |
65 # test files are under .../pdfium/testing/corpus. | 95 # test files are under .../pdfium/testing/corpus. |
66 failures = [] | 96 failures = [] |
67 surprises = [] | 97 surprises = [] |
68 walk_from_dir = finder.TestingDir('corpus'); | 98 walk_from_dir = finder.TestingDir('corpus'); |
69 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') | 99 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') |
100 test_cases = [] | |
70 for source_dir, _, filename_list in os.walk(walk_from_dir): | 101 for source_dir, _, filename_list in os.walk(walk_from_dir): |
71 for input_filename in filename_list: | 102 for input_filename in filename_list: |
72 if input_file_re.match(input_filename): | 103 if input_file_re.match(input_filename): |
73 input_path = os.path.join(source_dir, input_filename) | 104 input_path = os.path.join(source_dir, input_filename) |
74 if os.path.isfile(input_path): | 105 if os.path.isfile(input_path): |
75 result = test_one_file(input_filename, source_dir, working_dir, | 106 test_cases.append((input_filename, source_dir)) |
76 pdfium_test_path, image_differ) | 107 |
77 if test_suppressor.IsSuppressed(input_filename): | 108 if options.num_workers > 1: |
78 if result: | 109 pool = multiprocessing.Pool(options.num_workers) |
79 surprises.append(input_path) | 110 worker_func = functools.partial(test_one_file_parallel, working_dir, |
80 else: | 111 pdfium_test_path, image_differ) |
81 if not result: | 112 worker_results = pool.imap(worker_func, test_cases) |
Nathan Parker
2015/10/09 19:55:04
Do you want a timeout on these? Or is there a high
Lei Zhang
2015/10/09 20:14:33
I believe buildbots have a timeout of its own. The
| |
82 failures.append(input_path) | 113 for worker_result in worker_results: |
114 result, output, input_filename, source_dir = worker_result | |
115 input_path = os.path.join(source_dir, input_filename) | |
116 sys.stdout.write(output) | |
117 handle_result(test_suppressor, input_filename, input_path, result, | |
118 surprises, failures) | |
119 else: | |
120 for test_case in test_cases: | |
121 input_filename, source_dir = test_case | |
122 result = test_one_file(input_filename, source_dir, working_dir, | |
123 pdfium_test_path, image_differ) | |
124 handle_result(test_suppressor, input_filename, input_path, result, | |
125 surprises, failures) | |
83 | 126 |
84 if surprises: | 127 if surprises: |
85 surprises.sort() | 128 surprises.sort() |
86 print '\n\nUnexpected Successes:' | 129 print '\n\nUnexpected Successes:' |
87 for surprise in surprises: | 130 for surprise in surprises: |
88 print surprise; | 131 print surprise; |
89 | 132 |
90 if failures: | 133 if failures: |
91 failures.sort() | 134 failures.sort() |
92 print '\n\nSummary of Failures:' | 135 print '\n\nSummary of Failures:' |
93 for failure in failures: | 136 for failure in failures: |
94 print failure | 137 print failure |
95 return 1 | 138 return 1 |
96 | 139 |
97 return 0 | 140 return 0 |
98 | 141 |
99 | 142 |
100 if __name__ == '__main__': | 143 if __name__ == '__main__': |
101 sys.exit(main()) | 144 sys.exit(main()) |
OLD | NEW |