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