dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2016 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 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 6 | import cStringIO |
| 7 | import functools |
| 8 | import multiprocessing |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 9 | import optparse |
| 10 | import os |
| 11 | import re |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 12 | import shutil |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | import common |
| 17 | import pngdiffer |
| 18 | import suppressor |
| 19 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 20 | class KeyboardInterruptError(Exception): pass |
| 21 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 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 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 28 | def TestOneFileParallel(this, test_case): |
| 29 | """Wrapper to call GenerateAndTest() and redirect output to stdout.""" |
| 30 | try: |
| 31 | input_filename, source_dir = test_case |
| 32 | result = this.GenerateAndTest(input_filename, source_dir); |
| 33 | return (result, input_filename, source_dir) |
| 34 | except KeyboardInterrupt: |
| 35 | raise KeyboardInterruptError() |
| 36 | |
| 37 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 38 | class TestRunner: |
| 39 | def __init__(self, dirname): |
| 40 | self.test_dir = dirname |
| 41 | |
| 42 | def GenerateAndTest(self, input_filename, source_dir): |
| 43 | input_root, _ = os.path.splitext(input_filename) |
| 44 | expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 45 | |
| 46 | pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 47 | |
| 48 | # Remove any existing generated images from previous runs. |
| 49 | actual_images = self.image_differ.GetActualFiles(input_filename, source_dir, |
| 50 | self.working_dir) |
| 51 | for image in actual_images: |
| 52 | if os.path.exists(image): |
| 53 | os.remove(image) |
| 54 | |
| 55 | sys.stdout.flush() |
| 56 | |
| 57 | raised_exception = self.Generate(source_dir, input_filename, input_root, |
| 58 | pdf_path) |
| 59 | |
| 60 | if raised_exception != None: |
| 61 | print "FAILURE: " + input_filename + "; " + str(raised_exception) |
| 62 | return False |
| 63 | |
| 64 | if os.path.exists(expected_txt_path): |
| 65 | raised_exception = self.TestText(input_root, expected_txt_path, pdf_path) |
| 66 | else: |
| 67 | raised_exception = self.TestPixel(input_root, pdf_path) |
| 68 | |
| 69 | if raised_exception != None: |
| 70 | print "FAILURE: " + input_filename + "; " + str(raised_exception) |
| 71 | return False |
| 72 | |
| 73 | if len(actual_images): |
| 74 | if self.image_differ.HasDifferences(input_filename, source_dir, |
| 75 | self.working_dir): |
| 76 | return False |
| 77 | |
| 78 | return True |
| 79 | |
| 80 | def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 81 | original_path = os.path.join(source_dir, input_filename) |
| 82 | input_path = os.path.join(source_dir, input_root + '.in') |
| 83 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 84 | input_event_path = os.path.join(source_dir, input_root + ".evt") |
| 85 | if os.path.exists(input_event_path): |
| 86 | output_event_path = os.path.splitext(pdf_path)[0] + ".evt" |
| 87 | shutil.copyfile(input_event_path, output_event_path) |
| 88 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 89 | if not os.path.exists(input_path): |
| 90 | if os.path.exists(original_path): |
| 91 | shutil.copyfile(original_path, pdf_path) |
| 92 | return None |
| 93 | |
| 94 | sys.stdout.flush() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 95 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 96 | return common.RunCommand( |
| 97 | [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 98 | input_path]) |
| 99 | |
| 100 | |
| 101 | def TestText(self, input_root, expected_txt_path, pdf_path): |
| 102 | txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 103 | |
| 104 | with open(txt_path, 'w') as outfile: |
| 105 | # add Dr. Memory wrapper if exist |
| 106 | cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
| 107 | cmd_to_run.extend([self.pdfium_test_path, pdf_path]) |
| 108 | subprocess.check_call(cmd_to_run, stdout=outfile) |
| 109 | |
| 110 | cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path] |
| 111 | return common.RunCommand(cmd) |
| 112 | |
| 113 | |
| 114 | def TestPixel(self, input_root, pdf_path): |
| 115 | cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
tsepez | 10b01bf | 2016-05-04 12:52:42 -0700 | [diff] [blame] | 116 | cmd_to_run.extend([self.pdfium_test_path, '--send-events', '--png', |
| 117 | pdf_path]) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 118 | return common.RunCommand(cmd_to_run) |
| 119 | |
| 120 | |
| 121 | def HandleResult(self, input_filename, input_path, result): |
| 122 | if self.test_suppressor.IsResultSuppressed(input_filename): |
| 123 | if result: |
| 124 | self.surprises.append(input_path) |
| 125 | else: |
| 126 | if not result: |
| 127 | self.failures.append(input_path) |
| 128 | |
| 129 | |
| 130 | def Run(self): |
| 131 | parser = optparse.OptionParser() |
| 132 | parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 133 | help='relative path from the base source directory') |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 134 | parser.add_option('-j', default=multiprocessing.cpu_count(), |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 135 | dest='num_workers', type='int', |
| 136 | help='run NUM_WORKERS jobs in parallel') |
| 137 | parser.add_option('--wrapper', default='', dest="wrapper", |
| 138 | help='wrapper for running test under Dr. Memory') |
| 139 | options, args = parser.parse_args() |
| 140 | |
| 141 | finder = common.DirectoryFinder(options.build_dir) |
| 142 | self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 143 | self.text_diff_path = finder.ScriptPath('text_diff.py') |
| 144 | |
| 145 | self.drmem_wrapper = options.wrapper |
| 146 | |
| 147 | self.source_dir = finder.TestingDir() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 148 | if self.test_dir != 'corpus': |
| 149 | test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 150 | else: |
| 151 | test_dir = finder.TestingDir(self.test_dir) |
| 152 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 153 | self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 154 | if not os.path.exists(self.pdfium_test_path): |
| 155 | print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
| 156 | print "Use --build-dir to specify its location." |
| 157 | return 1 |
| 158 | |
| 159 | self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
| 160 | if not os.path.exists(self.working_dir): |
| 161 | os.makedirs(self.working_dir) |
| 162 | |
| 163 | self.feature_string = subprocess.check_output([self.pdfium_test_path, |
| 164 | '--show-config']) |
| 165 | self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 166 | self.image_differ = pngdiffer.PNGDiffer(finder) |
| 167 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 168 | walk_from_dir = finder.TestingDir(test_dir); |
| 169 | |
| 170 | test_cases = [] |
| 171 | input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$') |
| 172 | if len(args): |
| 173 | for file_name in args: |
| 174 | file_name.replace(".pdf", ".in") |
| 175 | input_path = os.path.join(walk_from_dir, file_name) |
| 176 | if not os.path.isfile(input_path): |
| 177 | print "Can't find test file '%s'" % file_name |
| 178 | return 1 |
| 179 | |
| 180 | test_cases.append((os.path.basename(input_path), |
| 181 | os.path.dirname(input_path))) |
| 182 | else: |
| 183 | for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 184 | for input_filename in filename_list: |
| 185 | if input_file_re.match(input_filename): |
| 186 | input_path = os.path.join(file_dir, input_filename) |
| 187 | if not self.test_suppressor.IsExecutionSuppressed(input_path): |
| 188 | if os.path.isfile(input_path): |
| 189 | test_cases.append((input_filename, file_dir)) |
| 190 | |
| 191 | self.failures = [] |
| 192 | self.surprises = [] |
| 193 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 194 | if options.num_workers > 1 and len(test_cases) > 1: |
| 195 | try: |
| 196 | pool = multiprocessing.Pool(options.num_workers) |
| 197 | worker_func = functools.partial(TestOneFileParallel, self) |
| 198 | |
| 199 | worker_results = pool.imap(worker_func, test_cases) |
| 200 | for worker_result in worker_results: |
| 201 | result, input_filename, source_dir = worker_result |
| 202 | input_path = os.path.join(source_dir, input_filename) |
| 203 | |
| 204 | self.HandleResult(input_filename, input_path, result) |
| 205 | |
| 206 | except KeyboardInterrupt: |
| 207 | pool.terminate() |
| 208 | finally: |
| 209 | pool.close() |
| 210 | pool.join() |
| 211 | else: |
| 212 | for test_case in test_cases: |
| 213 | input_filename, input_file_dir = test_case |
| 214 | result = self.GenerateAndTest(input_filename, input_file_dir) |
| 215 | self.HandleResult(input_filename, |
| 216 | os.path.join(input_file_dir, input_filename), result) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 217 | |
| 218 | if self.surprises: |
| 219 | self.surprises.sort() |
| 220 | print '\n\nUnexpected Successes:' |
| 221 | for surprise in self.surprises: |
| 222 | print surprise; |
| 223 | |
| 224 | if self.failures: |
| 225 | self.failures.sort() |
| 226 | print '\n\nSummary of Failures:' |
| 227 | for failure in self.failures: |
| 228 | print failure |
| 229 | return 1 |
| 230 | |
| 231 | return 0 |