| 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 # Nomenclature: |  12 # Nomenclature: | 
|  13 #   x_root - "x" |  13 #   x_root - "x" | 
|  14 #   x_filename - "x.ext" |  14 #   x_filename - "x.ext" | 
|  15 #   x_path - "path/to/a/b/c/x.ext" |  15 #   x_path - "path/to/a/b/c/x.ext" | 
|  16 #   c_dir - "path/to/a/b/c" |  16 #   c_dir - "path/to/a/b/c" | 
|  17  |  17  | 
|  18 def generate_and_test(input_filename, source_dir, working_dir, |  18 def generate_and_test(input_filename, source_dir, working_dir, | 
|  19                       fixup_path, pdfium_test_path): |  19                       fixup_path, pdfium_test_path): | 
|  20   input_root, _ = os.path.splitext(input_filename) |  20   input_root, _ = os.path.splitext(input_filename) | 
|  21   input_path = os.path.join(source_dir, input_root + '.in') |  21   input_path = os.path.join(source_dir, input_root + '.in') | 
|  22   pdf_path = os.path.join(working_dir, input_root + '.pdf') |  22   pdf_path = os.path.join(working_dir, input_root + '.pdf') | 
|  23   txt_path = os.path.join(working_dir, input_root + '.txt') |  23   actual_path_template = os.path.join(working_dir, input_root + '.pdf.%d.ppm') | 
|  24   expected_path = os.path.join(source_dir, input_root + '_expected.txt') |  24   expected_path_template = os.path.join(source_dir, | 
 |  25                                         input_root + '_expected.pdf.%d.ppm') | 
|  25   try: |  26   try: | 
|  26     subprocess.check_call( |  27     subprocess.check_call( | 
|  27         [fixup_path, '--output-dir=' + working_dir, input_path]) |  28         [fixup_path, '--output-dir=' + working_dir, input_path]) | 
|  28     with open(txt_path, 'w') as outfile: |  29     subprocess.check_call([pdfium_test_path, '--ppm', pdf_path]) | 
|  29       subprocess.check_call([pdfium_test_path, pdf_path], stdout=outfile) |  30     i = 0; | 
|  30     subprocess.check_call(['diff', expected_path, txt_path]) |  31     while True: | 
 |  32       expected_path = expected_path_template % i; | 
 |  33       actual_path = actual_path_template % i; | 
 |  34       if not os.path.exists(expected_path): | 
 |  35         break | 
 |  36       subprocess.check_call(['diff', expected_path, actual_path]) | 
 |  37       i += 1 | 
