| 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 optparse | 6 import optparse |
| 7 import os | 7 import os |
| 8 import re | 8 import re |
| 9 import subprocess | 9 import subprocess |
| 10 import sys | 10 import sys |
| 11 | 11 |
| 12 import common |
| 13 import pngdiffer |
| 14 import suppressor |
| 15 |
| 12 # Nomenclature: | 16 # Nomenclature: |
| 13 # x_root - "x" | 17 # x_root - "x" |
| 14 # x_filename - "x.ext" | 18 # x_filename - "x.ext" |
| 15 # x_path - "path/to/a/b/c/x.ext" | 19 # x_path - "path/to/a/b/c/x.ext" |
| 16 # c_dir - "path/to/a/b/c" | 20 # c_dir - "path/to/a/b/c" |
| 17 | 21 |
| 18 def extract_suppressions(filename): | |
| 19 with open(filename) as f: | |
| 20 suppressions = [y for y in [ | |
| 21 x.split('#')[0].strip() for x in f.readlines()] if y] | |
| 22 return suppressions | |
| 23 | |
| 24 def generate_and_test(input_filename, source_dir, working_dir, | 22 def generate_and_test(input_filename, source_dir, working_dir, |
| 25 fixup_path, pdfium_test_path, pdfium_diff_path): | 23 fixup_path, pdfium_test_path, image_differ): |
| 26 input_root, _ = os.path.splitext(input_filename) | 24 input_root, _ = os.path.splitext(input_filename) |
| 27 input_path = os.path.join(source_dir, input_root + '.in') | 25 input_path = os.path.join(source_dir, input_root + '.in') |
| 28 pdf_path = os.path.join(working_dir, input_root + '.pdf') | 26 pdf_path = os.path.join(working_dir, input_root + '.pdf') |
| 29 actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.png') | |
| 30 expected_path_template = os.path.join(source_dir, | |
| 31 input_root + '_expected.pdf.%d.png') | |
| 32 try: | 27 try: |
| 33 sys.stdout.flush() | 28 sys.stdout.flush() |
| 34 subprocess.check_call( | 29 subprocess.check_call( |
| 35 [sys.executable, fixup_path, '--output-dir=' + working_dir, input_path]) | 30 [sys.executable, fixup_path, '--output-dir=' + working_dir, input_path]) |
| 36 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) | 31 subprocess.check_call([pdfium_test_path, '--png', pdf_path]) |
| 37 i = 0; | |
| 38 while True: | |
| 39 expected_path = expected_path_template % i; | |
| 40 actual_path = actual_path_template % i; | |
| 41 if not os.path.exists(expected_path): | |
| 42 if i == 0: | |
| 43 print "WARNING: no expected results files found for " + input_filename | |
| 44 break | |
| 45 print "Checking " + actual_path | |
| 46 sys.stdout.flush() | |
| 47 subprocess.check_call([pdfium_diff_path, expected_path, actual_path]) | |
| 48 i += 1 | |
| 49 except subprocess.CalledProcessError as e: | 32 except subprocess.CalledProcessError as e: |
| 50 print "FAILURE: " + input_filename + "; " + str(e) | 33 print "FAILURE: " + input_filename + "; " + str(e) |
| 51 return False | 34 return False |
| 35 if image_differ.HasDifferences(input_filename, source_dir, working_dir): |
| 36 print "FAILURE: " + input_filename |
| 37 return False |
| 52 return True | 38 return True |
| 53 | 39 |
| 54 def main(): | 40 def main(): |
| 55 if sys.platform.startswith('linux'): | |
| 56 os_name = 'linux' | |
| 57 elif sys.platform.startswith('win'): | |
| 58 os_name = 'win' | |
| 59 elif sys.platform.startswith('darwin'): | |
| 60 os_name = 'mac' | |
| 61 else: | |
| 62 print 'Confused, can not determine OS, aborting.' | |
| 63 return 1 | |
| 64 | |
| 65 parser = optparse.OptionParser() | 41 parser = optparse.OptionParser() |
| 66 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 42 parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |
| 67 help='relative path from the base source directory') | 43 help='relative path from the base source directory') |
| 68 options, args = parser.parse_args() | 44 options, args = parser.parse_args() |
| 69 | 45 finder = common.DirectoryFinder(options.build_dir) |
| 70 # Expect |my_dir| to be .../pdfium/testing/tools. | 46 fixup_path = finder.ScriptPath('fixup_pdf_template.py') |
| 71 my_dir = os.path.dirname(os.path.realpath(__file__)) | 47 source_dir = finder.TestingDir(os.path.join('resources', 'pixel')) |
| 72 testing_dir = os.path.dirname(my_dir) | 48 pdfium_test_path = finder.ExecutablePath('pdfium_test') |
| 73 pdfium_dir = os.path.dirname(testing_dir) | 49 working_dir = finder.WorkingDir(os.path.join('testing', 'pixel')) |
| 74 if (os.path.basename(my_dir) != 'tools' or | |
| 75 os.path.basename(testing_dir) != 'testing'): | |
| 76 print 'Confused, can not find pdfium root directory, aborting.' | |
| 77 return 1 | |
| 78 | |
| 79 # Other scripts are found in the same directory as this one. | |
| 80 fixup_path = os.path.join(my_dir, 'fixup_pdf_template.py') | |
| 81 | |
| 82 # test files are in .../pdfium/testing/resources/pixel. | |
| 83 source_dir = os.path.join(testing_dir, 'resources', 'pixel') | |
| 84 | |
| 85 # Find path to build directory. This depends on whether this is a | |
| 86 # standalone build vs. a build as part of a chromium checkout. For | |
| 87 # standalone, we expect a path like .../pdfium/out/Debug, but for | |
| 88 # chromium, we expect a path like .../src/out/Debug two levels | |
| 89 # higher (to skip over the third_party/pdfium path component under | |
| 90 # which chromium sticks pdfium). | |
| 91 base_dir = pdfium_dir | |
| 92 one_up_dir = os.path.dirname(base_dir) | |
| 93 two_up_dir = os.path.dirname(one_up_dir) | |
| 94 if (os.path.basename(two_up_dir) == 'src' and | |
| 95 os.path.basename(one_up_dir) == 'third_party'): | |
| 96 base_dir = two_up_dir | |
| 97 build_dir = os.path.join(base_dir, options.build_dir) | |
| 98 | |
| 99 # Compiled binaries are found under the build path. | |
| 100 pdfium_test_path = os.path.join(build_dir, 'pdfium_test') | |
| 101 pdfium_diff_path = os.path.join(build_dir, 'pdfium_diff') | |
| 102 if sys.platform.startswith('win'): | |
| 103 pdfium_test_path = pdfium_test_path + '.exe' | |
| 104 pdfium_diff_path = pdfium_diff_path + '.exe' | |
| 105 # TODO(tsepez): Mac may require special handling here. | |
| 106 | |
| 107 # Place generated files under the build directory, not source directory. | |
| 108 gen_dir = os.path.join(build_dir, 'gen', 'pdfium') | |
| 109 working_dir = os.path.join(gen_dir, 'testing', 'pixel') | |
| 110 if not os.path.exists(working_dir): | 50 if not os.path.exists(working_dir): |
| 111 os.makedirs(working_dir) | 51 os.makedirs(working_dir) |
| 112 | 52 |
| 113 suppression_list = extract_suppressions( | 53 test_suppressor = suppressor.Suppressor(finder) |
| 114 os.path.join(testing_dir, 'SUPPRESSIONS')) | 54 image_differ = pngdiffer.PNGDiffer(finder) |
| 115 | |
| 116 platform_suppression_filename = 'SUPPRESSIONS_%s' % os_name | |
| 117 platform_suppression_list = extract_suppressions( | |
| 118 os.path.join(testing_dir, platform_suppression_filename)) | |
| 119 | 55 |
| 120 failures = [] | 56 failures = [] |
| 121 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$') | 57 input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$') |
| 122 for input_filename in os.listdir(source_dir): | 58 for input_filename in os.listdir(source_dir): |
| 123 if input_file_re.match(input_filename): | 59 if input_file_re.match(input_filename): |
| 124 input_path = os.path.join(source_dir, input_filename) | 60 input_path = os.path.join(source_dir, input_filename) |
| 125 if os.path.isfile(input_path): | 61 if os.path.isfile(input_path): |
| 126 if input_filename in suppression_list: | 62 if test_suppressor.IsSuppressed(input_filename): |
| 127 print "Not running %s, found in SUPPRESSIONS file" % input_filename | |
| 128 continue | |
| 129 if input_filename in platform_suppression_list: | |
| 130 print ("Not running %s, found in %s file" % | |
| 131 (input_filename, platform_suppression_filename)) | |
| 132 continue | 63 continue |
| 133 if not generate_and_test(input_filename, source_dir, working_dir, | 64 if not generate_and_test(input_filename, source_dir, working_dir, |
| 134 fixup_path, pdfium_test_path, pdfium_diff_path)
: | 65 fixup_path, pdfium_test_path, image_differ): |
| 135 failures.append(input_path) | 66 failures.append(input_path) |
| 136 | 67 |
| 137 if failures: | 68 if failures: |
| 138 print '\n\nSummary of Failures:' | 69 print '\n\nSummary of Failures:' |
| 139 for failure in failures: | 70 for failure in failures: |
| 140 print failure | 71 print failure |
| 141 return 1 | 72 return 1 |
| 142 return 0 | 73 return 0 |
| 143 | 74 |
| 144 if __name__ == '__main__': | 75 if __name__ == '__main__': |
| 145 sys.exit(main()) | 76 sys.exit(main()) |
| OLD | NEW |