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 functools |
| 7 | import multiprocessing |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 8 | import optparse |
| 9 | import os |
| 10 | import re |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 11 | import shutil |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | import common |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 16 | import gold |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 17 | import pngdiffer |
| 18 | import suppressor |
| 19 | |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 20 | # Arbitrary timestamp, expressed in seconds since the epoch, used to make sure |
| 21 | # that tests that depend on the current time are stable. Happens to be the |
| 22 | # timestamp of the first commit to repo, 2014/5/9 17:48:50. |
| 23 | TEST_SEED_TIME = "1399672130" |
| 24 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 25 | class KeyboardInterruptError(Exception): pass |
| 26 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 27 | # Nomenclature: |
| 28 | # x_root - "x" |
| 29 | # x_filename - "x.ext" |
| 30 | # x_path - "path/to/a/b/c/x.ext" |
| 31 | # c_dir - "path/to/a/b/c" |
| 32 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 33 | def TestOneFileParallel(this, test_case): |
| 34 | """Wrapper to call GenerateAndTest() and redirect output to stdout.""" |
| 35 | try: |
| 36 | input_filename, source_dir = test_case |
| 37 | result = this.GenerateAndTest(input_filename, source_dir); |
| 38 | return (result, input_filename, source_dir) |
| 39 | except KeyboardInterrupt: |
| 40 | raise KeyboardInterruptError() |
| 41 | |
| 42 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 43 | def DeleteFiles(files): |
| 44 | """Utility function to delete a list of files""" |
| 45 | for f in files: |
| 46 | if os.path.exists(f): |
| 47 | os.remove(f) |
| 48 | |
| 49 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 50 | class TestRunner: |
| 51 | def __init__(self, dirname): |
Ryan Harrison | 80302c7 | 2018-05-10 18:27:25 +0000 | [diff] [blame] | 52 | # Currently the only used directories are corpus, javascript, and pixel, |
| 53 | # which all correspond directly to the type for the test being run. In the |
| 54 | # future if there are tests that don't have this clean correspondence, then |
| 55 | # an argument for the type will need to be added. |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 56 | self.test_dir = dirname |
Ryan Harrison | 80302c7 | 2018-05-10 18:27:25 +0000 | [diff] [blame] | 57 | self.test_type = dirname |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 58 | self.delete_output_on_success = False |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 59 | self.enforce_expected_images = False |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 60 | self.oneshot_renderer = False |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 61 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 62 | # GenerateAndTest returns a tuple <success, outputfiles> where |
| 63 | # success is a boolean indicating whether the tests passed comparison |
| 64 | # tests and outputfiles is a list tuples: |
| 65 | # (path_to_image, md5_hash_of_pixelbuffer) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 66 | def GenerateAndTest(self, input_filename, source_dir): |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 67 | use_ahem = 'use_ahem' in source_dir |
| 68 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 69 | input_root, _ = os.path.splitext(input_filename) |
| 70 | expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 71 | |
| 72 | pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 73 | |
| 74 | # Remove any existing generated images from previous runs. |
| 75 | actual_images = self.image_differ.GetActualFiles(input_filename, source_dir, |
| 76 | self.working_dir) |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 77 | DeleteFiles(actual_images) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 78 | |
| 79 | sys.stdout.flush() |
| 80 | |
| 81 | raised_exception = self.Generate(source_dir, input_filename, input_root, |
| 82 | pdf_path) |
| 83 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 84 | if raised_exception is not None: |
| 85 | print 'FAILURE: %s; %s' % (input_filename, raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 86 | return False, [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 87 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 88 | results = [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 89 | if os.path.exists(expected_txt_path): |
| 90 | raised_exception = self.TestText(input_root, expected_txt_path, pdf_path) |
| 91 | else: |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 92 | raised_exception, results = self.TestPixel(input_root, pdf_path, use_ahem) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 93 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 94 | if raised_exception is not None: |
| 95 | print 'FAILURE: %s; %s' % (input_filename, raised_exception) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 96 | return False, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 97 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 98 | if actual_images: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 99 | if self.image_differ.HasDifferences(input_filename, source_dir, |
| 100 | self.working_dir): |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 101 | self.RegenerateIfNeeded_(input_filename, source_dir) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 102 | return False, results |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 103 | else: |
| 104 | if (self.enforce_expected_images |
Henrique Nakashima | 6fac27d | 2017-06-27 15:52:45 -0400 | [diff] [blame] | 105 | and not self.test_suppressor.IsImageDiffSuppressed(input_filename)): |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 106 | self.RegenerateIfNeeded_(input_filename, source_dir) |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 107 | print 'FAILURE: %s; Missing expected images' % input_filename |
| 108 | return False, results |
| 109 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 110 | if self.delete_output_on_success: |
| 111 | DeleteFiles(actual_images) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 112 | return True, results |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 113 | |
Henrique Nakashima | 15bc974 | 2018-04-26 15:55:07 +0000 | [diff] [blame] | 114 | def RegenerateIfNeeded_(self, input_filename, source_dir): |
| 115 | if (not self.options.regenerate_expected |
| 116 | or self.test_suppressor.IsResultSuppressed(input_filename) |
| 117 | or self.test_suppressor.IsImageDiffSuppressed(input_filename)): |
| 118 | return |
| 119 | |
| 120 | platform_only = (self.options.regenerate_expected == 'platform') |
| 121 | self.image_differ.Regenerate(input_filename, source_dir, |
| 122 | self.working_dir, platform_only) |
| 123 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 124 | def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 125 | original_path = os.path.join(source_dir, input_filename) |
| 126 | input_path = os.path.join(source_dir, input_root + '.in') |
| 127 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 128 | input_event_path = os.path.join(source_dir, input_root + '.evt') |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 129 | if os.path.exists(input_event_path): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 130 | output_event_path = os.path.splitext(pdf_path)[0] + '.evt' |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 131 | shutil.copyfile(input_event_path, output_event_path) |
| 132 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 133 | if not os.path.exists(input_path): |
| 134 | if os.path.exists(original_path): |
| 135 | shutil.copyfile(original_path, pdf_path) |
| 136 | return None |
| 137 | |
| 138 | sys.stdout.flush() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 139 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 140 | return common.RunCommand( |
| 141 | [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 142 | input_path]) |
| 143 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 144 | def TestText(self, input_root, expected_txt_path, pdf_path): |
| 145 | txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 146 | |
| 147 | with open(txt_path, 'w') as outfile: |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 148 | cmd_to_run = [self.pdfium_test_path, '--send-events', |
| 149 | '--time=' + TEST_SEED_TIME, pdf_path] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 150 | subprocess.check_call(cmd_to_run, stdout=outfile) |
| 151 | |
| 152 | cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path] |
| 153 | return common.RunCommand(cmd) |
| 154 | |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 155 | def TestPixel(self, input_root, pdf_path, use_ahem): |
Ryan Harrison | 70cca36 | 2018-08-10 18:55:46 +0000 | [diff] [blame] | 156 | cmd_to_run = [self.pdfium_test_path, '--send-events', '--png', '--md5', |
| 157 | '--time=' + TEST_SEED_TIME] |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 158 | |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 159 | if self.oneshot_renderer: |
| 160 | cmd_to_run.append('--render-oneshot') |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 161 | |
| 162 | if use_ahem: |
| 163 | cmd_to_run.append('--font-dir=%s' % self.font_dir) |
| 164 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 165 | cmd_to_run.append(pdf_path) |
| 166 | return common.RunCommandExtractHashedFiles(cmd_to_run) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 167 | |
| 168 | def HandleResult(self, input_filename, input_path, result): |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 169 | success, image_paths = result |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 170 | |
| 171 | if image_paths: |
| 172 | for img_path, md5_hash in image_paths: |
| 173 | # The output filename without image extension becomes the test name. |
| 174 | # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png" |
| 175 | # becomes "example_005.pdf.0". |
| 176 | test_name = os.path.splitext(os.path.split(img_path)[1])[0] |
| 177 | |
Stephan Altmueller | bcd66f5 | 2018-06-21 14:45:44 +0000 | [diff] [blame] | 178 | matched = "suppressed" |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 179 | if not self.test_suppressor.IsResultSuppressed(input_filename): |
| 180 | matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash) |
| 181 | if matched == gold.GoldBaseline.MISMATCH: |
| 182 | print 'Skia Gold hash mismatch for test case: %s' % test_name |
| 183 | elif matched == gold.GoldBaseline.NO_BASELINE: |
| 184 | print 'No Skia Gold baseline found for test case: %s' % test_name |
| 185 | |
| 186 | if self.gold_results: |
Stephan Altmueller | bcd66f5 | 2018-06-21 14:45:44 +0000 | [diff] [blame] | 187 | self.gold_results.AddTestResult(test_name, md5_hash, img_path, matched) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 188 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 189 | if self.test_suppressor.IsResultSuppressed(input_filename): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 190 | self.result_suppressed_cases.append(input_filename) |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 191 | if success: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 192 | self.surprises.append(input_path) |
| 193 | else: |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 194 | if not success: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 195 | self.failures.append(input_path) |
| 196 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 197 | def Run(self): |
| 198 | parser = optparse.OptionParser() |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 199 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 200 | parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 201 | help='relative path from the base source directory') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 202 | |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 203 | parser.add_option('-j', default=multiprocessing.cpu_count(), |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 204 | dest='num_workers', type='int', |
| 205 | help='run NUM_WORKERS jobs in parallel') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 206 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 207 | parser.add_option('--gold_properties', default='', dest="gold_properties", |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 208 | help='Key value pairs that are written to the top level ' |
| 209 | 'of the JSON file that is ingested by Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 210 | |
| 211 | parser.add_option('--gold_key', default='', dest="gold_key", |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 212 | help='Key value pairs that are added to the "key" field ' |
| 213 | 'of the JSON file that is ingested by Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 214 | |
| 215 | parser.add_option('--gold_output_dir', default='', dest="gold_output_dir", |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 216 | help='Path of where to write the JSON output to be ' |
| 217 | 'uploaded to Gold.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 218 | |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 219 | parser.add_option('--gold_ignore_hashes', default='', |
| 220 | dest="gold_ignore_hashes", |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 221 | help='Path to a file with MD5 hashes we wish to ignore.') |
| 222 | |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 223 | parser.add_option('--regenerate_expected', default='', |
| 224 | dest="regenerate_expected", |
| 225 | help='Regenerates expected images. Valid values are ' |
| 226 | '"all" to regenerate all expected pngs, and ' |
| 227 | '"platform" to regenerate only platform-specific ' |
| 228 | 'expected pngs.') |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 229 | |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 230 | parser.add_option('--ignore_errors', action="store_true", |
| 231 | dest="ignore_errors", |
| 232 | help='Prevents the return value from being non-zero ' |
| 233 | 'when image comparison fails.') |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 234 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 235 | self.options, self.args = parser.parse_args() |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 236 | |
Henrique Nakashima | 352e251 | 2017-10-26 11:22:52 -0400 | [diff] [blame] | 237 | if (self.options.regenerate_expected |
| 238 | and self.options.regenerate_expected not in ['all', 'platform']) : |
| 239 | print 'FAILURE: --regenerate_expected must be "all" or "platform"' |
| 240 | return 1 |
| 241 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 242 | finder = common.DirectoryFinder(self.options.build_dir) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 243 | self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 244 | self.text_diff_path = finder.ScriptPath('text_diff.py') |
Ryan Harrison | 1118a66 | 2018-05-31 19:26:52 +0000 | [diff] [blame] | 245 | self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts') |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 246 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 247 | self.source_dir = finder.TestingDir() |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 248 | if self.test_dir != 'corpus': |
| 249 | test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 250 | else: |
| 251 | test_dir = finder.TestingDir(self.test_dir) |
| 252 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 253 | self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 254 | if not os.path.exists(self.pdfium_test_path): |
| 255 | print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 256 | print 'Use --build-dir to specify its location.' |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 257 | return 1 |
| 258 | |
| 259 | self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
| 260 | if not os.path.exists(self.working_dir): |
| 261 | os.makedirs(self.working_dir) |
| 262 | |
| 263 | self.feature_string = subprocess.check_output([self.pdfium_test_path, |
| 264 | '--show-config']) |
| 265 | self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 266 | self.image_differ = pngdiffer.PNGDiffer(finder) |
Henrique Nakashima | 40be505 | 2018-10-10 23:18:14 +0000 | [diff] [blame] | 267 | error_message = self.image_differ.CheckMissingTools( |
| 268 | self.options.regenerate_expected) |
| 269 | if error_message: |
| 270 | print "FAILURE: %s" % error_message |
| 271 | return 1 |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 272 | |
Henrique Nakashima | ea4a56d | 2017-11-29 19:34:19 +0000 | [diff] [blame] | 273 | self.gold_baseline = gold.GoldBaseline(self.options.gold_properties) |
| 274 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 275 | walk_from_dir = finder.TestingDir(test_dir); |
| 276 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 277 | self.test_cases = [] |
| 278 | self.execution_suppressed_cases = [] |
Henrique Nakashima | 62d5076 | 2017-06-27 13:06:23 -0400 | [diff] [blame] | 279 | input_file_re = re.compile('^.+[.](in|pdf)$') |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 280 | if self.args: |
| 281 | for file_name in self.args: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 282 | file_name.replace('.pdf', '.in') |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 283 | input_path = os.path.join(walk_from_dir, file_name) |
| 284 | if not os.path.isfile(input_path): |
| 285 | print "Can't find test file '%s'" % file_name |
| 286 | return 1 |
| 287 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 288 | self.test_cases.append((os.path.basename(input_path), |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 289 | os.path.dirname(input_path))) |
| 290 | else: |
| 291 | for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 292 | for input_filename in filename_list: |
| 293 | if input_file_re.match(input_filename): |
| 294 | input_path = os.path.join(file_dir, input_filename) |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 295 | if self.test_suppressor.IsExecutionSuppressed(input_path): |
| 296 | self.execution_suppressed_cases.append(input_path) |
| 297 | else: |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 298 | if os.path.isfile(input_path): |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 299 | self.test_cases.append((input_filename, file_dir)) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 300 | |
Lei Zhang | 1ee9601 | 2018-04-09 17:31:14 +0000 | [diff] [blame] | 301 | self.test_cases.sort() |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 302 | self.failures = [] |
| 303 | self.surprises = [] |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 304 | self.result_suppressed_cases = [] |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 305 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 306 | # Collect Gold results if an output directory was named. |
| 307 | self.gold_results = None |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 308 | if self.options.gold_output_dir: |
Ryan Harrison | 80302c7 | 2018-05-10 18:27:25 +0000 | [diff] [blame] | 309 | self.gold_results = gold.GoldResults(self.test_type, |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 310 | self.options.gold_output_dir, |
| 311 | self.options.gold_properties, |
| 312 | self.options.gold_key, |
| 313 | self.options.gold_ignore_hashes) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 314 | |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 315 | if self.options.num_workers > 1 and len(self.test_cases) > 1: |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 316 | try: |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 317 | pool = multiprocessing.Pool(self.options.num_workers) |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 318 | worker_func = functools.partial(TestOneFileParallel, self) |
| 319 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 320 | worker_results = pool.imap(worker_func, self.test_cases) |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 321 | for worker_result in worker_results: |
| 322 | result, input_filename, source_dir = worker_result |
| 323 | input_path = os.path.join(source_dir, input_filename) |
| 324 | |
| 325 | self.HandleResult(input_filename, input_path, result) |
| 326 | |
| 327 | except KeyboardInterrupt: |
| 328 | pool.terminate() |
| 329 | finally: |
| 330 | pool.close() |
| 331 | pool.join() |
| 332 | else: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 333 | for test_case in self.test_cases: |
dsinclair | 849284d | 2016-05-17 06:13:36 -0700 | [diff] [blame] | 334 | input_filename, input_file_dir = test_case |
| 335 | result = self.GenerateAndTest(input_filename, input_file_dir) |
| 336 | self.HandleResult(input_filename, |
| 337 | os.path.join(input_file_dir, input_filename), result) |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 338 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 339 | if self.gold_results: |
| 340 | self.gold_results.WriteResults() |
| 341 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 342 | if self.surprises: |
| 343 | self.surprises.sort() |
| 344 | print '\n\nUnexpected Successes:' |
| 345 | for surprise in self.surprises: |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 346 | print surprise |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 347 | |
| 348 | if self.failures: |
| 349 | self.failures.sort() |
| 350 | print '\n\nSummary of Failures:' |
| 351 | for failure in self.failures: |
| 352 | print failure |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 353 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 354 | self._PrintSummary() |
| 355 | |
| 356 | if self.failures: |
Henrique Nakashima | 06673ed | 2017-10-25 17:31:13 -0400 | [diff] [blame] | 357 | if not self.options.ignore_errors: |
dan sinclair | 00d4064 | 2017-01-30 19:48:54 -0800 | [diff] [blame] | 358 | return 1 |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 359 | |
dsinclair | 2a8a20c | 2016-04-25 09:46:17 -0700 | [diff] [blame] | 360 | return 0 |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 361 | |
| 362 | def _PrintSummary(self): |
| 363 | number_test_cases = len(self.test_cases) |
| 364 | number_failures = len(self.failures) |
| 365 | number_suppressed = len(self.result_suppressed_cases) |
| 366 | number_successes = number_test_cases - number_failures - number_suppressed |
| 367 | number_surprises = len(self.surprises) |
| 368 | print |
| 369 | print 'Test cases executed: %d' % number_test_cases |
| 370 | print ' Successes: %d' % number_successes |
| 371 | print ' Suppressed: %d' % number_suppressed |
| 372 | print ' Surprises: %d' % number_surprises |
| 373 | print ' Failures: %d' % number_failures |
| 374 | print |
| 375 | print 'Test cases not executed: %d' % len(self.execution_suppressed_cases) |
| 376 | |
Lei Zhang | 5767aca | 2018-12-05 19:57:46 +0000 | [diff] [blame] | 377 | def SetDeleteOutputOnSuccess(self, new_value): |
| 378 | """Set whether to delete generated output if the test passes.""" |
| 379 | self.delete_output_on_success = new_value |
| 380 | |
Henrique Nakashima | 3bcabf3 | 2017-06-27 09:48:24 -0400 | [diff] [blame] | 381 | def SetEnforceExpectedImages(self, new_value): |
| 382 | """Set whether to enforce that each test case provide an expected image.""" |
| 383 | self.enforce_expected_images = new_value |
Dan Sinclair | aeadad1 | 2017-07-18 16:43:41 -0400 | [diff] [blame] | 384 | |
| 385 | def SetOneShotRenderer(self, new_value): |
| 386 | """Set whether to use the oneshot renderer. """ |
| 387 | self.oneshot_renderer = new_value |