OLD | NEW |
(Empty) | |
| 1 #!/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 |
| 6 import optparse |
| 7 import os |
| 8 import re |
| 9 import subprocess |
| 10 import sys |
| 11 |
| 12 import common |
| 13 import pngdiffer |
| 14 import suppressor |
| 15 |
| 16 # Nomenclature: |
| 17 # x_root - "x" |
| 18 # x_filename - "x.ext" |
| 19 # x_path - "path/to/a/b/c/x.ext" |
| 20 # c_dir - "path/to/a/b/c" |
| 21 |
| 22 class TestRunner: |
| 23 def __init__(self, dirname): |
| 24 self.test_dir = dirname |
| 25 |
| 26 def GenerateAndTest(self, input_filename, source_dir): |
| 27 input_root, _ = os.path.splitext(input_filename) |
| 28 expected_txt_path = os.path.join(source_dir, input_root + '_expected.txt') |
| 29 |
| 30 pdf_path = os.path.join(self.working_dir, input_root + '.pdf') |
| 31 |
| 32 # Remove any existing generated images from previous runs. |
| 33 actual_images = self.image_differ.GetActualFiles(input_filename, source_dir, |
| 34 self.working_dir) |
| 35 for image in actual_images: |
| 36 if os.path.exists(image): |
| 37 os.remove(image) |
| 38 |
| 39 sys.stdout.flush() |
| 40 |
| 41 raised_exception = self.Generate(source_dir, input_filename, input_root, |
| 42 pdf_path) |
| 43 |
| 44 if raised_exception != None: |
| 45 print "FAILURE: " + input_filename + "; " + str(raised_exception) |
| 46 return False |
| 47 |
| 48 if os.path.exists(expected_txt_path): |
| 49 raised_exception = self.TestText(input_root, expected_txt_path, pdf_path) |
| 50 else: |
| 51 raised_exception = self.TestPixel(input_root, pdf_path) |
| 52 |
| 53 if raised_exception != None: |
| 54 print "FAILURE: " + input_filename + "; " + str(raised_exception) |
| 55 return False |
| 56 |
| 57 if len(actual_images): |
| 58 if self.image_differ.HasDifferences(input_filename, source_dir, |
| 59 self.working_dir): |
| 60 return False |
| 61 |
| 62 return True |
| 63 |
| 64 def Generate(self, source_dir, input_filename, input_root, pdf_path): |
| 65 original_path = os.path.join(source_dir, input_filename) |
| 66 input_path = os.path.join(source_dir, input_root + '.in') |
| 67 |
| 68 if not os.path.exists(input_path): |
| 69 if os.path.exists(original_path): |
| 70 shutil.copyfile(original_path, pdf_path) |
| 71 return None |
| 72 |
| 73 sys.stdout.flush() |
| 74 return common.RunCommand( |
| 75 [sys.executable, self.fixup_path, '--output-dir=' + self.working_dir, |
| 76 input_path]) |
| 77 |
| 78 |
| 79 def TestText(self, input_root, expected_txt_path, pdf_path): |
| 80 txt_path = os.path.join(self.working_dir, input_root + '.txt') |
| 81 |
| 82 with open(txt_path, 'w') as outfile: |
| 83 # add Dr. Memory wrapper if exist |
| 84 cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
| 85 cmd_to_run.extend([self.pdfium_test_path, pdf_path]) |
| 86 subprocess.check_call(cmd_to_run, stdout=outfile) |
| 87 |
| 88 cmd = [sys.executable, self.text_diff_path, expected_txt_path, txt_path] |
| 89 return common.RunCommand(cmd) |
| 90 |
| 91 |
| 92 def TestPixel(self, input_root, pdf_path): |
| 93 cmd_to_run = common.DrMemoryWrapper(self.drmem_wrapper, input_root) |
| 94 cmd_to_run.extend([self.pdfium_test_path, '--png', pdf_path]) |
| 95 return common.RunCommand(cmd_to_run) |
| 96 |
| 97 |
| 98 def HandleResult(self, input_filename, input_path, result): |
| 99 if self.test_suppressor.IsResultSuppressed(input_filename): |
| 100 if result: |
| 101 self.surprises.append(input_path) |
| 102 else: |
| 103 if not result: |
| 104 self.failures.append(input_path) |
| 105 |
| 106 |
| 107 def Run(self): |
| 108 parser = optparse.OptionParser() |
| 109 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 110 help='relative path from the base source directory') |
| 111 parser.add_option('-j', default=1, |
| 112 dest='num_workers', type='int', |
| 113 help='run NUM_WORKERS jobs in parallel') |
| 114 parser.add_option('--wrapper', default='', dest="wrapper", |
| 115 help='wrapper for running test under Dr. Memory') |
| 116 options, args = parser.parse_args() |
| 117 |
| 118 finder = common.DirectoryFinder(options.build_dir) |
| 119 self.fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 120 self.text_diff_path = finder.ScriptPath('text_diff.py') |
| 121 |
| 122 self.drmem_wrapper = options.wrapper |
| 123 |
| 124 self.source_dir = finder.TestingDir() |
| 125 self.pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 126 if not os.path.exists(self.pdfium_test_path): |
| 127 print "FAILURE: Can't find test executable '%s'" % self.pdfium_test_path |
| 128 print "Use --build-dir to specify its location." |
| 129 return 1 |
| 130 |
| 131 self.working_dir = finder.WorkingDir(os.path.join('testing', self.test_dir)) |
| 132 if not os.path.exists(self.working_dir): |
| 133 os.makedirs(self.working_dir) |
| 134 |
| 135 self.feature_string = subprocess.check_output([self.pdfium_test_path, |
| 136 '--show-config']) |
| 137 self.test_suppressor = suppressor.Suppressor(finder, self.feature_string) |
| 138 self.image_differ = pngdiffer.PNGDiffer(finder) |
| 139 |
| 140 test_dir = finder.TestingDir(os.path.join('resources', self.test_dir)) |
| 141 walk_from_dir = finder.TestingDir(test_dir); |
| 142 |
| 143 test_cases = [] |
| 144 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.](in|pdf)$') |
| 145 if len(args): |
| 146 for file_name in args: |
| 147 file_name.replace(".pdf", ".in") |
| 148 input_path = os.path.join(walk_from_dir, file_name) |
| 149 if not os.path.isfile(input_path): |
| 150 print "Can't find test file '%s'" % file_name |
| 151 return 1 |
| 152 |
| 153 test_cases.append((os.path.basename(input_path), |
| 154 os.path.dirname(input_path))) |
| 155 else: |
| 156 for file_dir, _, filename_list in os.walk(walk_from_dir): |
| 157 for input_filename in filename_list: |
| 158 if input_file_re.match(input_filename): |
| 159 input_path = os.path.join(file_dir, input_filename) |
| 160 if not self.test_suppressor.IsExecutionSuppressed(input_path): |
| 161 if os.path.isfile(input_path): |
| 162 test_cases.append((input_filename, file_dir)) |
| 163 |
| 164 self.failures = [] |
| 165 self.surprises = [] |
| 166 |
| 167 for test_case in test_cases: |
| 168 input_filename, input_file_dir = test_case |
| 169 result = self.GenerateAndTest(input_filename, input_file_dir) |
| 170 self.HandleResult(input_filename, |
| 171 os.path.join(input_file_dir, input_filename), result) |
| 172 |
| 173 if self.surprises: |
| 174 self.surprises.sort() |
| 175 print '\n\nUnexpected Successes:' |
| 176 for surprise in self.surprises: |
| 177 print surprise; |
| 178 |
| 179 if self.failures: |
| 180 self.failures.sort() |
| 181 print '\n\nSummary of Failures:' |
| 182 for failure in self.failures: |
| 183 print failure |
| 184 return 1 |
| 185 |
| 186 return 0 |
OLD | NEW |