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 |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 17 | import gold |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 18 | import pngdiffer |
| 19 | import suppressor |
| 20 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 21 | class KeyboardInterruptError(Exception): pass |
| 22 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 23 | # Nomenclature: |
| 24 | # x_root - "x" |
| 25 | # x_filename - "x.ext" |
| 26 | # x_path - "path/to/a/b/c/x.ext" |
| 27 | # c_dir - "path/to/a/b/c" |
| 28 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 29 | def TestOneFileParallel(this, test_case): |
| 30 | """Wrapper to call GenerateAndTest() and redirect output to stdout.""" |
| 31 | try: |
| 32 | input_filename, source_dir = test_case |
| 33 | result = this.GenerateAndTest(input_filename, source_dir); |
| 34 | return (result, input_filename, source_dir) |
| 35 | except KeyboardInterrupt: |
| 36 | raise KeyboardInterruptError() |
| 37 | |
| 38 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 39 | class TestRunner: |
| 40 | def __init__(self, dirname): |
| 41 | self.test_dir = dirname |
| 42 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 43 | # GenerateAndTest returns a tuple <success, outputfiles> where |
| 44 | # success is a boolean indicating whether the tests passed comparison |
| 45 | # tests and outputfiles is a list tuples: |
| 46 | # (path_to_image, md5_hash_of_pixelbuffer) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 47 | def GenerateAndTest(self, input_filename, source_dir): |
| 48 | input_root, _ = os.path.splitext(input_filename) |
| 49 | expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 50 | |
| 51 | pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 52 | |
| 53 | # Remove any existing generated images from previous runs. |
| 54 | actual_images = self.image_differ.GetActualFiles(input_filename, source_dir, |
| 55 | self.working_dir) |
| 56 | for image in actual_images: |
| 57 | if os.path.exists(image): |
| 58 | os.remove(image) |
| 59 | |
| 60 | sys.stdout.flush() |
| 61 | |
| 62 | raised_exception = self.Generate(source_dir, input_filename, input_root, |
| 63 | pdf_path) |
| 64 | |
| 65 | if raised_exception != None: |
| 66 | print "FAILURE: " + input_filename + "; " + str(raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 67 | return False, [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 68 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 69 | results = [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 70 | if os.path.exists(expected_txt_path): |
| 71 | raised_exception = self.TestText(input_root, expected_txt_path, pdf_path) |
| 72 | else: |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 73 | raised_exception, results = self.TestPixel(input_root, pdf_path) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 74 | |
| 75 | if raised_exception != None: |
| 76 | print "FAILURE: " + input_filename + "; " + str(raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 77 | return False, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 78 | |
| 79 | if len(actual_images): |
| 80 | if self.image_differ.HasDifferences(input_filename, source_dir, |
| 81 | self.working_dir): |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 82 | return False, results |
| 83 | return True, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 84 | |
| 85 | def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 86 | original_path = os.path.join(source_dir, input_filename) |
| 87 | input_path = os.path.join(source_dir, input_root + '.in') |
| 88 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 89 | input_event_path = os.path.join(source_dir, input_root + ".evt") |
| 90 | if os.path.exists(input_event_path): |
| 91 | output_event_path = os.path.splitext(pdf_path)[0] + ".evt" |
| 92 | shutil.copyfile(input_event_path, output_event_path) |
| 93 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 94 | if not os.path.exists(input_path): |
| 95 | if os.path.exists(original_path): |
| 96 | shutil.copyfile(original_path, pdf_path) |
| 97 | return None |
| 98 | |
| 99 | sys.stdout.flush() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 100 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 101 | return common.RunCommand( |
| 102 | [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 103 | input_path]) |
| 104 | |
| 105 | |
| 106 | def TestText(self, input_root, expected_txt_path, pdf_path): |
| 107 | txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 108 | |
| 109 | with open(txt_path, 'w') as outfile: |
| 110 | # add Dr. Memory wrapper if exist |
| 111 | cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
| 112 | cmd_to_run.extend([self.pdfium_test_path, pdf_path]) |
| 113 | subprocess.check_call(cmd_to_run, stdout=outfile) |
| 114 | |
| 115 | cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path] |
| 116 | return common.RunCommand(cmd) |
| 117 | |
| 118 | |
| 119 | def TestPixel(self, input_root, pdf_path): |
| 120 | cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 121 | cmd_to_run.extend([self.pdfium_test_path, '--send-events', '--png']) |
| 122 | if self.gold_results: |
| 123 | cmd_to_run.append('--md5') |
| 124 | cmd_to_run.append(pdf_path) |
| 125 | return common.RunCommandExtractHashedFiles(cmd_to_run) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 126 | |
| 127 | def HandleResult(self, input_filename, input_path, result): |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 128 | if self.gold_results: |
| 129 | success, image_paths = result |
stephana | 38c2705 | 2017-01-13 13:16:40 -0800 | [diff] [blame] | 130 | if image_paths: |
| 131 | for img_path, md5_hash in image_paths: |
| 132 | # the output filename (without extension becomes the test name) |
| 133 | test_name = os.path.splitext(os.path.split(img_path)[1])[0] |
| 134 | self.gold_results.AddTestResult(test_name, md5_hash, img_path) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 135 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 136 | if self.test_suppressor.IsResultSuppressed(input_filename): |
| 137 | if result: |
| 138 | self.surprises.append(input_path) |
| 139 | else: |
| 140 | if not result: |
| 141 | self.failures.append(input_path) |
| 142 | |
| 143 | |
| 144 | def Run(self): |
| 145 | parser = optparse.OptionParser() |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 146 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 147 | parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 148 | help='relative path from the base source directory') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 149 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 150 | parser.add_option('-j', default=multiprocessing.cpu_count(), |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 151 | dest='num_workers', type='int', |
| 152 | help='run NUM_WORKERS jobs in parallel') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 153 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 154 | parser.add_option('--wrapper', default='', dest="wrapper", |
| 155 | help='wrapper for running test under Dr. Memory') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 156 | |
| 157 | parser.add_option('--gold_properties', default='', dest="gold_properties", |
| 158 | help='Key value pairs that are written to the top level of the JSON file that is ingested by Gold.') |
| 159 | |
| 160 | parser.add_option('--gold_key', default='', dest="gold_key", |
| 161 | help='Key value pairs that are added to the "key" field of the JSON file that is ingested by Gold.') |
| 162 | |
| 163 | parser.add_option('--gold_output_dir', default='', dest="gold_output_dir", |
| 164 | help='Path of where to write the JSON output to be uploaded to Gold.') |
| 165 | |
| 166 | parser.add_option('--ignore_errors', action="store_true", dest="ignore_errors", |
| 167 | help='Prevents the return value from being non-zero when image comparison fails.') |
| 168 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 169 | options, args = parser.parse_args() |
| 170 | |
| 171 | finder = common.DirectoryFinder(options.build_dir) |
| 172 | self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 173 | self.text_diff_path = finder.ScriptPath('text_diff.py') |
| 174 | |
| 175 | self.drmem_wrapper = options.wrapper |
| 176 | |
| 177 | self.source_dir = finder.TestingDir() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 178 | if self.test_dir != 'corpus': |
| 179 | test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 180 | else: |
| 181 | test_dir = finder.TestingDir(self.test_dir) |
| 182 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 183 | self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 184 | if not os.path.exists(self.pdfium_test_path): |
| 185 | print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
| 186 | print "Use --build-dir to specify its location." |
| 187 | return 1 |
| 188 | |
| 189 | self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
| 190 | if not os.path.exists(self.working_dir): |
| 191 | os.makedirs(self.working_dir) |
| 192 | |
| 193 | self.feature_string = subprocess.check_output([self.pdfium_test_path, |
| 194 | '--show-config']) |
| 195 | self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 196 | self.image_differ = pngdiffer.PNGDiffer(finder) |
| 197 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 198 | walk_from_dir = finder.TestingDir(test_dir); |
| 199 | |
| 200 | test_cases = [] |
| 201 | input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$') |
| 202 | if len(args): |
| 203 | for file_name in args: |
| 204 | file_name.replace(".pdf", ".in") |
| 205 | input_path = os.path.join(walk_from_dir, file_name) |
| 206 | if not os.path.isfile(input_path): |
| 207 | print "Can't find test file '%s'" % file_name |
| 208 | return 1 |
| 209 | |
| 210 | test_cases.append((os.path.basename(input_path), |
| 211 | os.path.dirname(input_path))) |
| 212 | else: |
| 213 | for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 214 | for input_filename in filename_list: |
| 215 | if input_file_re.match(input_filename): |
| 216 | input_path = os.path.join(file_dir, input_filename) |
| 217 | if not self.test_suppressor.IsExecutionSuppressed(input_path): |
| 218 | if os.path.isfile(input_path): |
| 219 | test_cases.append((input_filename, file_dir)) |
| 220 | |
| 221 | self.failures = [] |
| 222 | self.surprises = [] |
| 223 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 224 | # Collect Gold results if an output directory was named. |
| 225 | self.gold_results = None |
| 226 | if options.gold_output_dir: |
| 227 | self.gold_results = gold.GoldResults("pdfium", |
| 228 | options.gold_output_dir, |
| 229 | options.gold_properties, |
| 230 | options.gold_key) |
| 231 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 232 | if options.num_workers > 1 and len(test_cases) > 1: |
| 233 | try: |
| 234 | pool = multiprocessing.Pool(options.num_workers) |
| 235 | worker_func = functools.partial(TestOneFileParallel, self) |
| 236 | |
| 237 | worker_results = pool.imap(worker_func, test_cases) |
| 238 | for worker_result in worker_results: |
| 239 | result, input_filename, source_dir = worker_result |
| 240 | input_path = os.path.join(source_dir, input_filename) |
| 241 | |
| 242 | self.HandleResult(input_filename, input_path, result) |
| 243 | |
| 244 | except KeyboardInterrupt: |
| 245 | pool.terminate() |
| 246 | finally: |
| 247 | pool.close() |
| 248 | pool.join() |
| 249 | else: |
| 250 | for test_case in test_cases: |
| 251 | input_filename, input_file_dir = test_case |
| 252 | result = self.GenerateAndTest(input_filename, input_file_dir) |
| 253 | self.HandleResult(input_filename, |
| 254 | os.path.join(input_file_dir, input_filename), result) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 255 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 256 | if self.gold_results: |
| 257 | self.gold_results.WriteResults() |
| 258 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 259 | if self.surprises: |
| 260 | self.surprises.sort() |
| 261 | print '\n\nUnexpected Successes:' |
| 262 | for surprise in self.surprises: |
| 263 | print surprise; |
| 264 | |
| 265 | if self.failures: |
| 266 | self.failures.sort() |
| 267 | print '\n\nSummary of Failures:' |
| 268 | for failure in self.failures: |
| 269 | print failure |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 270 | if not options.ignore_errors: |
| 271 | return 1 |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 272 | return 0 |