blob: f4b45ac6f0b5f47f32f1dcf5d116c59a35f18307 [file] [log] [blame]
dsinclair2a8a20c2016-04-25 09:46:17 -07001#!/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
dsinclair849284d2016-05-17 06:13:36 -07006import functools
7import multiprocessing
dsinclair2a8a20c2016-04-25 09:46:17 -07008import optparse
9import os
10import re
dsinclair849284d2016-05-17 06:13:36 -070011import shutil
dsinclair2a8a20c2016-04-25 09:46:17 -070012import subprocess
13import sys
14
K Moon8de7d57d2019-12-05 19:32:33 +000015# pylint: disable=relative-import
dsinclair2a8a20c2016-04-25 09:46:17 -070016import common
stephanafa05e972017-01-02 06:19:41 -080017import gold
dsinclair2a8a20c2016-04-25 09:46:17 -070018import pngdiffer
19import suppressor
20
Ryan Harrison70cca362018-08-10 18:55:46 +000021# Arbitrary timestamp, expressed in seconds since the epoch, used to make sure
22# that tests that depend on the current time are stable. Happens to be the
23# timestamp of the first commit to repo, 2014/5/9 17:48:50.
24TEST_SEED_TIME = "1399672130"
25
Lei Zhangfe3ab672019-11-19 19:09:40 +000026# List of test types that should run text tests instead of pixel tests.
27TEXT_TESTS = ['javascript']
28
Lei Zhang30543372019-11-19 19:02:30 +000029
30class KeyboardInterruptError(Exception):
31 pass
32
dsinclair849284d2016-05-17 06:13:36 -070033
dsinclair2a8a20c2016-04-25 09:46:17 -070034# Nomenclature:
35# x_root - "x"
36# x_filename - "x.ext"
37# x_path - "path/to/a/b/c/x.ext"
38# c_dir - "path/to/a/b/c"
39
Lei Zhang30543372019-11-19 19:02:30 +000040
dsinclair849284d2016-05-17 06:13:36 -070041def TestOneFileParallel(this, test_case):
42 """Wrapper to call GenerateAndTest() and redirect output to stdout."""
43 try:
44 input_filename, source_dir = test_case
Lei Zhang30543372019-11-19 19:02:30 +000045 result = this.GenerateAndTest(input_filename, source_dir)
dsinclair849284d2016-05-17 06:13:36 -070046 return (result, input_filename, source_dir)
47 except KeyboardInterrupt:
48 raise KeyboardInterruptError()
49
50
Lei Zhang5767aca2018-12-05 19:57:46 +000051def DeleteFiles(files):
52 """Utility function to delete a list of files"""
53 for f in files:
54 if os.path.exists(f):
55 os.remove(f)
56
57
dsinclair2a8a20c2016-04-25 09:46:17 -070058class TestRunner:
Lei Zhang30543372019-11-19 19:02:30 +000059
dsinclair2a8a20c2016-04-25 09:46:17 -070060 def __init__(self, dirname):
Ryan Harrison80302c72018-05-10 18:27:25 +000061 # Currently the only used directories are corpus, javascript, and pixel,
62 # which all correspond directly to the type for the test being run. In the
63 # future if there are tests that don't have this clean correspondence, then
64 # an argument for the type will need to be added.
dsinclair2a8a20c2016-04-25 09:46:17 -070065 self.test_dir = dirname
Ryan Harrison80302c72018-05-10 18:27:25 +000066 self.test_type = dirname
Lei Zhang5767aca2018-12-05 19:57:46 +000067 self.delete_output_on_success = False
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040068 self.enforce_expected_images = False
Dan Sinclairaeadad12017-07-18 16:43:41 -040069 self.oneshot_renderer = False
dsinclair2a8a20c2016-04-25 09:46:17 -070070
stephanafa05e972017-01-02 06:19:41 -080071 # GenerateAndTest returns a tuple <success, outputfiles> where
72 # success is a boolean indicating whether the tests passed comparison
73 # tests and outputfiles is a list tuples:
74 # (path_to_image, md5_hash_of_pixelbuffer)
dsinclair2a8a20c2016-04-25 09:46:17 -070075 def GenerateAndTest(self, input_filename, source_dir):
76 input_root, _ = os.path.splitext(input_filename)
dsinclair2a8a20c2016-04-25 09:46:17 -070077 pdf_path = os.path.join(self.working_dir, input_root + '.pdf')
78
79 # Remove any existing generated images from previous runs.
80 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
81 self.working_dir)
Lei Zhang5767aca2018-12-05 19:57:46 +000082 DeleteFiles(actual_images)
dsinclair2a8a20c2016-04-25 09:46:17 -070083
84 sys.stdout.flush()
85
86 raised_exception = self.Generate(source_dir, input_filename, input_root,
87 pdf_path)
88
Henrique Nakashima3bcabf32017-06-27 09:48:24 -040089 if raised_exception is not None:
90 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -080091 return False, []
dsinclair2a8a20c2016-04-25 09:46:17 -070092
stephanafa05e972017-01-02 06:19:41 -080093 results = []
Lei Zhangfe3ab672019-11-19 19:09:40 +000094 if self.test_type in TEXT_TESTS:
95 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt')
Daniel Hosseinian77b3a432019-12-18 17:54:37 +000096 raised_exception = self.TestText(input_filename, input_root,
97 expected_txt_path, pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -070098 else:
Lei Zhangfe3ab672019-11-19 19:09:40 +000099 use_ahem = 'use_ahem' in source_dir
K Moon8de7d57d2019-12-05 19:32:33 +0000100 raised_exception, results = self.TestPixel(pdf_path, use_ahem)
dsinclair2a8a20c2016-04-25 09:46:17 -0700101
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400102 if raised_exception is not None:
103 print 'FAILURE: %s; %s' % (input_filename, raised_exception)
stephanafa05e972017-01-02 06:19:41 -0800104 return False, results
dsinclair2a8a20c2016-04-25 09:46:17 -0700105
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400106 if actual_images:
dsinclair2a8a20c2016-04-25 09:46:17 -0700107 if self.image_differ.HasDifferences(input_filename, source_dir,
108 self.working_dir):
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000109 self.RegenerateIfNeeded_(input_filename, source_dir)
stephanafa05e972017-01-02 06:19:41 -0800110 return False, results
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400111 else:
Lei Zhang30543372019-11-19 19:02:30 +0000112 if (self.enforce_expected_images and
113 not self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000114 self.RegenerateIfNeeded_(input_filename, source_dir)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400115 print 'FAILURE: %s; Missing expected images' % input_filename
116 return False, results
117
Lei Zhang5767aca2018-12-05 19:57:46 +0000118 if self.delete_output_on_success:
119 DeleteFiles(actual_images)
stephanafa05e972017-01-02 06:19:41 -0800120 return True, results
dsinclair2a8a20c2016-04-25 09:46:17 -0700121
Hui Yingstb8b3f5f2020-04-11 00:20:36 +0000122 # TODO(crbug.com/pdfium/1508): Add support for an option to automatically
123 # generate Skia/SkiaPaths specific expected results.
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000124 def RegenerateIfNeeded_(self, input_filename, source_dir):
Lei Zhang30543372019-11-19 19:02:30 +0000125 if (not self.options.regenerate_expected or
126 self.test_suppressor.IsResultSuppressed(input_filename) or
127 self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000128 return
129
130 platform_only = (self.options.regenerate_expected == 'platform')
Lei Zhang30543372019-11-19 19:02:30 +0000131 self.image_differ.Regenerate(input_filename, source_dir, self.working_dir,
132 platform_only)
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000133
dsinclair2a8a20c2016-04-25 09:46:17 -0700134 def Generate(self, source_dir, input_filename, input_root, pdf_path):
135 original_path = os.path.join(source_dir, input_filename)
136 input_path = os.path.join(source_dir, input_root + '.in')
137
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400138 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700139 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400140 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700141 shutil.copyfile(input_event_path, output_event_path)
142
dsinclair2a8a20c2016-04-25 09:46:17 -0700143 if not os.path.exists(input_path):
144 if os.path.exists(original_path):
145 shutil.copyfile(original_path, pdf_path)
146 return None
147
148 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700149
Lei Zhang30543372019-11-19 19:02:30 +0000150 return common.RunCommand([
151 sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
152 input_path
153 ])
dsinclair2a8a20c2016-04-25 09:46:17 -0700154
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000155 def TestText(self, input_filename, input_root, expected_txt_path, pdf_path):
dsinclair2a8a20c2016-04-25 09:46:17 -0700156 txt_path = os.path.join(self.working_dir, input_root + '.txt')
157
158 with open(txt_path, 'w') as outfile:
Lei Zhang30543372019-11-19 19:02:30 +0000159 cmd_to_run = [
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000160 self.pdfium_test_path, '--send-events', '--time=' + TEST_SEED_TIME
Lei Zhang30543372019-11-19 19:02:30 +0000161 ]
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000162
163 if self.options.disable_javascript:
164 cmd_to_run.append('--disable-javascript')
165
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000166 if self.options.disable_xfa:
167 cmd_to_run.append('--disable-xfa')
168
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000169 cmd_to_run.append(pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -0700170 subprocess.check_call(cmd_to_run, stdout=outfile)
171
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000172 # If the expected file does not exist, the output is expected to be empty.
Lei Zhangfe3ab672019-11-19 19:09:40 +0000173 if not os.path.exists(expected_txt_path):
174 return self._VerifyEmptyText(txt_path)
175
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000176 # If JavaScript is disabled, the output should be empty.
177 # However, if the test is suppressed and JavaScript is disabled, do not
178 # verify that the text is empty so the suppressed test does not surprise.
179 if (self.options.disable_javascript and
180 not self.test_suppressor.IsResultSuppressed(input_filename)):
181 return self._VerifyEmptyText(txt_path)
182
dsinclair2a8a20c2016-04-25 09:46:17 -0700183 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
184 return common.RunCommand(cmd)
185
Lei Zhangfe3ab672019-11-19 19:09:40 +0000186 def _VerifyEmptyText(self, txt_path):
187 try:
188 with open(txt_path, "r") as txt_file:
189 txt_data = txt_file.readlines()
190 if not len(txt_data):
191 return None
192 sys.stdout.write('Unexpected output:\n')
193 for line in txt_data:
194 sys.stdout.write(line)
195 raise Exception('%s should be empty.' % txt_path)
196 except Exception as e:
197 return e
198
K Moon8de7d57d2019-12-05 19:32:33 +0000199 def TestPixel(self, pdf_path, use_ahem):
Lei Zhang30543372019-11-19 19:02:30 +0000200 cmd_to_run = [
201 self.pdfium_test_path, '--send-events', '--png', '--md5',
202 '--time=' + TEST_SEED_TIME
203 ]
Ryan Harrison1118a662018-05-31 19:26:52 +0000204
Dan Sinclairaeadad12017-07-18 16:43:41 -0400205 if self.oneshot_renderer:
206 cmd_to_run.append('--render-oneshot')
Ryan Harrison1118a662018-05-31 19:26:52 +0000207
208 if use_ahem:
209 cmd_to_run.append('--font-dir=%s' % self.font_dir)
210
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000211 if self.options.disable_javascript:
212 cmd_to_run.append('--disable-javascript')
213
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000214 if self.options.disable_xfa:
215 cmd_to_run.append('--disable-xfa')
216
Lei Zhangafce8532019-11-20 18:09:41 +0000217 if self.options.reverse_byte_order:
218 cmd_to_run.append('--reverse-byte-order')
219
stephanafa05e972017-01-02 06:19:41 -0800220 cmd_to_run.append(pdf_path)
221 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700222
223 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800224 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000225
226 if image_paths:
227 for img_path, md5_hash in image_paths:
228 # The output filename without image extension becomes the test name.
229 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
230 # becomes "example_005.pdf.0".
231 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
232
Stephan Altmuellerbcd66f52018-06-21 14:45:44 +0000233 matched = "suppressed"
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000234 if not self.test_suppressor.IsResultSuppressed(input_filename):
235 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
236 if matched == gold.GoldBaseline.MISMATCH:
237 print 'Skia Gold hash mismatch for test case: %s' % test_name
Lei Zhang30543372019-11-19 19:02:30 +0000238 elif matched == gold.GoldBaseline.NO_BASELINE:
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000239 print 'No Skia Gold baseline found for test case: %s' % test_name
240
241 if self.gold_results:
Lei Zhang30543372019-11-19 19:02:30 +0000242 self.gold_results.AddTestResult(test_name, md5_hash, img_path,
243 matched)
stephanafa05e972017-01-02 06:19:41 -0800244
dsinclair2a8a20c2016-04-25 09:46:17 -0700245 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400246 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800247 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700248 self.surprises.append(input_path)
249 else:
dan sinclair00d40642017-01-30 19:48:54 -0800250 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700251 self.failures.append(input_path)
252
dsinclair2a8a20c2016-04-25 09:46:17 -0700253 def Run(self):
K Moon8de7d57d2019-12-05 19:32:33 +0000254 # Running a test defines a number of attributes on the fly.
255 # pylint: disable=attribute-defined-outside-init
256
dsinclair2a8a20c2016-04-25 09:46:17 -0700257 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800258
Lei Zhang30543372019-11-19 19:02:30 +0000259 parser.add_option(
260 '--build-dir',
261 default=os.path.join('out', 'Debug'),
262 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800263
Lei Zhang30543372019-11-19 19:02:30 +0000264 parser.add_option(
265 '-j',
266 default=multiprocessing.cpu_count(),
267 dest='num_workers',
268 type='int',
269 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800270
Lei Zhang30543372019-11-19 19:02:30 +0000271 parser.add_option(
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000272 '--disable-javascript',
273 action="store_true",
274 dest="disable_javascript",
275 help='Prevents JavaScript from executing in PDF files.')
276
277 parser.add_option(
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000278 '--disable-xfa',
279 action="store_true",
280 dest="disable_xfa",
281 help='Prevents processing XFA forms.')
282
283 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000284 '--gold_properties',
285 default='',
286 dest="gold_properties",
287 help='Key value pairs that are written to the top level '
288 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800289
Lei Zhang30543372019-11-19 19:02:30 +0000290 parser.add_option(
291 '--gold_key',
292 default='',
293 dest="gold_key",
294 help='Key value pairs that are added to the "key" field '
295 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800296
Lei Zhang30543372019-11-19 19:02:30 +0000297 parser.add_option(
298 '--gold_output_dir',
299 default='',
300 dest="gold_output_dir",
301 help='Path of where to write the JSON output to be '
302 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800303
Lei Zhang30543372019-11-19 19:02:30 +0000304 parser.add_option(
305 '--gold_ignore_hashes',
306 default='',
307 dest="gold_ignore_hashes",
308 help='Path to a file with MD5 hashes we wish to ignore.')
stephanad5320362017-01-26 15:18:54 -0800309
Lei Zhang30543372019-11-19 19:02:30 +0000310 parser.add_option(
311 '--regenerate_expected',
312 default='',
313 dest="regenerate_expected",
314 help='Regenerates expected images. Valid values are '
315 '"all" to regenerate all expected pngs, and '
316 '"platform" to regenerate only platform-specific '
317 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400318
Lei Zhang30543372019-11-19 19:02:30 +0000319 parser.add_option(
Lei Zhangafce8532019-11-20 18:09:41 +0000320 '--reverse-byte-order',
321 action='store_true',
322 dest="reverse_byte_order",
323 help='Run image-based tests using --reverse-byte-order.')
324
325 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000326 '--ignore_errors',
327 action="store_true",
328 dest="ignore_errors",
329 help='Prevents the return value from being non-zero '
330 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800331
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400332 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700333
Lei Zhang30543372019-11-19 19:02:30 +0000334 if (self.options.regenerate_expected and
335 self.options.regenerate_expected not in ['all', 'platform']):
Henrique Nakashima352e2512017-10-26 11:22:52 -0400336 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
337 return 1
338
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400339 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700340 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
341 self.text_diff_path = finder.ScriptPath('text_diff.py')
Ryan Harrison1118a662018-05-31 19:26:52 +0000342 self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts')
dsinclair2a8a20c2016-04-25 09:46:17 -0700343
dsinclair2a8a20c2016-04-25 09:46:17 -0700344 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700345 if self.test_dir != 'corpus':
346 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
347 else:
348 test_dir = finder.TestingDir(self.test_dir)
349
dsinclair2a8a20c2016-04-25 09:46:17 -0700350 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
351 if not os.path.exists(self.pdfium_test_path):
352 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400353 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700354 return 1
355
356 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
Lei Zhang96eff7c2019-09-19 21:08:58 +0000357 shutil.rmtree(self.working_dir, ignore_errors=True)
358 os.makedirs(self.working_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700359
Lei Zhang76f95692020-04-01 22:16:25 +0000360 self.features = subprocess.check_output(
361 [self.pdfium_test_path, '--show-config']).strip().split(',')
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000362 self.test_suppressor = suppressor.Suppressor(
Lei Zhang76f95692020-04-01 22:16:25 +0000363 finder, self.features, self.options.disable_javascript,
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000364 self.options.disable_xfa)
Hui Yingstb8b3f5f2020-04-11 00:20:36 +0000365 self.image_differ = pngdiffer.PNGDiffer(finder, self.features,
Lei Zhangafce8532019-11-20 18:09:41 +0000366 self.options.reverse_byte_order)
Henrique Nakashima40be5052018-10-10 23:18:14 +0000367 error_message = self.image_differ.CheckMissingTools(
368 self.options.regenerate_expected)
369 if error_message:
370 print "FAILURE: %s" % error_message
371 return 1
dsinclair2a8a20c2016-04-25 09:46:17 -0700372
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000373 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
374
Lei Zhang30543372019-11-19 19:02:30 +0000375 walk_from_dir = finder.TestingDir(test_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700376
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400377 self.test_cases = []
378 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400379 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400380 if self.args:
381 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400382 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700383 input_path = os.path.join(walk_from_dir, file_name)
384 if not os.path.isfile(input_path):
385 print "Can't find test file '%s'" % file_name
386 return 1
387
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400388 self.test_cases.append((os.path.basename(input_path),
Lei Zhang30543372019-11-19 19:02:30 +0000389 os.path.dirname(input_path)))
dsinclair2a8a20c2016-04-25 09:46:17 -0700390 else:
391 for file_dir, _, filename_list in os.walk(walk_from_dir):
392 for input_filename in filename_list:
393 if input_file_re.match(input_filename):
394 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400395 if self.test_suppressor.IsExecutionSuppressed(input_path):
396 self.execution_suppressed_cases.append(input_path)
397 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700398 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400399 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700400
Lei Zhang1ee96012018-04-09 17:31:14 +0000401 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700402 self.failures = []
403 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400404 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700405
stephanafa05e972017-01-02 06:19:41 -0800406 # Collect Gold results if an output directory was named.
407 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400408 if self.options.gold_output_dir:
Lei Zhang30543372019-11-19 19:02:30 +0000409 self.gold_results = gold.GoldResults(
410 self.test_type, self.options.gold_output_dir,
411 self.options.gold_properties, self.options.gold_key,
412 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800413
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400414 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700415 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400416 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700417 worker_func = functools.partial(TestOneFileParallel, self)
418
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400419 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700420 for worker_result in worker_results:
421 result, input_filename, source_dir = worker_result
422 input_path = os.path.join(source_dir, input_filename)
423
424 self.HandleResult(input_filename, input_path, result)
425
426 except KeyboardInterrupt:
427 pool.terminate()
428 finally:
429 pool.close()
430 pool.join()
431 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400432 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700433 input_filename, input_file_dir = test_case
434 result = self.GenerateAndTest(input_filename, input_file_dir)
435 self.HandleResult(input_filename,
436 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700437
stephanafa05e972017-01-02 06:19:41 -0800438 if self.gold_results:
439 self.gold_results.WriteResults()
440
dsinclair2a8a20c2016-04-25 09:46:17 -0700441 if self.surprises:
442 self.surprises.sort()
443 print '\n\nUnexpected Successes:'
444 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400445 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700446
447 if self.failures:
448 self.failures.sort()
449 print '\n\nSummary of Failures:'
450 for failure in self.failures:
451 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800452
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400453 self._PrintSummary()
454
455 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400456 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800457 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400458
dsinclair2a8a20c2016-04-25 09:46:17 -0700459 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400460
461 def _PrintSummary(self):
462 number_test_cases = len(self.test_cases)
463 number_failures = len(self.failures)
464 number_suppressed = len(self.result_suppressed_cases)
465 number_successes = number_test_cases - number_failures - number_suppressed
466 number_surprises = len(self.surprises)
467 print
468 print 'Test cases executed: %d' % number_test_cases
469 print ' Successes: %d' % number_successes
470 print ' Suppressed: %d' % number_suppressed
471 print ' Surprises: %d' % number_surprises
472 print ' Failures: %d' % number_failures
473 print
474 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
475
Lei Zhang5767aca2018-12-05 19:57:46 +0000476 def SetDeleteOutputOnSuccess(self, new_value):
477 """Set whether to delete generated output if the test passes."""
478 self.delete_output_on_success = new_value
479
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400480 def SetEnforceExpectedImages(self, new_value):
481 """Set whether to enforce that each test case provide an expected image."""
482 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400483
484 def SetOneShotRenderer(self, new_value):
485 """Set whether to use the oneshot renderer. """
486 self.oneshot_renderer = new_value