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