| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The PDFium Authors. All rights reserved. | 2 # Copyright 2015 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 | 6 import cStringIO |
| 7 import functools | 7 import functools |
| 8 import multiprocessing | 8 import multiprocessing |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import re | 11 import re |
| 12 import shutil | 12 import shutil |
| 13 import subprocess | 13 import subprocess |
| 14 import sys | 14 import sys |
| 15 | 15 |
| 16 import common | 16 import common |
| 17 import pngdiffer | 17 import pngdiffer |
| 18 import suppressor | 18 import suppressor |
| 19 | 19 |
| 20 class KeyboardInterruptError(Exception): pass | 20 class KeyboardInterruptError(Exception): pass |
| 21 | 21 |
| 22 # Nomenclature: | 22 # Nomenclature: |
| 23 # x_root - "x" | 23 # x_root - "x" |
| 24 # x_filename - "x.ext" | 24 # x_filename - "x.ext" |
| 25 # x_path - "path/to/a/b/c/x.ext" | 25 # x_path - "path/to/a/b/c/x.ext" |
| 26 # c_dir - "path/to/a/b/c" | 26 # c_dir - "path/to/a/b/c" |
| 27 | 27 |
| 28 def test_one_file(input_filename, source_dir, working_dir, | 28 def test_one_file(input_filename, source_dir, working_dir, |
| 29 pdfium_test_path, image_differ, redirect_output=False): | 29 pdfium_test_path, image_differ, drmem_wrapper, |
| 30 redirect_output=False): |
| 30 input_path = os.path.join(source_dir, input_filename) | 31 input_path = os.path.join(source_dir, input_filename) |
| 31 pdf_path = os.path.join(working_dir, input_filename) | 32 pdf_path = os.path.join(working_dir, input_filename) |
| 32 | |
| 33 # Remove any existing generated images from previous runs. | 33 # Remove any existing generated images from previous runs. |
| 34 actual_images = image_differ.GetActualFiles( | 34 actual_images = image_differ.GetActualFiles( |
| 35 input_filename, source_dir, working_dir) | 35 input_filename, source_dir, working_dir) |
| 36 for image in actual_images: | 36 for image in actual_images: |
| 37 if os.path.exists(image): | 37 if os.path.exists(image): |
| 38 os.remove(image) | 38 os.remove(image) |
| 39 | 39 |
| 40 shutil.copyfile(input_path, pdf_path) | 40 shutil.copyfile(input_path, pdf_path) |
| 41 sys.stdout.flush() | 41 sys.stdout.flush() |
| 42 error = common.RunCommand([pdfium_test_path, '--png', pdf_path], | 42 # add Dr. Memory wrapper if exist |
| 43 redirect_output) | 43 # remove .pdf suffix |
| 44 cmd_to_run = common.DrMemoryWrapper(drmem_wrapper, |
| 45 os.path.splitext(input_filename)[0]) |
| 46 cmd_to_run.extend([pdfium_test_path, '--png', pdf_path]) |
| 47 # run test |
| 48 error = common.RunCommand(cmd_to_run, redirect_output) |
| 44 if error: | 49 if error: |
| 45 print "FAILURE: " + input_filename + "; " + str(error) | 50 print "FAILURE: " + input_filename + "; " + str(error) |
| 46 return False | 51 return False |
| 47 return not image_differ.HasDifferences(input_filename, source_dir, | 52 return not image_differ.HasDifferences(input_filename, source_dir, |
| 48 working_dir, redirect_output) | 53 working_dir, redirect_output) |
| 49 | 54 |
| 50 | 55 |
| 51 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, | 56 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, |
| 52 test_case): | 57 test_case): |
| 53 """Wrapper function to call test_one_file() and redirect output to stdout.""" | 58 """Wrapper function to call test_one_file() and redirect output to stdout.""" |
| 54 try: | 59 try: |
| 55 old_stdout = sys.stdout | 60 old_stdout = sys.stdout |
| 56 old_stderr = sys.stderr | 61 old_stderr = sys.stderr |
| 57 sys.stdout = cStringIO.StringIO() | 62 sys.stdout = cStringIO.StringIO() |
| 58 sys.stderr = sys.stdout | 63 sys.stderr = sys.stdout |
| 59 input_filename, source_dir = test_case | 64 input_filename, source_dir = test_case |
| 60 result = test_one_file(input_filename, source_dir, working_dir, | 65 result = test_one_file(input_filename, source_dir, working_dir, |
| 61 pdfium_test_path, image_differ, True); | 66 pdfium_test_path, image_differ, "", True); |
| 62 output = sys.stdout | 67 output = sys.stdout |
| 63 sys.stdout = old_stdout | 68 sys.stdout = old_stdout |
| 64 sys.stderr = old_stderr | 69 sys.stderr = old_stderr |
| 65 return (result, output.getvalue(), input_filename, source_dir) | 70 return (result, output.getvalue(), input_filename, source_dir) |
| 66 except KeyboardInterrupt: | 71 except KeyboardInterrupt: |
| 67 raise KeyboardInterruptError() | 72 raise KeyboardInterruptError() |
| 68 | 73 |
| 69 | 74 |
| 70 def handle_result(test_suppressor, input_filename, input_path, result, | 75 def handle_result(test_suppressor, input_filename, input_path, result, |
| 71 surprises, failures): | 76 surprises, failures): |
| 72 if test_suppressor.IsSuppressed(input_filename): | 77 if test_suppressor.IsSuppressed(input_filename): |
| 73 if result: | 78 if result: |
| 74 surprises.append(input_path) | 79 surprises.append(input_path) |
| 75 else: | 80 else: |
| 76 if not result: | 81 if not result: |
| 77 failures.append(input_path) | 82 failures.append(input_path) |
| 78 | 83 |
| 79 | 84 |
| 80 def main(): | 85 def main(): |
| 81 parser = optparse.OptionParser() | 86 parser = optparse.OptionParser() |
| 82 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 87 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 83 help='relative path from the base source directory') | 88 help='relative path from the base source directory') |
| 84 parser.add_option('-j', default=multiprocessing.cpu_count(), | 89 parser.add_option('-j', default=multiprocessing.cpu_count(), |
| 85 dest='num_workers', type='int', | 90 dest='num_workers', type='int', |
| 86 help='run NUM_WORKERS jobs in parallel') | 91 help='run NUM_WORKERS jobs in parallel') |
| 92 parser.add_option('--wrapper', default='', dest="wrapper", |
| 93 help='Dr. Memory wrapper for running test under Dr. Memory') |
| 87 options, args = parser.parse_args() | 94 options, args = parser.parse_args() |
| 88 finder = common.DirectoryFinder(options.build_dir) | 95 finder = common.DirectoryFinder(options.build_dir) |
| 89 pdfium_test_path = finder.ExecutablePath('pdfium_test') | 96 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 90 if not os.path.exists(pdfium_test_path): | 97 if not os.path.exists(pdfium_test_path): |
| 91 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path | 98 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path |
| 92 print "Use --build-dir to specify its location." | 99 print "Use --build-dir to specify its location." |
| 93 return 1 | 100 return 1 |
| 94 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) | 101 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
| 95 if not os.path.exists(working_dir): | 102 if not os.path.exists(working_dir): |
| 96 os.makedirs(working_dir) | 103 os.makedirs(working_dir) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 surprises, failures) | 143 surprises, failures) |
| 137 pool.close() | 144 pool.close() |
| 138 except KeyboardInterrupt: | 145 except KeyboardInterrupt: |
| 139 pool.terminate() | 146 pool.terminate() |
| 140 finally: | 147 finally: |
| 141 pool.join() | 148 pool.join() |
| 142 else: | 149 else: |
| 143 for test_case in test_cases: | 150 for test_case in test_cases: |
| 144 input_filename, source_dir = test_case | 151 input_filename, source_dir = test_case |
| 145 result = test_one_file(input_filename, source_dir, working_dir, | 152 result = test_one_file(input_filename, source_dir, working_dir, |
| 146 pdfium_test_path, image_differ) | 153 pdfium_test_path, image_differ, |
| 154 options.wrapper) |
| 147 handle_result(test_suppressor, input_filename, input_path, result, | 155 handle_result(test_suppressor, input_filename, input_path, result, |
| 148 surprises, failures) | 156 surprises, failures) |
| 149 | 157 |
| 150 if surprises: | 158 if surprises: |
| 151 surprises.sort() | 159 surprises.sort() |
| 152 print '\n\nUnexpected Successes:' | 160 print '\n\nUnexpected Successes:' |
| 153 for surprise in surprises: | 161 for surprise in surprises: |
| 154 print surprise; | 162 print surprise; |
| 155 | 163 |
| 156 if failures: | 164 if failures: |
| 157 failures.sort() | 165 failures.sort() |
| 158 print '\n\nSummary of Failures:' | 166 print '\n\nSummary of Failures:' |
| 159 for failure in failures: | 167 for failure in failures: |
| 160 print failure | 168 print failure |
| 161 return 1 | 169 return 1 |
| 162 | 170 |
| 163 return 0 | 171 return 0 |
| 164 | 172 |
| 165 | 173 |
| 166 if __name__ == '__main__': | 174 if __name__ == '__main__': |
| 167 sys.exit(main()) | 175 sys.exit(main()) |
| OLD | NEW |