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 optparse | 6 import optparse |
7 import os | 7 import os |
8 import re | 8 import re |
| 9 import shutil |
9 import subprocess | 10 import subprocess |
10 import shutil | |
11 import sys | 11 import sys |
12 | 12 |
| 13 import common |
| 14 import pngdiffer |
| 15 import suppressor |
| 16 |
13 # Nomenclature: | 17 # Nomenclature: |
14 # x_root - "x" | 18 # x_root - "x" |
15 # x_filename - "x.ext" | 19 # x_filename - "x.ext" |
16 # x_path - "path/to/a/b/c/x.ext" | 20 # x_path - "path/to/a/b/c/x.ext" |
17 # c_dir - "path/to/a/b/c" | 21 # c_dir - "path/to/a/b/c" |
18 | 22 |
19 def extract_suppressions(filename): | |
20 with open(filename) as f: | |
21 suppressions = [y for y in [ | |
22 x.split('#')[0].strip() for x in f.readlines()] if y] | |
23 return suppressions | |
24 | |
25 def test_one_file(input_filename, source_dir, working_dir, | 23 def test_one_file(input_filename, source_dir, working_dir, |
26 pdfium_test_path, pdfium_diff_path): | 24 pdfium_test_path, image_differ): |
27 input_root, _ = os.path.splitext(input_filename) | |
28 input_path = os.path.join(source_dir, input_filename) | 25 input_path = os.path.join(source_dir, input_filename) |
29 pdf_path = os.path.join(working_dir, input_filename) | 26 pdf_path = os.path.join(working_dir, input_filename) |
30 actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.png') | |
31 expected_path_template = os.path.join(source_dir, | |
32 input_root + '_expected.pdf.%d.png') | |
33 try: | 27 try: |
34 shutil.copyfile(input_path, pdf_path) | 28 shutil.copyfile(input_path, pdf_path) |
35 sys.stdout.flush() | 29 sys.stdout.flush() |
36 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) | 30 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) |
37 i = 0; | |
38 while True: | |
39 expected_path = expected_path_template % i; | |
40 actual_path = actual_path_template % i; | |
41 if not os.path.exists(expected_path): | |
42 if i == 0: | |
43 print "WARNING: no expected results files found for " + input_filename | |
44 break | |
45 print "Checking " + actual_path | |
46 sys.stdout.flush() | |
47 subprocess.check_call([pdfium_diff_path, expected_path, actual_path]) | |
48 i += 1 | |
49 except subprocess.CalledProcessError as e: | 31 except subprocess.CalledProcessError as e: |
50 print "FAILURE: " + input_filename + "; " + str(e) | 32 print "FAILURE: " + input_filename + "; " + str(e) |
51 return False | 33 return False |
| 34 if image_differ.HasDifferences(input_filename, source_dir, working_dir): |
| 35 return False |
52 return True | 36 return True |
53 | 37 |
54 def main(): | 38 def main(): |
55 if sys.platform.startswith('linux'): | |
56 os_name = 'linux' | |
57 elif sys.platform.startswith('win'): | |
58 os_name = 'win' | |
59 elif sys.platform.startswith('darwin'): | |
60 os_name = 'mac' | |
61 else: | |
62 print 'Confused, can not determine OS, aborting.' | |
63 return 1 | |
64 | |
65 parser = optparse.OptionParser() | 39 parser = optparse.OptionParser() |
66 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 40 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
67 help='relative path from the base source directory') | 41 help='relative path from the base source directory') |
68 options, args = parser.parse_args() | 42 options, args = parser.parse_args() |
69 | 43 finder = common.DirectoryFinder(options.build_dir) |
70 # Expect |my_dir| to be .../pdfium/testing/tools. | 44 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
71 my_dir = os.path.dirname(os.path.realpath(__file__)) | 45 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
72 testing_dir = os.path.dirname(my_dir) | |
73 pdfium_dir = os.path.dirname(testing_dir) | |
74 if (os.path.basename(my_dir) != 'tools' or | |
75 os.path.basename(testing_dir) != 'testing'): | |
76 print 'Confused, can not find pdfium root directory, aborting.' | |
77 return 1 | |
78 | |
79 # Find path to build directory. This depends on whether this is a | |
80 # standalone build vs. a build as part of a chromium checkout. For | |
81 # standalone, we expect a path like .../pdfium/out/Debug, but for | |
82 # chromium, we expect a path like .../src/out/Debug two levels | |
83 # higher (to skip over the third_party/pdfium path component under | |
84 # which chromium sticks pdfium). | |
85 base_dir = pdfium_dir | |
86 one_up_dir = os.path.dirname(base_dir) | |
87 two_up_dir = os.path.dirname(one_up_dir) | |
88 if (os.path.basename(two_up_dir) == 'src' and | |
89 os.path.basename(one_up_dir) == 'third_party'): | |
90 base_dir = two_up_dir | |
91 build_dir = os.path.join(base_dir, options.build_dir) | |
92 | |
93 # Compiled binaries are found under the build path. | |
94 pdfium_test_path = os.path.join(build_dir, 'pdfium_test') | |
95 pdfium_diff_path = os.path.join(build_dir, 'pdfium_diff') | |
96 if os_name == 'win': | |
97 pdfium_test_path = pdfium_test_path + '.exe' | |
98 pdfium_diff_path = pdfium_diff_path + '.exe' | |
99 # TODO(tsepez): Mac may require special handling here. | |
100 | |
101 # Place generated files under the build directory, not source directory. | |
102 working_dir = os.path.join(build_dir, 'gen', 'pdfium', 'testing', 'corpus') | |
103 if not os.path.exists(working_dir): | 46 if not os.path.exists(working_dir): |
104 os.makedirs(working_dir) | 47 os.makedirs(working_dir) |
105 | 48 |
106 suppression_list = extract_suppressions( | 49 test_suppressor = suppressor.Suppressor(finder) |
107 os.path.join(testing_dir, 'SUPPRESSIONS')) | 50 image_differ = pngdiffer.PNGDiffer(finder) |
108 | |
109 platform_suppression_filename = 'SUPPRESSIONS_%s' % os_name | |
110 platform_suppression_list = extract_suppressions( | |
111 os.path.join(testing_dir, platform_suppression_filename)) | |
112 | 51 |
113 # test files are under .../pdfium/testing/corpus. | 52 # test files are under .../pdfium/testing/corpus. |
114 failures = [] | 53 failures = [] |
115 walk_from_dir = os.path.join(testing_dir, 'corpus'); | 54 walk_from_dir = finder.TestingDir('corpus'); |
116 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') | 55 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') |
117 for source_dir, _, filename_list in os.walk(walk_from_dir): | 56 for source_dir, _, filename_list in os.walk(walk_from_dir): |
118 for input_filename in filename_list: | 57 for input_filename in filename_list: |
119 if input_file_re.match(input_filename): | 58 if input_file_re.match(input_filename): |
120 input_path = os.path.join(source_dir, input_filename) | 59 input_path = os.path.join(source_dir, input_filename) |
121 if os.path.isfile(input_path): | 60 if os.path.isfile(input_path): |
122 if input_filename in suppression_list: | 61 if test_suppressor.IsSuppressed(input_filename): |
123 print "Not running %s, found in SUPPRESSIONS file" % input_filename | |
124 continue | 62 continue |
125 if input_filename in platform_suppression_list: | |
126 print ("Not running %s, found in %s file" % | |
127 (input_filename, platform_suppression_filename)) | |
128 continue | |
129 | |
130 if not test_one_file(input_filename, source_dir, working_dir, | 63 if not test_one_file(input_filename, source_dir, working_dir, |
131 pdfium_test_path, pdfium_diff_path): | 64 pdfium_test_path, image_differ): |
132 failures.append(input_path) | 65 failures.append(input_path) |
133 | 66 |
134 if failures: | 67 if failures: |
135 print '\n\nSummary of Failures:' | 68 print '\n\nSummary of Failures:' |
136 for failure in failures: | 69 for failure in failures: |
137 print failure | 70 print failure |
138 return 1 | 71 return 1 |
139 | 72 |
140 return 0 | 73 return 0 |
141 | 74 |
142 | 75 |
143 if __name__ == '__main__': | 76 if __name__ == '__main__': |
144 sys.exit(main()) | 77 sys.exit(main()) |
OLD | NEW |