blob: 3a055baf77f284eb9ca2dca7735907cba8aa96ae [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
164 cmd_to_run.append(pdf_path)
dsinclair2a8a20c2016-04-25 09:46:17 -0700165 subprocess.check_call(cmd_to_run, stdout=outfile)
166
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000167 # If the expected file does not exist, the output is expected to be empty.
Lei Zhangfe3ab672019-11-19 19:09:40 +0000168 if not os.path.exists(expected_txt_path):
169 return self._VerifyEmptyText(txt_path)
170
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000171 # If JavaScript is disabled, the output should be empty.
172 # However, if the test is suppressed and JavaScript is disabled, do not
173 # verify that the text is empty so the suppressed test does not surprise.
174 if (self.options.disable_javascript and
175 not self.test_suppressor.IsResultSuppressed(input_filename)):
176 return self._VerifyEmptyText(txt_path)
177
dsinclair2a8a20c2016-04-25 09:46:17 -0700178 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path]
179 return common.RunCommand(cmd)
180
Lei Zhangfe3ab672019-11-19 19:09:40 +0000181 def _VerifyEmptyText(self, txt_path):
182 try:
183 with open(txt_path, "r") as txt_file:
184 txt_data = txt_file.readlines()
185 if not len(txt_data):
186 return None
187 sys.stdout.write('Unexpected output:\n')
188 for line in txt_data:
189 sys.stdout.write(line)
190 raise Exception('%s should be empty.' % txt_path)
191 except Exception as e:
192 return e
193
K Moon8de7d57d2019-12-05 19:32:33 +0000194 def TestPixel(self, pdf_path, use_ahem):
Lei Zhang30543372019-11-19 19:02:30 +0000195 cmd_to_run = [
196 self.pdfium_test_path, '--send-events', '--png', '--md5',
197 '--time=' + TEST_SEED_TIME
198 ]
Ryan Harrison1118a662018-05-31 19:26:52 +0000199
Dan Sinclairaeadad12017-07-18 16:43:41 -0400200 if self.oneshot_renderer:
201 cmd_to_run.append('--render-oneshot')
Ryan Harrison1118a662018-05-31 19:26:52 +0000202
203 if use_ahem:
204 cmd_to_run.append('--font-dir=%s' % self.font_dir)
205
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000206 if self.options.disable_javascript:
207 cmd_to_run.append('--disable-javascript')
208
Lei Zhangafce8532019-11-20 18:09:41 +0000209 if self.options.reverse_byte_order:
210 cmd_to_run.append('--reverse-byte-order')
211
stephanafa05e972017-01-02 06:19:41 -0800212 cmd_to_run.append(pdf_path)
213 return common.RunCommandExtractHashedFiles(cmd_to_run)
dsinclair2a8a20c2016-04-25 09:46:17 -0700214
215 def HandleResult(self, input_filename, input_path, result):
dan sinclair00d40642017-01-30 19:48:54 -0800216 success, image_paths = result
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000217
218 if image_paths:
219 for img_path, md5_hash in image_paths:
220 # The output filename without image extension becomes the test name.
221 # For example, "/path/to/.../testing/corpus/example_005.pdf.0.png"
222 # becomes "example_005.pdf.0".
223 test_name = os.path.splitext(os.path.split(img_path)[1])[0]
224
Stephan Altmuellerbcd66f52018-06-21 14:45:44 +0000225 matched = "suppressed"
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000226 if not self.test_suppressor.IsResultSuppressed(input_filename):
227 matched = self.gold_baseline.MatchLocalResult(test_name, md5_hash)
228 if matched == gold.GoldBaseline.MISMATCH:
229 print 'Skia Gold hash mismatch for test case: %s' % test_name
Lei Zhang30543372019-11-19 19:02:30 +0000230 elif matched == gold.GoldBaseline.NO_BASELINE:
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000231 print 'No Skia Gold baseline found for test case: %s' % test_name
232
233 if self.gold_results:
Lei Zhang30543372019-11-19 19:02:30 +0000234 self.gold_results.AddTestResult(test_name, md5_hash, img_path,
235 matched)
stephanafa05e972017-01-02 06:19:41 -0800236
dsinclair2a8a20c2016-04-25 09:46:17 -0700237 if self.test_suppressor.IsResultSuppressed(input_filename):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400238 self.result_suppressed_cases.append(input_filename)
dan sinclair00d40642017-01-30 19:48:54 -0800239 if success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700240 self.surprises.append(input_path)
241 else:
dan sinclair00d40642017-01-30 19:48:54 -0800242 if not success:
dsinclair2a8a20c2016-04-25 09:46:17 -0700243 self.failures.append(input_path)
244
dsinclair2a8a20c2016-04-25 09:46:17 -0700245 def Run(self):
K Moon8de7d57d2019-12-05 19:32:33 +0000246 # Running a test defines a number of attributes on the fly.
247 # pylint: disable=attribute-defined-outside-init
248
dsinclair2a8a20c2016-04-25 09:46:17 -0700249 parser = optparse.OptionParser()
stephanafa05e972017-01-02 06:19:41 -0800250
Lei Zhang30543372019-11-19 19:02:30 +0000251 parser.add_option(
252 '--build-dir',
253 default=os.path.join('out', 'Debug'),
254 help='relative path from the base source directory')
stephanafa05e972017-01-02 06:19:41 -0800255
Lei Zhang30543372019-11-19 19:02:30 +0000256 parser.add_option(
257 '-j',
258 default=multiprocessing.cpu_count(),
259 dest='num_workers',
260 type='int',
261 help='run NUM_WORKERS jobs in parallel')
stephanafa05e972017-01-02 06:19:41 -0800262
Lei Zhang30543372019-11-19 19:02:30 +0000263 parser.add_option(
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000264 '--disable-javascript',
265 action="store_true",
266 dest="disable_javascript",
267 help='Prevents JavaScript from executing in PDF files.')
268
269 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000270 '--gold_properties',
271 default='',
272 dest="gold_properties",
273 help='Key value pairs that are written to the top level '
274 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800275
Lei Zhang30543372019-11-19 19:02:30 +0000276 parser.add_option(
277 '--gold_key',
278 default='',
279 dest="gold_key",
280 help='Key value pairs that are added to the "key" field '
281 'of the JSON file that is ingested by Gold.')
stephanafa05e972017-01-02 06:19:41 -0800282
Lei Zhang30543372019-11-19 19:02:30 +0000283 parser.add_option(
284 '--gold_output_dir',
285 default='',
286 dest="gold_output_dir",
287 help='Path of where to write the JSON output to be '
288 'uploaded to Gold.')
stephanafa05e972017-01-02 06:19:41 -0800289
Lei Zhang30543372019-11-19 19:02:30 +0000290 parser.add_option(
291 '--gold_ignore_hashes',
292 default='',
293 dest="gold_ignore_hashes",
294 help='Path to a file with MD5 hashes we wish to ignore.')
stephanad5320362017-01-26 15:18:54 -0800295
Lei Zhang30543372019-11-19 19:02:30 +0000296 parser.add_option(
297 '--regenerate_expected',
298 default='',
299 dest="regenerate_expected",
300 help='Regenerates expected images. Valid values are '
301 '"all" to regenerate all expected pngs, and '
302 '"platform" to regenerate only platform-specific '
303 'expected pngs.')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400304
Lei Zhang30543372019-11-19 19:02:30 +0000305 parser.add_option(
Lei Zhangafce8532019-11-20 18:09:41 +0000306 '--reverse-byte-order',
307 action='store_true',
308 dest="reverse_byte_order",
309 help='Run image-based tests using --reverse-byte-order.')
310
311 parser.add_option(
Lei Zhang30543372019-11-19 19:02:30 +0000312 '--ignore_errors',
313 action="store_true",
314 dest="ignore_errors",
315 help='Prevents the return value from being non-zero '
316 'when image comparison fails.')
stephanafa05e972017-01-02 06:19:41 -0800317
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400318 self.options, self.args = parser.parse_args()
dsinclair2a8a20c2016-04-25 09:46:17 -0700319
Lei Zhang30543372019-11-19 19:02:30 +0000320 if (self.options.regenerate_expected and
321 self.options.regenerate_expected not in ['all', 'platform']):
Henrique Nakashima352e2512017-10-26 11:22:52 -0400322 print 'FAILURE: --regenerate_expected must be "all" or "platform"'
323 return 1
324
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400325 finder = common.DirectoryFinder(self.options.build_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700326 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py')
327 self.text_diff_path = finder.ScriptPath('text_diff.py')
Ryan Harrison1118a662018-05-31 19:26:52 +0000328 self.font_dir = os.path.join(finder.TestingDir(), 'resources', 'fonts')
dsinclair2a8a20c2016-04-25 09:46:17 -0700329
dsinclair2a8a20c2016-04-25 09:46:17 -0700330 self.source_dir = finder.TestingDir()
dsinclair849284d2016-05-17 06:13:36 -0700331 if self.test_dir != 'corpus':
332 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir))
333 else:
334 test_dir = finder.TestingDir(self.test_dir)
335
dsinclair2a8a20c2016-04-25 09:46:17 -0700336 self.pdfium_test_path = finder.ExecutablePath('pdfium_test')
337 if not os.path.exists(self.pdfium_test_path):
338 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400339 print 'Use --build-dir to specify its location.'
dsinclair2a8a20c2016-04-25 09:46:17 -0700340 return 1
341
342 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir))
Lei Zhang96eff7c2019-09-19 21:08:58 +0000343 shutil.rmtree(self.working_dir, ignore_errors=True)
344 os.makedirs(self.working_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700345
Lei Zhang30543372019-11-19 19:02:30 +0000346 self.feature_string = subprocess.check_output(
347 [self.pdfium_test_path, '--show-config'])
Daniel Hosseinian77b3a432019-12-18 17:54:37 +0000348 self.test_suppressor = suppressor.Suppressor(
349 finder, self.feature_string, self.options.disable_javascript)
Lei Zhangafce8532019-11-20 18:09:41 +0000350 self.image_differ = pngdiffer.PNGDiffer(finder,
351 self.options.reverse_byte_order)
Henrique Nakashima40be5052018-10-10 23:18:14 +0000352 error_message = self.image_differ.CheckMissingTools(
353 self.options.regenerate_expected)
354 if error_message:
355 print "FAILURE: %s" % error_message
356 return 1
dsinclair2a8a20c2016-04-25 09:46:17 -0700357
Henrique Nakashimaea4a56d2017-11-29 19:34:19 +0000358 self.gold_baseline = gold.GoldBaseline(self.options.gold_properties)
359
Lei Zhang30543372019-11-19 19:02:30 +0000360 walk_from_dir = finder.TestingDir(test_dir)
dsinclair2a8a20c2016-04-25 09:46:17 -0700361
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400362 self.test_cases = []
363 self.execution_suppressed_cases = []
Henrique Nakashima62d50762017-06-27 13:06:23 -0400364 input_file_re = re.compile('^.+[.](in|pdf)$')
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400365 if self.args:
366 for file_name in self.args:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400367 file_name.replace('.pdf', '.in')
dsinclair2a8a20c2016-04-25 09:46:17 -0700368 input_path = os.path.join(walk_from_dir, file_name)
369 if not os.path.isfile(input_path):
370 print "Can't find test file '%s'" % file_name
371 return 1
372
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400373 self.test_cases.append((os.path.basename(input_path),
Lei Zhang30543372019-11-19 19:02:30 +0000374 os.path.dirname(input_path)))
dsinclair2a8a20c2016-04-25 09:46:17 -0700375 else:
376 for file_dir, _, filename_list in os.walk(walk_from_dir):
377 for input_filename in filename_list:
378 if input_file_re.match(input_filename):
379 input_path = os.path.join(file_dir, input_filename)
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400380 if self.test_suppressor.IsExecutionSuppressed(input_path):
381 self.execution_suppressed_cases.append(input_path)
382 else:
dsinclair2a8a20c2016-04-25 09:46:17 -0700383 if os.path.isfile(input_path):
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400384 self.test_cases.append((input_filename, file_dir))
dsinclair2a8a20c2016-04-25 09:46:17 -0700385
Lei Zhang1ee96012018-04-09 17:31:14 +0000386 self.test_cases.sort()
dsinclair2a8a20c2016-04-25 09:46:17 -0700387 self.failures = []
388 self.surprises = []
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400389 self.result_suppressed_cases = []
dsinclair2a8a20c2016-04-25 09:46:17 -0700390
stephanafa05e972017-01-02 06:19:41 -0800391 # Collect Gold results if an output directory was named.
392 self.gold_results = None
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400393 if self.options.gold_output_dir:
Lei Zhang30543372019-11-19 19:02:30 +0000394 self.gold_results = gold.GoldResults(
395 self.test_type, self.options.gold_output_dir,
396 self.options.gold_properties, self.options.gold_key,
397 self.options.gold_ignore_hashes)
stephanafa05e972017-01-02 06:19:41 -0800398
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400399 if self.options.num_workers > 1 and len(self.test_cases) > 1:
dsinclair849284d2016-05-17 06:13:36 -0700400 try:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400401 pool = multiprocessing.Pool(self.options.num_workers)
dsinclair849284d2016-05-17 06:13:36 -0700402 worker_func = functools.partial(TestOneFileParallel, self)
403
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400404 worker_results = pool.imap(worker_func, self.test_cases)
dsinclair849284d2016-05-17 06:13:36 -0700405 for worker_result in worker_results:
406 result, input_filename, source_dir = worker_result
407 input_path = os.path.join(source_dir, input_filename)
408
409 self.HandleResult(input_filename, input_path, result)
410
411 except KeyboardInterrupt:
412 pool.terminate()
413 finally:
414 pool.close()
415 pool.join()
416 else:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400417 for test_case in self.test_cases:
dsinclair849284d2016-05-17 06:13:36 -0700418 input_filename, input_file_dir = test_case
419 result = self.GenerateAndTest(input_filename, input_file_dir)
420 self.HandleResult(input_filename,
421 os.path.join(input_file_dir, input_filename), result)
dsinclair2a8a20c2016-04-25 09:46:17 -0700422
stephanafa05e972017-01-02 06:19:41 -0800423 if self.gold_results:
424 self.gold_results.WriteResults()
425
dsinclair2a8a20c2016-04-25 09:46:17 -0700426 if self.surprises:
427 self.surprises.sort()
428 print '\n\nUnexpected Successes:'
429 for surprise in self.surprises:
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400430 print surprise
dsinclair2a8a20c2016-04-25 09:46:17 -0700431
432 if self.failures:
433 self.failures.sort()
434 print '\n\nSummary of Failures:'
435 for failure in self.failures:
436 print failure
dan sinclair00d40642017-01-30 19:48:54 -0800437
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400438 self._PrintSummary()
439
440 if self.failures:
Henrique Nakashima06673ed2017-10-25 17:31:13 -0400441 if not self.options.ignore_errors:
dan sinclair00d40642017-01-30 19:48:54 -0800442 return 1
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400443
dsinclair2a8a20c2016-04-25 09:46:17 -0700444 return 0
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400445
446 def _PrintSummary(self):
447 number_test_cases = len(self.test_cases)
448 number_failures = len(self.failures)
449 number_suppressed = len(self.result_suppressed_cases)
450 number_successes = number_test_cases - number_failures - number_suppressed
451 number_surprises = len(self.surprises)
452 print
453 print 'Test cases executed: %d' % number_test_cases
454 print ' Successes: %d' % number_successes
455 print ' Suppressed: %d' % number_suppressed
456 print ' Surprises: %d' % number_surprises
457 print ' Failures: %d' % number_failures
458 print
459 print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
460
Lei Zhang5767aca2018-12-05 19:57:46 +0000461 def SetDeleteOutputOnSuccess(self, new_value):
462 """Set whether to delete generated output if the test passes."""
463 self.delete_output_on_success = new_value
464
Henrique Nakashima3bcabf32017-06-27 09:48:24 -0400465 def SetEnforceExpectedImages(self, new_value):
466 """Set whether to enforce that each test case provide an expected image."""
467 self.enforce_expected_images = new_value
Dan Sinclairaeadad12017-07-18 16:43:41 -0400468
469 def SetOneShotRenderer(self, new_value):
470 """Set whether to use the oneshot renderer. """
471 self.oneshot_renderer = new_value