OLD | NEW |
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 cStringIO |
| 7 import functools |
| 8 import multiprocessing |
| 9 import optparse |
| 10 import os |
| 11 import re |
| 12 import shutil |
| 13 import subprocess |
6 import sys | 14 import sys |
7 | 15 |
8 import test_runner | 16 import common |
| 17 import pngdiffer |
| 18 import suppressor |
| 19 |
| 20 class KeyboardInterruptError(Exception): pass |
| 21 |
| 22 # Nomenclature: |
| 23 # x_root - "x" |
| 24 # x_filename - "x.ext" |
| 25 # x_path - "path/to/a/b/c/x.ext" |
| 26 # c_dir - "path/to/a/b/c" |
| 27 |
| 28 def test_one_file(input_filename, source_dir, working_dir, |
| 29 pdfium_test_path, image_differ, drmem_wrapper, |
| 30 redirect_output=False): |
| 31 input_path = os.path.join(source_dir, input_filename) |
| 32 pdf_path = os.path.join(working_dir, input_filename) |
| 33 # Remove any existing generated images from previous runs. |
| 34 actual_images = image_differ.GetActualFiles( |
| 35 input_filename, source_dir, working_dir) |
| 36 for image in actual_images: |
| 37 if os.path.exists(image): |
| 38 os.remove(image) |
| 39 |
| 40 shutil.copyfile(input_path, pdf_path) |
| 41 sys.stdout.flush() |
| 42 # add Dr. Memory wrapper if exist |
| 43 # remove .pdf suffix |
| 44 cmd_to_run = common.DrMemoryWrapper(drmem_wrapper, |
| 45 os.path.splitext(input_filename)[0]) |
| 46 cmd_to_run.extend([pdfium_test_path, '--png', pdf_path]) |
| 47 # run test |
| 48 error = common.RunCommand(cmd_to_run, redirect_output) |
| 49 if error: |
| 50 print "FAILURE: " + input_filename + "; " + str(error) |
| 51 return False |
| 52 return not image_differ.HasDifferences(input_filename, source_dir, |
| 53 working_dir, redirect_output) |
| 54 |
| 55 |
| 56 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, |
| 57 test_case): |
| 58 """Wrapper function to call test_one_file() and redirect output to stdout.""" |
| 59 try: |
| 60 old_stdout = sys.stdout |
| 61 old_stderr = sys.stderr |
| 62 sys.stdout = cStringIO.StringIO() |
| 63 sys.stderr = sys.stdout |
| 64 input_filename, source_dir = test_case |
| 65 result = test_one_file(input_filename, source_dir, working_dir, |
| 66 pdfium_test_path, image_differ, "", True); |
| 67 output = sys.stdout |
| 68 sys.stdout = old_stdout |
| 69 sys.stderr = old_stderr |
| 70 return (result, output.getvalue(), input_filename, source_dir) |
| 71 except KeyboardInterrupt: |
| 72 raise KeyboardInterruptError() |
| 73 |
| 74 |
| 75 def handle_result(test_suppressor, input_filename, input_path, result, |
| 76 surprises, failures): |
| 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 |
9 | 84 |
10 def main(): | 85 def main(): |
11 runner = test_runner.TestRunner('corpus') | 86 parser = optparse.OptionParser() |
12 return runner.Run() | 87 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 88 help='relative path from the base source directory') |
| 89 parser.add_option('-j', default=multiprocessing.cpu_count(), |
| 90 dest='num_workers', type='int', |
| 91 help='run NUM_WORKERS jobs in parallel') |
| 92 parser.add_option('--wrapper', default='', dest="wrapper", |
| 93 help='Dr. Memory wrapper for running test under Dr. Memory') |
| 94 options, args = parser.parse_args() |
| 95 finder = common.DirectoryFinder(options.build_dir) |
| 96 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 97 if not os.path.exists(pdfium_test_path): |
| 98 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path |
| 99 print "Use --build-dir to specify its location." |
| 100 return 1 |
| 101 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
| 102 if not os.path.exists(working_dir): |
| 103 os.makedirs(working_dir) |
| 104 |
| 105 feature_string = subprocess.check_output([pdfium_test_path, '--show-config']) |
| 106 test_suppressor = suppressor.Suppressor(finder, feature_string) |
| 107 image_differ = pngdiffer.PNGDiffer(finder) |
| 108 |
| 109 # test files are under .../pdfium/testing/corpus. |
| 110 failures = [] |
| 111 surprises = [] |
| 112 walk_from_dir = finder.TestingDir('corpus'); |
| 113 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]pdf$') |
| 114 test_cases = [] |
| 115 |
| 116 if len(args): |
| 117 for file_name in args: |
| 118 input_path = os.path.join(walk_from_dir, file_name) |
| 119 if not os.path.isfile(input_path): |
| 120 print "Can't find test file '%s'" % file_name |
| 121 return 1 |
| 122 |
| 123 test_cases.append((os.path.basename(input_path), |
| 124 os.path.dirname(input_path))) |
| 125 else: |
| 126 for source_dir, _, filename_list in os.walk(walk_from_dir): |
| 127 for input_filename in filename_list: |
| 128 if input_file_re.match(input_filename): |
| 129 input_path = os.path.join(source_dir, input_filename) |
| 130 if os.path.isfile(input_path): |
| 131 test_cases.append((input_filename, source_dir)) |
| 132 |
| 133 if options.num_workers > 1 and len(test_cases) > 1: |
| 134 try: |
| 135 pool = multiprocessing.Pool(options.num_workers) |
| 136 worker_func = functools.partial(test_one_file_parallel, working_dir, |
| 137 pdfium_test_path, image_differ) |
| 138 worker_results = pool.imap(worker_func, test_cases) |
| 139 for worker_result in worker_results: |
| 140 result, output, input_filename, source_dir = worker_result |
| 141 input_path = os.path.join(source_dir, input_filename) |
| 142 sys.stdout.write(output) |
| 143 handle_result(test_suppressor, input_filename, input_path, result, |
| 144 surprises, failures) |
| 145 pool.close() |
| 146 except KeyboardInterrupt: |
| 147 pool.terminate() |
| 148 finally: |
| 149 pool.join() |
| 150 else: |
| 151 for test_case in test_cases: |
| 152 input_filename, source_dir = test_case |
| 153 result = test_one_file(input_filename, source_dir, working_dir, |
| 154 pdfium_test_path, image_differ, |
| 155 options.wrapper) |
| 156 handle_result(test_suppressor, input_filename, input_path, result, |
| 157 surprises, failures) |
| 158 |
| 159 if surprises: |
| 160 surprises.sort() |
| 161 print '\n\nUnexpected Successes:' |
| 162 for surprise in surprises: |
| 163 print surprise; |
| 164 |
| 165 if failures: |
| 166 failures.sort() |
| 167 print '\n\nSummary of Failures:' |
| 168 for failure in failures: |
| 169 print failure |
| 170 return 1 |
| 171 |
| 172 return 0 |
| 173 |
13 | 174 |
14 if __name__ == '__main__': | 175 if __name__ == '__main__': |
15 sys.exit(main()) | 176 sys.exit(main()) |
OLD | NEW |