Add Mac-specific pixel test expectations.
[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 optparse
7 import os
8 import re
9 import shutil
10 import subprocess
11 import sys
12
13 import common
14 import pngdiffer
15 import suppressor
16
17 # Nomenclature:
18 #   x_root - "x"
19 #   x_filename - "x.ext"
20 #   x_path - "path/to/a/b/c/x.ext"
21 #   c_dir - "path/to/a/b/c"
22
23 def test_one_file(input_filename, source_dir, working_dir,
24                   pdfium_test_path, image_differ):
25   input_path = os.path.join(source_dir, input_filename)
26   pdf_path = os.path.join(working_dir, input_filename)
27
28   # Remove any existing generated images from previous runs.
29   actual_images = image_differ.GetActualFiles(
30       input_filename, source_dir, working_dir)
31   for image in actual_images:
32     if os.path.exists(image):
33       os.remove(image)
34
35   try:
36     shutil.copyfile(input_path, pdf_path)
37     sys.stdout.flush()
38     subprocess.check_call([pdfium_test_path, '--png', pdf_path])
39   except subprocess.CalledProcessError as e:
40     print "FAILURE: " + input_filename + "; " + str(e)
41     return False
42   if image_differ.HasDifferences(input_filename, source_dir, working_dir):
43     return False
44   return True
45
46
47 def main():
48   parser = optparse.OptionParser()
49   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
50                     help='relative path from the base source directory')
51   options, args = parser.parse_args()
52   finder = common.DirectoryFinder(options.build_dir)
53   pdfium_test_path = finder.ExecutablePath('pdfium_test')
54   if not os.path.exists(pdfium_test_path):
55     print "FAILURE: Can't find test executable '%s'" % pdfium_test_path
56     print "Use --build-dir to specify its location."
57     return 1
58   working_dir = finder.WorkingDir(os.path.join('testing', 'corpus'))
59   if not os.path.exists(working_dir):
60     os.makedirs(working_dir)
61
62   test_suppressor = suppressor.Suppressor(finder)
63   image_differ = pngdiffer.PNGDiffer(finder)
64
65   # test files are under .../pdfium/testing/corpus.
66   failures = []
67   surprises = []
68   walk_from_dir = finder.TestingDir('corpus');
69   input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$')
70   for source_dir, _, filename_list in os.walk(walk_from_dir):
71     for input_filename in filename_list:
72       if input_file_re.match(input_filename):
73          input_path = os.path.join(source_dir, input_filename)
74          if os.path.isfile(input_path):
75            result = test_one_file(input_filename, source_dir, working_dir,
76                                   pdfium_test_path, image_differ)
77            if test_suppressor.IsSuppressed(input_filename):
78              if result:
79                surprises.append(input_path)
80            else:
81              if not result:
82                failures.append(input_path)
83
84   if surprises:
85     surprises.sort()
86     print '\n\nUnexpected Successes:'
87     for surprise in surprises:
88       print surprise;
89
90   if failures:
91     failures.sort()
92     print '\n\nSummary of Failures:'
93     for failure in failures:
94       print failure
95     return 1
96
97   return 0
98
99
100 if __name__ == '__main__':
101   sys.exit(main())