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

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: Line wrap. 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 optparse 6 import optparse
7 import os 7 import os
8 import re 8 import re
9 import subprocess 9 import subprocess
10 import shutil 10 import shutil
11 import sys 11 import sys
12 12
13 # Nomenclature: 13 # Nomenclature:
14 # x_root - "x" 14 # x_root - "x"
15 # x_filename - "x.ext" 15 # x_filename - "x.ext"
16 # x_path - "path/to/a/b/c/x.ext" 16 # x_path - "path/to/a/b/c/x.ext"
17 # c_dir - "path/to/a/b/c" 17 # c_dir - "path/to/a/b/c"
18 18
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, 19 def test_one_file(input_filename, source_dir, working_dir,
26 pdfium_test_path, pdfium_diff_path): 20 pdfium_test_path, image_differ):
27 input_root, _ = os.path.splitext(input_filename)
28 input_path = os.path.join(source_dir, input_filename) 21 input_path = os.path.join(source_dir, input_filename)
29 pdf_path = os.path.join(working_dir, input_filename) 22 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: 23 try:
34 shutil.copyfile(input_path, pdf_path) 24 shutil.copyfile(input_path, pdf_path)
35 sys.stdout.flush() 25 sys.stdout.flush()
36 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) 26 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: 27 except subprocess.CalledProcessError as e:
50 print "FAILURE: " + input_filename + "; " + str(e) 28 print "FAILURE: " + input_filename + "; " + str(e)
51 return False 29 return False
30 if image_differ.HasDifferences(input_filename, source_dir, working_dir):
31 return False
52 return True 32 return True
53 33
54 def main(): 34 def main():
55 if sys.platform.startswith('linux'): 35 if sys.platform.startswith('linux'):
56 os_name = 'linux' 36 os_name = 'linux'
57 elif sys.platform.startswith('win'): 37 elif sys.platform.startswith('win'):
58 os_name = 'win' 38 os_name = 'win'
59 elif sys.platform.startswith('darwin'): 39 elif sys.platform.startswith('darwin'):
60 os_name = 'mac' 40 os_name = 'mac'
61 else: 41 else:
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 if os_name == 'win': 76 if os_name == 'win':
97 pdfium_test_path = pdfium_test_path + '.exe' 77 pdfium_test_path = pdfium_test_path + '.exe'
98 pdfium_diff_path = pdfium_diff_path + '.exe' 78 pdfium_diff_path = pdfium_diff_path + '.exe'
99 # TODO(tsepez): Mac may require special handling here. 79 # TODO(tsepez): Mac may require special handling here.
100 80
101 # Place generated files under the build directory, not source directory. 81 # Place generated files under the build directory, not source directory.
102 working_dir = os.path.join(build_dir, 'gen', 'pdfium', 'testing', 'corpus') 82 working_dir = os.path.join(build_dir, 'gen', 'pdfium', 'testing', 'corpus')
103 if not os.path.exists(working_dir): 83 if not os.path.exists(working_dir):
104 os.makedirs(working_dir) 84 os.makedirs(working_dir)
105 85
106 suppression_list = extract_suppressions( 86 from suppressor import Suppressor
Lei Zhang 2015/04/02 23:16:20 from x import y, only where y is a module. https:
Tom Sepez 2015/04/03 18:13:15 Done.
107 os.path.join(testing_dir, 'SUPPRESSIONS')) 87 test_suppressor = Suppressor(os_name, testing_dir)
108 88
109 platform_suppression_filename = 'SUPPRESSIONS_%s' % os_name 89 from pngdiffer import PNGDiffer
110 platform_suppression_list = extract_suppressions( 90 image_differ = PNGDiffer(os_name, pdfium_diff_path)
111 os.path.join(testing_dir, platform_suppression_filename))
112 91
113 # test files are under .../pdfium/testing/corpus. 92 # test files are under .../pdfium/testing/corpus.
114 failures = [] 93 failures = []
115 walk_from_dir = os.path.join(testing_dir, 'corpus'); 94 walk_from_dir = os.path.join(testing_dir, 'corpus');
116 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') 95 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$')
117 for source_dir, _, filename_list in os.walk(walk_from_dir): 96 for source_dir, _, filename_list in os.walk(walk_from_dir):
118 for input_filename in filename_list: 97 for input_filename in filename_list:
119 if input_file_re.match(input_filename): 98 if input_file_re.match(input_filename):
120 input_path = os.path.join(source_dir, input_filename) 99 input_path = os.path.join(source_dir, input_filename)
121 if os.path.isfile(input_path): 100 if os.path.isfile(input_path):
122 if input_filename in suppression_list: 101 if test_suppressor.IsSuppressed(input_filename):
123 print "Not running %s, found in SUPPRESSIONS file" % input_filename
124 continue 102 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, 103 if not test_one_file(input_filename, source_dir, working_dir,
131 pdfium_test_path, pdfium_diff_path): 104 pdfium_test_path, image_differ):
132 failures.append(input_path) 105 failures.append(input_path)
133 106
134 if failures: 107 if failures:
135 print '\n\nSummary of Failures:' 108 print '\n\nSummary of Failures:'
136 for failure in failures: 109 for failure in failures:
137 print failure 110 print failure
138 return 1 111 return 1
139 112
140 return 0 113 return 0
141 114
142 115
143 if __name__ == '__main__': 116 if __name__ == '__main__':
144 sys.exit(main()) 117 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698