Remove existing generated images before running a test.
authorLei Zhang <thestig@chromium.org>
Wed, 9 Sep 2015 19:49:19 +0000 (12:49 -0700)
committerLei Zhang <thestig@chromium.org>
Wed, 9 Sep 2015 19:49:19 +0000 (12:49 -0700)
R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/1314443007 .

testing/tools/pngdiffer.py
testing/tools/run_corpus_tests.py
testing/tools/run_pixel_tests.py

index 7a7e8dd..35eaaa4 100755 (executable)
@@ -16,14 +16,33 @@ class PNGDiffer():
     self.pdfium_diff_path = finder.ExecutablePath('pdfium_diff')
     self.os_name = finder.os_name
 
+  def GetActualFiles(self, input_filename, source_dir, working_dir):
+    actual_paths = []
+    template_paths = self._GetTemplatePaths(
+        input_filename, source_dir, working_dir)
+    actual_path_template = template_paths[0];
+    expected_path_template = template_paths[1]
+    platform_expected_path_template = template_paths[2]
+    i = 0
+    while True:
+      actual_path = actual_path_template % i
+      expected_path = expected_path_template % i
+      platform_expected_path = (
+          platform_expected_path_template % (self.os_name, i))
+      if os.path.exists(platform_expected_path):
+        expected_path = platform_expected_path
+      elif not os.path.exists(expected_path):
+        break
+      actual_paths.append(actual_path)
+      i += 1
+    return actual_paths
+
   def HasDifferences(self, input_filename, source_dir, working_dir):
-    input_root, _ = os.path.splitext(input_filename)
-    actual_path_template = os.path.join(
-        working_dir, input_root + self.ACTUAL_TEMPLATE)
-    expected_path_template = os.path.join(
-        source_dir, input_root + self.EXPECTED_TEMPLATE)
-    platform_expected_path_template = os.path.join(
-        source_dir, input_root + self.PLATFORM_EXPECTED_TEMPLATE)
+    template_paths = self._GetTemplatePaths(
+        input_filename, source_dir, working_dir)
+    actual_path_template = template_paths[0];
+    expected_path_template = template_paths[1]
+    platform_expected_path_template = template_paths[2]
     i = 0
     try:
       while True:
@@ -46,3 +65,12 @@ class PNGDiffer():
       print "FAILURE: " + input_filename + "; " + str(e)
       return True
     return False
+
+  def _GetTemplatePaths(self, input_filename, source_dir, working_dir):
+    input_root, _ = os.path.splitext(input_filename)
+    actual_path = os.path.join(working_dir, input_root + self.ACTUAL_TEMPLATE)
+    expected_path = os.path.join(
+        source_dir, input_root + self.EXPECTED_TEMPLATE)
+    platform_expected_path = os.path.join(
+        source_dir, input_root + self.PLATFORM_EXPECTED_TEMPLATE)
+    return (actual_path, expected_path, platform_expected_path)
index 28616fb..0dadd52 100755 (executable)
@@ -24,6 +24,14 @@ def test_one_file(input_filename, source_dir, working_dir,
                   pdfium_test_path, image_differ):
   input_path = os.path.join(source_dir, input_filename)
   pdf_path = os.path.join(working_dir, input_filename)
+
+  # Remove any existing generated images from previous runs.
+  actual_images = image_differ.GetActualFiles(
+      input_filename, source_dir, working_dir)
+  for image in actual_images:
+    if os.path.exists(image):
+      os.remove(image)
+
   try:
     shutil.copyfile(input_path, pdf_path)
     sys.stdout.flush()
@@ -35,6 +43,7 @@ def test_one_file(input_filename, source_dir, working_dir,
     return False
   return True
 
+
 def main():
   parser = optparse.OptionParser()
   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
index 98e8916..c65a67a 100755 (executable)
@@ -24,6 +24,14 @@ def generate_and_test(input_filename, source_dir, working_dir,
   input_root, _ = os.path.splitext(input_filename)
   input_path = os.path.join(source_dir, input_root + '.in')
   pdf_path = os.path.join(working_dir, input_root + '.pdf')
+
+  # Remove any existing generated images from previous runs.
+  actual_images = image_differ.GetActualFiles(
+      input_filename, source_dir, working_dir)
+  for image in actual_images:
+    if os.path.exists(image):
+      os.remove(image)
+
   try:
     sys.stdout.flush()
     subprocess.check_call(
@@ -37,6 +45,7 @@ def generate_and_test(input_filename, source_dir, working_dir,
     return False
   return True
 
+
 def main():
   parser = optparse.OptionParser()
   parser.add_option('--build-dir', default=os.path.join('out', 'Debug'),
@@ -77,5 +86,6 @@ def main():
     return 1
   return 0
 
+
 if __name__ == '__main__':
   sys.exit(main())