| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2016 The PDFium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import cStringIO |
| 7 import functools |
| 8 import multiprocessing |
| 6 import optparse | 9 import optparse |
| 7 import os | 10 import os |
| 8 import re | 11 import re |
| 12 import shutil |
| 9 import subprocess | 13 import subprocess |
| 10 import sys | 14 import sys |
| 11 | 15 |
| 12 import common | 16 import common |
| 13 import pngdiffer | 17 import pngdiffer |
| 14 import suppressor | 18 import suppressor |
| 15 | 19 |
| 20 class KeyboardInterruptError(Exception): pass |
| 21 |
| 16 # Nomenclature: | 22 # Nomenclature: |
| 17 # x_root - "x" | 23 # x_root - "x" |
| 18 # x_filename - "x.ext" | 24 # x_filename - "x.ext" |
| 19 # x_path - "path/to/a/b/c/x.ext" | 25 # x_path - "path/to/a/b/c/x.ext" |
| 20 # c_dir - "path/to/a/b/c" | 26 # c_dir - "path/to/a/b/c" |
| 21 | 27 |
| 28 def TestOneFileParallel(this, test_case): |
| 29 """Wrapper to call GenerateAndTest() and redirect output to stdout.""" |
| 30 try: |
| 31 input_filename, source_dir = test_case |
| 32 result = this.GenerateAndTest(input_filename, source_dir); |
| 33 return (result, input_filename, source_dir) |
| 34 except KeyboardInterrupt: |
| 35 raise KeyboardInterruptError() |
| 36 |
| 37 |
| 22 class TestRunner: | 38 class TestRunner: |
| 23 def __init__(self, dirname): | 39 def __init__(self, dirname): |
| 24 self.test_dir = dirname | 40 self.test_dir = dirname |
| 25 | 41 |
| 26 def GenerateAndTest(self, input_filename, source_dir): | 42 def GenerateAndTest(self, input_filename, source_dir): |
| 27 input_root, _ = os.path.splitext(input_filename) | 43 input_root, _ = os.path.splitext(input_filename) |
| 28 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') | 44 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 29 | 45 |
| 30 pdf_path = os.path.join(self.working_dir, input_root + '.pdf') | 46 pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 31 | 47 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 58 if self.image_differ.HasDifferences(input_filename, source_dir, | 74 if self.image_differ.HasDifferences(input_filename, source_dir, |
| 59 self.working_dir): | 75 self.working_dir): |
| 60 return False | 76 return False |
| 61 | 77 |
| 62 return True | 78 return True |
| 63 | 79 |
| 64 def Generate(self, source_dir, input_filename, input_root, pdf_path): | 80 def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 65 original_path = os.path.join(source_dir, input_filename) | 81 original_path = os.path.join(source_dir, input_filename) |
| 66 input_path = os.path.join(source_dir, input_root + '.in') | 82 input_path = os.path.join(source_dir, input_root + '.in') |
| 67 | 83 |
| 84 input_event_path = os.path.join(source_dir, input_root + ".evt") |
| 85 if os.path.exists(input_event_path): |
| 86 output_event_path = os.path.splitext(pdf_path)[0] + ".evt" |
| 87 shutil.copyfile(input_event_path, output_event_path) |
| 88 |
| 68 if not os.path.exists(input_path): | 89 if not os.path.exists(input_path): |
| 69 if os.path.exists(original_path): | 90 if os.path.exists(original_path): |
| 70 shutil.copyfile(original_path, pdf_path) | 91 shutil.copyfile(original_path, pdf_path) |
| 71 return None | 92 return None |
| 72 | 93 |
| 73 sys.stdout.flush() | 94 sys.stdout.flush() |
| 95 |
| 74 return common.RunCommand( | 96 return common.RunCommand( |
| 75 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, | 97 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 76 input_path]) | 98 input_path]) |
| 77 | 99 |
| 78 | 100 |
| 79 def TestText(self, input_root, expected_txt_path, pdf_path): | 101 def TestText(self, input_root, expected_txt_path, pdf_path): |
| 80 txt_path = os.path.join(self.working_dir, input_root + '.txt') | 102 txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 81 | 103 |
| 82 with open(txt_path, 'w') as outfile: | 104 with open(txt_path, 'w') as outfile: |
| 83 # add Dr. Memory wrapper if exist | 105 # add Dr. Memory wrapper if exist |
| (...skipping 18 matching lines...) Expand all Loading... |
| 102 self.surprises.append(input_path) | 124 self.surprises.append(input_path) |
| 103 else: | 125 else: |
| 104 if not result: | 126 if not result: |
| 105 self.failures.append(input_path) | 127 self.failures.append(input_path) |
| 106 | 128 |
| 107 | 129 |
| 108 def Run(self): | 130 def Run(self): |
| 109 parser = optparse.OptionParser() | 131 parser = optparse.OptionParser() |
| 110 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 132 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 111 help='relative path from the base source directory') | 133 help='relative path from the base source directory') |
| 112 parser.add_option('-j', default=1, | 134 parser.add_option('-j', default=multiprocessing.cpu_count(), |
| 113 dest='num_workers', type='int', | 135 dest='num_workers', type='int', |
| 114 help='run NUM_WORKERS jobs in parallel') | 136 help='run NUM_WORKERS jobs in parallel') |
| 115 parser.add_option('--wrapper', default='', dest="wrapper", | 137 parser.add_option('--wrapper', default='', dest="wrapper", |
| 116 help='wrapper for running test under Dr. Memory') | 138 help='wrapper for running test under Dr. Memory') |
| 117 options, args = parser.parse_args() | 139 options, args = parser.parse_args() |
| 118 | 140 |
| 119 finder = common.DirectoryFinder(options.build_dir) | 141 finder = common.DirectoryFinder(options.build_dir) |
| 120 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') | 142 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 121 self.text_diff_path = finder.ScriptPath('text_diff.py') | 143 self.text_diff_path = finder.ScriptPath('text_diff.py') |
| 122 | 144 |
| 123 self.drmem_wrapper = options.wrapper | 145 self.drmem_wrapper = options.wrapper |
| 124 | 146 |
| 125 self.source_dir = finder.TestingDir() | 147 self.source_dir = finder.TestingDir() |
| 148 if self.test_dir != 'corpus': |
| 149 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 150 else: |
| 151 test_dir = finder.TestingDir(self.test_dir) |
| 152 |
| 126 self.pdfium_test_path = finder.ExecutablePath('pdfium_test') | 153 self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 127 if not os.path.exists(self.pdfium_test_path): | 154 if not os.path.exists(self.pdfium_test_path): |
| 128 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path | 155 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
| 129 print "Use --build-dir to specify its location." | 156 print "Use --build-dir to specify its location." |
| 130 return 1 | 157 return 1 |
| 131 | 158 |
| 132 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) | 159 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
| 133 if not os.path.exists(self.working_dir): | 160 if not os.path.exists(self.working_dir): |
| 134 os.makedirs(self.working_dir) | 161 os.makedirs(self.working_dir) |
| 135 | 162 |
| 136 self.feature_string = subprocess.check_output([self.pdfium_test_path, | 163 self.feature_string = subprocess.check_output([self.pdfium_test_path, |
| 137 '--show-config']) | 164 '--show-config']) |
| 138 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) | 165 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 139 self.image_differ = pngdiffer.PNGDiffer(finder) | 166 self.image_differ = pngdiffer.PNGDiffer(finder) |
| 140 | 167 |
| 141 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) | |
| 142 walk_from_dir = finder.TestingDir(test_dir); | 168 walk_from_dir = finder.TestingDir(test_dir); |
| 143 | 169 |
| 144 test_cases = [] | 170 test_cases = [] |
| 145 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$') | 171 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$') |
| 146 if len(args): | 172 if len(args): |
| 147 for file_name in args: | 173 for file_name in args: |
| 148 file_name.replace(".pdf", ".in") | 174 file_name.replace(".pdf", ".in") |
| 149 input_path = os.path.join(walk_from_dir, file_name) | 175 input_path = os.path.join(walk_from_dir, file_name) |
| 150 if not os.path.isfile(input_path): | 176 if not os.path.isfile(input_path): |
| 151 print "Can't find test file '%s'" % file_name | 177 print "Can't find test file '%s'" % file_name |
| 152 return 1 | 178 return 1 |
| 153 | 179 |
| 154 test_cases.append((os.path.basename(input_path), | 180 test_cases.append((os.path.basename(input_path), |
| 155 os.path.dirname(input_path))) | 181 os.path.dirname(input_path))) |
| 156 else: | 182 else: |
| 157 for file_dir, _, filename_list in os.walk(walk_from_dir): | 183 for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 158 for input_filename in filename_list: | 184 for input_filename in filename_list: |
| 159 if input_file_re.match(input_filename): | 185 if input_file_re.match(input_filename): |
| 160 input_path = os.path.join(file_dir, input_filename) | 186 input_path = os.path.join(file_dir, input_filename) |
| 161 if not self.test_suppressor.IsExecutionSuppressed(input_path): | 187 if not self.test_suppressor.IsExecutionSuppressed(input_path): |
| 162 if os.path.isfile(input_path): | 188 if os.path.isfile(input_path): |
| 163 test_cases.append((input_filename, file_dir)) | 189 test_cases.append((input_filename, file_dir)) |
| 164 | 190 |
| 165 self.failures = [] | 191 self.failures = [] |
| 166 self.surprises = [] | 192 self.surprises = [] |
| 167 | 193 |
| 168 for test_case in test_cases: | 194 if options.num_workers > 1 and len(test_cases) > 1: |
| 169 input_filename, input_file_dir = test_case | 195 try: |
| 170 result = self.GenerateAndTest(input_filename, input_file_dir) | 196 pool = multiprocessing.Pool(options.num_workers) |
| 171 self.HandleResult(input_filename, | 197 worker_func = functools.partial(TestOneFileParallel, self) |
| 172 os.path.join(input_file_dir, input_filename), result) | 198 |
| 199 worker_results = pool.imap(worker_func, test_cases) |
| 200 for worker_result in worker_results: |
| 201 result, input_filename, source_dir = worker_result |
| 202 input_path = os.path.join(source_dir, input_filename) |
| 203 |
| 204 self.HandleResult(input_filename, input_path, result) |
| 205 |
| 206 except KeyboardInterrupt: |
| 207 pool.terminate() |
| 208 finally: |
| 209 pool.close() |
| 210 pool.join() |
| 211 else: |
| 212 for test_case in test_cases: |
| 213 input_filename, input_file_dir = test_case |
| 214 result = self.GenerateAndTest(input_filename, input_file_dir) |
| 215 self.HandleResult(input_filename, |
| 216 os.path.join(input_file_dir, input_filename), result) |
| 173 | 217 |
| 174 if self.surprises: | 218 if self.surprises: |
| 175 self.surprises.sort() | 219 self.surprises.sort() |
| 176 print '\n\nUnexpected Successes:' | 220 print '\n\nUnexpected Successes:' |
| 177 for surprise in self.surprises: | 221 for surprise in self.surprises: |
| 178 print surprise; | 222 print surprise; |
| 179 | 223 |
| 180 if self.failures: | 224 if self.failures: |
| 181 self.failures.sort() | 225 self.failures.sort() |
| 182 print '\n\nSummary of Failures:' | 226 print '\n\nSummary of Failures:' |
| 183 for failure in self.failures: | 227 for failure in self.failures: |
| 184 print failure | 228 print failure |
| 185 return 1 | 229 return 1 |
| 186 | 230 |
| 187 return 0 | 231 return 0 |
| OLD | NEW |