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