Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: testing/tools/run_corpus_tests.py

Issue 1057983003: Refactor PDFium python test utilities. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Tidy. Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 common
Lei Zhang 2015/04/03 19:50:27 Group local imports separately: sys1 sys2 local1
Tom Sepez 2015/04/03 20:19:50 Done.
6 import optparse 7 import optparse
7 import os 8 import os
9 import pngdiffer
8 import re 10 import re
11 import shutil
9 import subprocess 12 import subprocess
10 import shutil 13 import suppressor
11 import sys 14 import sys
12 15
13 # Nomenclature: 16 # Nomenclature:
14 # x_root - "x" 17 # x_root - "x"
15 # x_filename - "x.ext" 18 # x_filename - "x.ext"
16 # x_path - "path/to/a/b/c/x.ext" 19 # x_path - "path/to/a/b/c/x.ext"
17 # c_dir - "path/to/a/b/c" 20 # c_dir - "path/to/a/b/c"
18 21
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, 22 def test_one_file(input_filename, source_dir, working_dir,
26 pdfium_test_path, pdfium_diff_path): 23 pdfium_test_path, image_differ):
27 input_root, _ = os.path.splitext(input_filename)
28 input_path = os.path.join(source_dir, input_filename) 24 input_path = os.path.join(source_dir, input_filename)
29 pdf_path = os.path.join(working_dir, input_filename) 25 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: 26 try:
34 shutil.copyfile(input_path, pdf_path) 27 shutil.copyfile(input_path, pdf_path)
35 sys.stdout.flush() 28 sys.stdout.flush()
36 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) 29 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: 30 except subprocess.CalledProcessError as e:
50 print "FAILURE: " + input_filename + "; " + str(e) 31 print "FAILURE: " + input_filename + "; " + str(e)
51 return False 32 return False
33 if image_differ.HasDifferences(input_filename, source_dir, working_dir):
34 return False
52 return True 35 return True
53 36
54 def main(): 37 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() 38 parser = optparse.OptionParser()
66 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), 39 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
67 help='relative path from the base source directory') 40 help='relative path from the base source directory')
68 options, args = parser.parse_args() 41 options, args = parser.parse_args()
69 42 finder = common.DirectoryFinder(options.build_dir)
70 # Expect |my_dir| to be .../pdfium/testing/tools. 43 pdfium_test_path = finder.ExecutablePath('pdfium_test')
71 my_dir = os.path.dirname(os.path.realpath(__file__)) 44 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): 45 if not os.path.exists(working_dir):
104 os.makedirs(working_dir) 46 os.makedirs(working_dir)
105 47
106 suppression_list = extract_suppressions( 48 test_suppressor = suppressor.Suppressor(finder)
107 os.path.join(testing_dir, 'SUPPRESSIONS')) 49 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 50
113 # test files are under .../pdfium/testing/corpus. 51 # test files are under .../pdfium/testing/corpus.
114 failures = [] 52 failures = []
115 walk_from_dir = os.path.join(testing_dir, 'corpus'); 53 walk_from_dir = finder.TestingDir('corpus');
116 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') 54 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$')
117 for source_dir, _, filename_list in os.walk(walk_from_dir): 55 for source_dir, _, filename_list in os.walk(walk_from_dir):
118 for input_filename in filename_list: 56 for input_filename in filename_list:
119 if input_file_re.match(input_filename): 57 if input_file_re.match(input_filename):
120 input_path = os.path.join(source_dir, input_filename) 58 input_path = os.path.join(source_dir, input_filename)
121 if os.path.isfile(input_path): 59 if os.path.isfile(input_path):
122 if input_filename in suppression_list: 60 if test_suppressor.IsSuppressed(input_filename):
123 print "Not running %s, found in SUPPRESSIONS file" % input_filename
124 continue 61 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, 62 if not test_one_file(input_filename, source_dir, working_dir,
131 pdfium_test_path, pdfium_diff_path): 63 pdfium_test_path, image_differ):
132 failures.append(input_path) 64 failures.append(input_path)
133 65
134 if failures: 66 if failures:
135 print '\n\nSummary of Failures:' 67 print '\n\nSummary of Failures:'
136 for failure in failures: 68 for failure in failures:
137 print failure 69 print failure
138 return 1 70 return 1
139 71
140 return 0 72 return 0
141 73
142 74
143 if __name__ == '__main__': 75 if __name__ == '__main__':
144 sys.exit(main()) 76 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698