|  31   except subprocess.CalledProcessError as e: |  38   except subprocess.CalledProcessError as e: | 
|  32     print "FAILURE: " + input_filename + "; " + str(e) |  39     print "FAILURE: " + input_filename + "; " + str(e) | 
|  33  |  40  | 
|  34 def main(): |  41 def main(): | 
|  35   parser = optparse.OptionParser() |  42   parser = optparse.OptionParser() | 
|  36   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), |  43   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'), | 
|  37                     help='relative path from the base source directory') |  44                     help='relative path from the base source directory') | 
|  38   options, args = parser.parse_args() |  45   options, args = parser.parse_args() | 
|  39  |  46  | 
|  40   # Expect |my_dir| to be .../pdfium/testing/tools. |  47   # Expect |my_dir| to be .../pdfium/testing/tools. | 
|  41   my_dir = os.path.dirname(os.path.realpath(__file__)) |  48   my_dir = os.path.dirname(os.path.realpath(__file__)) | 
|  42   testing_dir = os.path.dirname(my_dir) |  49   testing_dir = os.path.dirname(my_dir) | 
|  43   pdfium_dir = os.path.dirname(testing_dir) |  50   pdfium_dir = os.path.dirname(testing_dir) | 
|  44   if (os.path.basename(my_dir) != 'tools' or |  51   if (os.path.basename(my_dir) != 'tools' or | 
|  45       os.path.basename(testing_dir) != 'testing'): |  52       os.path.basename(testing_dir) != 'testing'): | 
|  46     print 'Confused, can not find pdfium root directory, aborting.' |  53     print 'Confused, can not find pdfium root directory, aborting.' | 
|  47     return 1 |  54     return 1 | 
|  48  |  55  | 
|  49   # Other scripts are found in the same directory as this one. |  56   # Other scripts are found in the same directory as this one. | 
|  50   fixup_path = os.path.join(my_dir, 'fixup_pdf_template.py') |  57   fixup_path = os.path.join(my_dir, 'fixup_pdf_template.py') | 
|  51  |  58  | 
|  52   # test files are in .../pdfium/testing/resources/javascript. |  59   # test files are in .../pdfium/testing/resources/pixel. | 
|  53   source_dir = os.path.join(testing_dir, 'resources', 'javascript') |  60   source_dir = os.path.join(testing_dir, 'resources', 'pixel') | 
|  54  |  61  | 
|  55   # Find path to build directory.  This depends on whether this is a |  62   # Find path to build directory.  This depends on whether this is a | 
|  56   # standalone build vs. a build as part of a chromium checkout. For |  63   # standalone build vs. a build as part of a chromium checkout. For | 
|  57   # standalone, we expect a path like .../pdfium/out/Debug, but for |  64   # standalone, we expect a path like .../pdfium/out/Debug, but for | 
|  58   # chromium, we expect a path like .../src/out/Debug two levels |  65   # chromium, we expect a path like .../src/out/Debug two levels | 
|  59   # higher (to skip over the third_party/pdfium path component under |  66   # higher (to skip over the third_party/pdfium path component under | 
|  60   # which chromium sticks pdfium). |  67   # which chromium sticks pdfium). | 
|  61   base_dir = pdfium_dir |  68   base_dir = pdfium_dir | 
|  62   one_up_dir = os.path.dirname(base_dir) |  69   one_up_dir = os.path.dirname(base_dir) | 
|  63   two_up_dir = os.path.dirname(one_up_dir) |  70   two_up_dir = os.path.dirname(one_up_dir) | 
|  64   if (os.path.basename(two_up_dir) == 'src' and |  71   if (os.path.basename(two_up_dir) == 'src' and | 
|  65       os.path.basename(one_up_dir) == 'third_party'): |  72       os.path.basename(one_up_dir) == 'third_party'): | 
|  66     base_dir = two_up_dir |  73     base_dir = two_up_dir | 
|  67   build_dir = os.path.join(base_dir, options.build_dir) |  74   build_dir = os.path.join(base_dir, options.build_dir) | 
|  68  |  75  | 
|  69   # Compiled binaries are found under the build path. |  76   # Compiled binaries are found under the build path. | 
|  70   pdfium_test_path = os.path.join(build_dir, 'pdfium_test') |  77   pdfium_test_path = os.path.join(build_dir, 'pdfium_test') | 
|  71   if sys.platform.startswith('win'): |  78   if sys.platform.startswith('win'): | 
|  72     pdfium_test_path = pdfium_test_path + '.exe' |  79     pdfium_test_path = pdfium_test_path + '.exe' | 
|  73   # TODO(tsepez): Mac may require special handling here. |  80   # TODO(tsepez): Mac may require special handling here. | 
|  74  |  81  | 
|  75   # Place generated files under the build directory, not source directory. |  82   # Place generated files under the build directory, not source directory. | 
|  76   gen_dir = os.path.join(build_dir, 'gen', 'pdfium') |  83   gen_dir = os.path.join(build_dir, 'gen', 'pdfium') | 
|  77   working_dir = os.path.join(gen_dir, 'testing', 'javascript') |  84   working_dir = os.path.join(gen_dir, 'testing', 'pixel') | 
|  78   if not os.path.exists(working_dir): |  85   if not os.path.exists(working_dir): | 
|  79     os.makedirs(working_dir) |  86     os.makedirs(working_dir) | 
|  80  |  87  | 
|  81   input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$') |  88   input_file_re = re.compile('^[a-zA-Z0-9_.]+[.]in$') | 
|  82   for input_filename in os.listdir(source_dir): |  89   for input_filename in os.listdir(source_dir): | 
|  83     if input_file_re.match(input_filename): |  90     if input_file_re.match(input_filename): | 
|  84       input_path = os.path.join(source_dir, input_filename) |  91       input_path = os.path.join(source_dir, input_filename) | 
|  85       if os.path.isfile(input_path): |  92       if os.path.isfile(input_path): | 
|  86         generate_and_test(input_filename, source_dir, working_dir, |  93         generate_and_test(input_filename, source_dir, working_dir, | 
|  87                           fixup_path, pdfium_test_path) |  94                           fixup_path, pdfium_test_path) | 
|  88   return 0 |  95   return 0 | 
|  89  |  96  | 
|  90  |  97  | 
|  91 if __name__ == '__main__': |  98 if __name__ == '__main__': | 
|  92   sys.exit(main()) |  99   sys.exit(main()) | 
| OLD | NEW |