Allow external font-path configuration from pdfium_test.
[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, font_dir, image_differ):
25   input_path = os.path.join(source_dir, input_filename)
26   pdf_path = os.path.join(working_dir, input_filename)
27   try:
28     shutil.copyfile(input_path, pdf_path)
29     sys.stdout.flush()
30     subprocess.check_call([pdfium_test_path, '--font-dir=' + font_dir,
31                            '--png', pdf_path])
32   except subprocess.CalledProcessError as e:
33     print "FAILURE: " + input_filename + "; " + str(e)
34     return False
35   if image_differ.HasDifferences(input_filename, source_dir, working_dir):
36     return False
37   return True
38
39 def main():
40   parser = optparse.OptionParser()
41   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
42                     help='relative path from the base source directory')
43   options, args = parser.parse_args()
44   finder = common.DirectoryFinder(options.build_dir)
45   pdfium_test_path = finder.ExecutablePath('pdfium_test')
46   if not os.path.exists(pdfium_test_path):
47     print "FAILURE: Can't find test executable '%s'" % pdfium_test_path
48     print "Use --build-dir to specify its location."
49     return 1
50   working_dir = finder.WorkingDir(os.path.join('testing', 'corpus'))
51   if not os.path.exists(working_dir):
52     os.makedirs(working_dir)
53
54   test_suppressor = suppressor.Suppressor(finder)
55   image_differ = pngdiffer.PNGDiffer(finder)
56
57   # test files are under .../pdfium/testing/corpus.
58   failures = []
59   surprises = []
60   walk_from_dir = finder.TestingDir('corpus');
61   font_dir = os.path.join(walk_from_dir, 'fonts');
62   input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$')
63   for source_dir, _, filename_list in os.walk(walk_from_dir):
64     for input_filename in filename_list:
65       if input_file_re.match(input_filename):
66          input_path = os.path.join(source_dir, input_filename)
67          if os.path.isfile(input_path):
68            result = test_one_file(input_filename, source_dir, working_dir,
69                                   pdfium_test_path, font_dir, image_differ)
70            if test_suppressor.IsSuppressed(input_filename):
71              if result:
72                surprises.append(input_path)
73            else:
74              if not result:
75                failures.append(input_path)
76
77   if surprises:
78     surprises.sort()
79     print '\n\nUnexpected Successes:'
80     for surprise in surprises:
81       print surprise;
82
83   if failures:
84     failures.sort()
85     print '\n\nSummary of Failures:'
86     for failure in failures:
87       print failure
88     return 1
89
90   return 0
91
92
93 if __name__ == '__main__':
94   sys.exit(main())