blob: d3640d69ea57926672fec248a0c0fe6f0efa6227 [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
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000122 def RegenerateIfNeeded_(self, input_filename, source_dir):
Lei Zhang30543372019-11-19 19:02:30 +0000123 if (not self.options.regenerate_expected or
124 self.test_suppressor.IsResultSuppressed(input_filename) or
125 self.test_suppressor.IsImageDiffSuppressed(input_filename)):
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000126 return
127
128 platform_only = (self.options.regenerate_expected == 'platform')
Lei Zhang30543372019-11-19 19:02:30 +0000129 self.image_differ.Regenerate(input_filename, source_dir, self.working_dir,
130 platform_only)
Henrique Nakashima15bc9742018-04-26 15:55:07 +0000131
dsinclair2a8a20c2016-04-25 09:46:17 -0700132 def Generate(self, source_dir, input_filename, input_root, pdf_path):
133 original_path = os.path.join(source_dir, input_filename)
134 input_path = os.path.join(source_dir, input_root + '.in')
135
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400136 input_event_path = os.path.join(source_dir, input_root + '.evt')
dsinclair849284d2016-05-17 06:13:36 -0700137 if os.path.exists(input_event_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400138 output_event_path = os.path.splitext(pdf_path)[0] + '.evt'
dsinclair849284d2016-05-17 06:13:36 -0700139 shutil.copyfile(input_event_path, output_event_path)
140
dsinclair2a8a20c2016-04-25 09:46:17 -0700141 if not os.path.exists(input_path):
142 if os.path.exists(original_path):
143 shutil.copyfile(original_path, pdf_path)
144 return None
145
146 sys.stdout.flush()
dsinclair849284d2016-05-17 06:13:36 -0700147
Lei Zhang30543372019-11-19 19:02:30 +0000148 return common.RunCommand([
149 sys.executable, self.fixup_path, '--output-dir=' + self.working_dir,
150 input_path
151 ])
dsinclair2a8a20c2016-04-25 09:46:17 -0700152
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000153 def TestText(self, input_filename, input_root, expected_txt_path, pdf_path):
dsinclair2a8a20c2016-04-25 09:46:17 -0700154 txt_path = os.path.join(self.working_dir, input_root + '.txt')
155
156 with open(txt_path, 'w') as outfile:
Lei Zhang30543372019-11-19 19:02:30 +0000157 cmd_to_run = [
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000158 self.pdfium_test_path, '--send-events', '--time=' + TEST_SEED_TIME
Lei Zhang30543372019-11-19 19:02:30 +0000159 ]
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000160
161 if self.options.disable_javascript:
162 cmd_to_run.append('--disable-javascript')
163
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000164 if self.options.disable_xfa:
165 cmd_to_run.append('--disable-xfa')
166
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000167 cmd_to_run.append(pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -0700168 subprocess.check_call(cmd_to_run, stdout=outfile)
169
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000170 # If the expected file does not exist, the output is expected to be empty.
Lei Zhangfe3ab672019-11-19 19:09:40 +0000171 if not os.path.exists(expected_txt_path):
172 return self._VerifyEmptyText(txt_path)
173
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000174 # If JavaScript is disabled, the output should be empty.
175 # However, if the test is suppressed and JavaScript is disabled, do not
176 # verify that the text is empty so the suppressed test does not surprise.
177 if (self.options.disable_javascript and
178 not self.test_suppressor.IsResultSuppressed(input_filename)):
179 return self._VerifyEmptyText(txt_path)
180
dsinclair2a8a20c2016-04-25 09:46:17 -0700181 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
182 return common.RunCommand(cmd)
183
Lei Zhangfe3ab672019-11-19 19:09:40 +0000184 def _VerifyEmptyText(self, txt_path):
185 try:
186 with open(txt_path, "r") as txt_file:
187 txt_data = txt_file.readlines()
188 if not len(txt_data):
189 return None
190 sys.stdout.write('Unexpected output:\n')
191 for line in txt_data:
192 sys.stdout.write(line)
193 raise Exception('%s should be empty.' % txt_path)
194 except Exception as e:
195 return e
196
K Moon8de7d57d2019-12-05 19:32:33 +0000197 def TestPixel(self, pdf_path, use_ahem):
Lei Zhang30543372019-11-19 19:02:30 +0000198 cmd_to_run = [
199 self.pdfium_test_path, '--send-events', '--png', '--md5',
200 '--time=' + TEST_SEED_TIME
201 ]
Ryan Harrison1118a662018-05-31 19:26:52 +0000202
Dan Sinclairaeadad12017-07-18 16:43:41 -0400203 if self.oneshot_renderer:
204 cmd_to_run.append('--render-oneshot')
Ryan Harrison1118a662018-05-31 19:26:52 +0000205
206 if use_ahem:
207 cmd_to_run.append('--font-dir=%s' % self.font_dir)
208
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000209 if self.options.disable_javascript:
210 cmd_to_run.append('--disable-javascript')
211
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000212 if self.options.disable_xfa:
213 cmd_to_run.append('--disable-xfa')
214
Lei Zhangafce8532019-11-20 18:09:41 +0000215 if self.options.reverse_byte_order:
216 cmd_to_run.append('--reverse-byte-order')
217
stephanafa05e972017-01-02 06:19:41 -0800218 cmd_to_run.append(pdf_path)
219 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700220
221 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800222 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000223
224 if image_paths:
225 for img_path, md5_hash in image_paths:
226 # The output filename without image extension becomes the test name.
227 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
228 # becomes "example_005.pdf.0".
229 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
230
Stephan Altmuellerbcd66f52018-06-21 14:45:44 +0000231 matched = "suppressed"
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000232 if not self.test_suppressor.IsResultSuppressed(input_filename):
233 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
234 if matched == gold.GoldBaseline.MISMATCH:
235 print 'Skia Gold hash mismatch for test case: %s' % test_name
Lei Zhang30543372019-11-19 19:02:30 +0000236 elif matched == gold.GoldBaseline.NO_BASELINE:
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000237 print 'No Skia Gold baseline found for test case: %s' % test_name
238
239 if self.gold_results:
Lei Zhang30543372019-11-19 19:02:30 +0000240 self.gold_results.AddTestResult(test_name, md5_hash, img_path,
241 matched)
stephanafa05e972017-01-02 06:19:41 -0800242
dsinclair2a8a20c2016-04-25 09:46:17 -0700243 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400244 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800245 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700246 self.surprises.append(input_path)
247 else:
dan sinclair00d40642017-01-30 19:48:54 -0800248 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700249 self.failures.append(input_path)
250
dsinclair2a8a20c2016-04-25 09:46:17 -0700251 def Run(self):
K Moon8de7d57d2019-12-05 19:32:33 +0000252 # Running a test defines a number of attributes on the fly.
253 # pylint: disable=attribute-defined-outside-init
254
dsinclair2a8a20c2016-04-25 09:46:17 -0700255 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800256
Lei Zhang30543372019-11-19 19:02:30 +0000257 parser.add_option(
258 '--build-dir',
259 default=os.path.join('out', 'Debug'),
260 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800261
Lei Zhang30543372019-11-19 19:02:30 +0000262 parser.add_option(
263 '-j',
264 default=multiprocessing.cpu_count(),
265 dest='num_workers',
266 type='int',
267 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800268
Lei Zhang30543372019-11-19 19:02:30 +0000269 parser.add_option(
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000270 '--disable-javascript',
271 action="store_true",
272 dest="disable_javascript",
273 help='Prevents JavaScript from executing in PDF files.')
274
275 parser.add_option(
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000276 '--disable-xfa',
277 action="store_true",
278 dest="disable_xfa",
279 help='Prevents processing XFA forms.')
280
281 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000282 '--gold_properties',
283 default='',
284 dest="gold_properties",
285 help='Key value pairs that are written to the top level '
286 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800287
Lei Zhang30543372019-11-19 19:02:30 +0000288 parser.add_option(
289 '--gold_key',
290 default='',
291 dest="gold_key",
292 help='Key value pairs that are added to the "key" field '
293 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800294
Lei Zhang30543372019-11-19 19:02:30 +0000295 parser.add_option(
296 '--gold_output_dir',
297 default='',
298 dest="gold_output_dir",
299 help='Path of where to write the JSON output to be '
300 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800301
Lei Zhang30543372019-11-19 19:02:30 +0000302 parser.add_option(
303 '--gold_ignore_hashes',
304 default='',
305 dest="gold_ignore_hashes",
306 help='Path to a file with MD5 hashes we wish to ignore.')
stephanad5320362017-01-26 15:18:54 -0800307
Lei Zhang30543372019-11-19 19:02:30 +0000308 parser.add_option(
309 '--regenerate_expected',
310 default='',
311 dest="regenerate_expected",
312 help='Regenerates expected images. Valid values are '
313 '"all" to regenerate all expected pngs, and '
314 '"platform" to regenerate only platform-specific '
315 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400316
Lei Zhang30543372019-11-19 19:02:30 +0000317 parser.add_option(
Lei Zhangafce8532019-11-20 18:09:41 +0000318 '--reverse-byte-order',
319 action='store_true',
320 dest="reverse_byte_order",
321 help='Run image-based tests using --reverse-byte-order.')
322
323 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000324 '--ignore_errors',
325 action="store_true",
326 dest="ignore_errors",
327 help='Prevents the return value from being non-zero '
328 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800329
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400330 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700331
Lei Zhang30543372019-11-19 19:02:30 +0000332 if (self.options.regenerate_expected and
333 self.options.regenerate_expected not in ['all', 'platform']):
Henrique Nakashima352e2512017-10-26 11:22:52 -0400334 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
335 return 1
336
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400337 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700338 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
339 self.text_diff_path = finder.ScriptPath('text_diff.py')
Ryan Harrison1118a662018-05-31 19:26:52 +0000340 self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts')
dsinclair2a8a20c2016-04-25 09:46:17 -0700341
dsinclair2a8a20c2016-04-25 09:46:17 -0700342 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700343 if self.test_dir != 'corpus':
344 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
345 else:
346 test_dir = finder.TestingDir(self.test_dir)
347
dsinclair2a8a20c2016-04-25 09:46:17 -0700348 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
349 if not os.path.exists(self.pdfium_test_path):
350 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400351 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700352 return 1
353
354 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
Lei Zhang96eff7c2019-09-19 21:08:58 +0000355 shutil.rmtree(self.working_dir, ignore_errors=True)
356 os.makedirs(self.working_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700357
Lei Zhang30543372019-11-19 19:02:30 +0000358 self.feature_string = subprocess.check_output(
359 [self.pdfium_test_path, '--show-config'])
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000360 self.test_suppressor = suppressor.Suppressor(
Daniel Hosseinian09dbeac2020-01-24 19:41:31 +0000361 finder, self.feature_string, self.options.disable_javascript,
362 self.options.disable_xfa)
Lei Zhangafce8532019-11-20 18:09:41 +0000363 self.image_differ = pngdiffer.PNGDiffer(finder,
364 self.options.reverse_byte_order)
Henrique Nakashima40be5052018-10-10 23:18:14 +0000365 error_message = self.image_differ.CheckMissingTools(
366 self.options.regenerate_expected)
367 if error_message:
368 print "FAILURE: %s" % error_message
369 return 1
dsinclair2a8a20c2016-04-25 09:46:17 -0700370
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000371 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
372
Lei Zhang30543372019-11-19 19:02:30 +0000373 walk_from_dir = finder.TestingDir(test_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700374
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400375 self.test_cases = []
376 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400377 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400378 if self.args:
379 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400380 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700381 input_path = os.path.join(walk_from_dir, file_name)
382 if not os.path.isfile(input_path):
383 print "Can't find test file '%s'" % file_name
384 return 1
385
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400386 self.test_cases.append((os.path.basename(input_path),
Lei Zhang30543372019-11-19 19:02:30 +0000387 os.path.dirname(input_path)))
dsinclair2a8a20c2016-04-25 09:46:17 -0700388 else:
389 for file_dir, _, filename_list in os.walk(walk_from_dir):
390 for input_filename in filename_list:
391 if input_file_re.match(input_filename):
392 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400393 if self.test_suppressor.IsExecutionSuppressed(input_path):
394 self.execution_suppressed_cases.append(input_path)
395 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700396 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400397 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700398
Lei Zhang1ee96012018-04-09 17:31:14 +0000399 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700400 self.failures = []
401 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400402 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700403
stephanafa05e972017-01-02 06:19:41 -0800404 # Collect Gold results if an output directory was named.
405 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400406 if self.options.gold_output_dir:
Lei Zhang30543372019-11-19 19:02:30 +0000407 self.gold_results = gold.GoldResults(
408 self.test_type, self.options.gold_output_dir,
409 self.options.gold_properties, self.options.gold_key,
410 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800411
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400412 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700413 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400414 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700415 worker_func = functools.partial(TestOneFileParallel, self)
416
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400417 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700418 for worker_result in worker_results:
419 result, input_filename, source_dir = worker_result
420 input_path = os.path.join(source_dir, input_filename)
421
422 self.HandleResult(input_filename, input_path, result)
423
424 except KeyboardInterrupt:
425 pool.terminate()
426 finally:
427 pool.close()
428 pool.join()
429 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400430 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700431 input_filename, input_file_dir = test_case
432 result = self.GenerateAndTest(input_filename, input_file_dir)
433 self.HandleResult(input_filename,
434 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700435
stephanafa05e972017-01-02 06:19:41 -0800436 if self.gold_results:
437 self.gold_results.WriteResults()
438
dsinclair2a8a20c2016-04-25 09:46:17 -0700439 if self.surprises:
440 self.surprises.sort()
441 print '\n\nUnexpected Successes:'
442 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400443 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700444
445 if self.failures:
446 self.failures.sort()
447 print '\n\nSummary of Failures:'
448 for failure in self.failures:
449 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800450
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400451 self._PrintSummary()
452
453 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400454 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800455 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400456
dsinclair2a8a20c2016-04-25 09:46:17 -0700457 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400458
459 def _PrintSummary(self):
460 number_test_cases = len(self.test_cases)
461 number_failures = len(self.failures)
462 number_suppressed = len(self.result_suppressed_cases)
463 number_successes = number_test_cases - number_failures - number_suppressed
464 number_surprises = len(self.surprises)
465 print
466 print 'Test cases executed: %d' % number_test_cases
467 print ' Successes: %d' % number_successes
468 print ' Suppressed: %d' % number_suppressed
469 print ' Surprises: %d' % number_surprises
470 print ' Failures: %d' % number_failures
471 print
472 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
473
Lei Zhang5767aca2018-12-05 19:57:46 +0000474 def SetDeleteOutputOnSuccess(self, new_value):
475 """Set whether to delete generated output if the test passes."""
476 self.delete_output_on_success = new_value
477
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400478 def SetEnforceExpectedImages(self, new_value):
479 """Set whether to enforce that each test case provide an expected image."""
480 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400481
482 def SetOneShotRenderer(self, new_value):
483 """Set whether to use the oneshot renderer. """
484 self.oneshot_renderer = new_value