Chromium Code Reviews| 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, input_filename[:-4]) | |
|
Lei Zhang
2015/11/21 20:21:59
Use os.path.splitext(input_filename)
zhaoqin
2015/11/23 21:47:19
Done.
| |
| 45 cmd_to_run.extend([pdfium_test_path, '--png', pdf_path]) | |
| 46 # run test | |
| 47 error = common.RunCommand(cmd_to_run, redirect_output) | |
| 44 if error: | 48 if error: |
| 45 print "FAILURE: " + input_filename + "; " + str(error) | 49 print "FAILURE: " + input_filename + "; " + str(error) |
| 46 return False | 50 return False |
| 47 return not image_differ.HasDifferences(input_filename, source_dir, | 51 return not image_differ.HasDifferences(input_filename, source_dir, |
| 48 working_dir, redirect_output) | 52 working_dir, redirect_output) |
| 49 | 53 |
| 50 | 54 |
| 51 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, | 55 def test_one_file_parallel(working_dir, pdfium_test_path, image_differ, |
| 52 test_case): | 56 test_case): |
| 53 """Wrapper function to call test_one_file() and redirect output to stdout.""" | 57 """Wrapper function to call test_one_file() and redirect output to stdout.""" |
| 54 try: | 58 try: |
| 55 old_stdout = sys.stdout | 59 old_stdout = sys.stdout |
| 56 old_stderr = sys.stderr | 60 old_stderr = sys.stderr |
| 57 sys.stdout = cStringIO.StringIO() | 61 sys.stdout = cStringIO.StringIO() |
| 58 sys.stderr = sys.stdout | 62 sys.stderr = sys.stdout |
| 59 input_filename, source_dir = test_case | 63 input_filename, source_dir = test_case |
| 60 result = test_one_file(input_filename, source_dir, working_dir, | 64 result = test_one_file(input_filename, source_dir, working_dir, |
| 61 pdfium_test_path, image_differ, True); | 65 pdfium_test_path, image_differ, "", True); |
| 62 output = sys.stdout | 66 output = sys.stdout |
| 63 sys.stdout = old_stdout | 67 sys.stdout = old_stdout |
| 64 sys.stderr = old_stderr | 68 sys.stderr = old_stderr |
| 65 return (result, output.getvalue(), input_filename, source_dir) | 69 return (result, output.getvalue(), input_filename, source_dir) |
| 66 except KeyboardInterrupt: | 70 except KeyboardInterrupt: |
| 67 raise KeyboardInterruptError() | 71 raise KeyboardInterruptError() |
| 68 | 72 |
| 69 | 73 |
| 70 def handle_result(test_suppressor, input_filename, input_path, result, | 74 def handle_result(test_suppressor, input_filename, input_path, result, |
| 71 surprises, failures): | 75 surprises, failures): |
| 72 if test_suppressor.IsSuppressed(input_filename): | 76 if test_suppressor.IsSuppressed(input_filename): |
| 73 if result: | 77 if result: |
| 74 surprises.append(input_path) | 78 surprises.append(input_path) |
| 75 else: | 79 else: |
| 76 if not result: | 80 if not result: |
| 77 failures.append(input_path) | 81 failures.append(input_path) |
| 78 | 82 |
| 79 | 83 |
| 80 def main(): | 84 def main(): |
| 81 parser = optparse.OptionParser() | 85 parser = optparse.OptionParser() |
| 82 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 86 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 83 help='relative path from the base source directory') | 87 help='relative path from the base source directory') |
| 84 parser.add_option('-j', default=multiprocessing.cpu_count(), | 88 parser.add_option('-j', default=multiprocessing.cpu_count(), |
| 85 dest='num_workers', type='int', | 89 dest='num_workers', type='int', |
| 86 help='run NUM_WORKERS jobs in parallel') | 90 help='run NUM_WORKERS jobs in parallel') |
| 91 parser.add_option('--wrapper', default='', dest="wrapper", | |
| 92 help='Dr. Memory wrapper for running test under Dr. Memory') | |
| 87 options, args = parser.parse_args() | 93 options, args = parser.parse_args() |
| 88 finder = common.DirectoryFinder(options.build_dir) | 94 finder = common.DirectoryFinder(options.build_dir) |
| 89 pdfium_test_path = finder.ExecutablePath('pdfium_test') | 95 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 90 if not os.path.exists(pdfium_test_path): | 96 if not os.path.exists(pdfium_test_path): |
| 91 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path | 97 print "FAILURE: Can't find test executable '%s'" % pdfium_test_path |
| 92 print "Use --build-dir to specify its location." | 98 print "Use --build-dir to specify its location." |
| 93 return 1 | 99 return 1 |
| 94 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) | 100 working_dir = finder.WorkingDir(os.path.join('testing', 'corpus')) |
| 95 if not os.path.exists(working_dir): | 101 if not os.path.exists(working_dir): |
| 96 os.makedirs(working_dir) | 102 os.makedirs(working_dir) |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 136 surprises, failures) | 142 surprises, failures) |
| 137 pool.close() | 143 pool.close() |
| 138 except KeyboardInterrupt: | 144 except KeyboardInterrupt: |
| 139 pool.terminate() | 145 pool.terminate() |
| 140 finally: | 146 finally: |
| 141 pool.join() | 147 pool.join() |
| 142 else: | 148 else: |
| 143 for test_case in test_cases: | 149 for test_case in test_cases: |
| 144 input_filename, source_dir = test_case | 150 input_filename, source_dir = test_case |
| 145 result = test_one_file(input_filename, source_dir, working_dir, | 151 result = test_one_file(input_filename, source_dir, working_dir, |
| 146 pdfium_test_path, image_differ) | 152 pdfium_test_path, image_differ, |
| 153 options.wrapper) | |
| 147 handle_result(test_suppressor, input_filename, input_path, result, | 154 handle_result(test_suppressor, input_filename, input_path, result, |
| 148 surprises, failures) | 155 surprises, failures) |
| 149 | 156 |
| 150 if surprises: | 157 if surprises: |
| 151 surprises.sort() | 158 surprises.sort() |
| 152 print '\n\nUnexpected Successes:' | 159 print '\n\nUnexpected Successes:' |
| 153 for surprise in surprises: | 160 for surprise in surprises: |
| 154 print surprise; | 161 print surprise; |
| 155 | 162 |
| 156 if failures: | 163 if failures: |
| 157 failures.sort() | 164 failures.sort() |
| 158 print '\n\nSummary of Failures:' | 165 print '\n\nSummary of Failures:' |
| 159 for failure in failures: | 166 for failure in failures: |
| 160 print failure | 167 print failure |
| 161 return 1 | 168 return 1 |
| 162 | 169 |
| 163 return 0 | 170 return 0 |
| 164 | 171 |
| 165 | 172 |
| 166 if __name__ == '__main__': | 173 if __name__ == '__main__': |
| 167 sys.exit(main()) | 174 sys.exit(main()) |
| OLD | NEW |