Add delete_output_on_success option to test_runner.py.

Running tests on larger corpuses produce lots of output, most of which
is uninteresting and not worth keeping. Add an option to delete them
right away to reduce disk space usage.

Change-Id: I79b776a79614aad9b7935f1f720071743ab789cd
Reviewed-on: https://pdfium-review.googlesource.com/c/46552
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/testing/tools/test_runner.py b/testing/tools/test_runner.py
index 7dea242..c8b217d 100644
--- a/testing/tools/test_runner.py
+++ b/testing/tools/test_runner.py
@@ -40,6 +40,13 @@
     raise KeyboardInterruptError()
 
 
+def DeleteFiles(files):
+  """Utility function to delete a list of files"""
+  for f in files:
+    if os.path.exists(f):
+      os.remove(f)
+
+
 class TestRunner:
   def __init__(self, dirname):
     # Currently the only used directories are corpus, javascript, and pixel,
@@ -48,6 +55,7 @@
     # an argument for the type will need to be added.
     self.test_dir = dirname
     self.test_type = dirname
+    self.delete_output_on_success = False
     self.enforce_expected_images = False
     self.oneshot_renderer = False
 
@@ -66,9 +74,7 @@
     # Remove any existing generated images from previous runs.
     actual_images = self.image_differ.GetActualFiles(input_filename, source_dir,
                                                      self.working_dir)
-    for image in actual_images:
-      if os.path.exists(image):
-        os.remove(image)
+    DeleteFiles(actual_images)
 
     sys.stdout.flush()
 
@@ -101,6 +107,8 @@
         print 'FAILURE: %s; Missing expected images' % input_filename
         return False, results
 
+    if self.delete_output_on_success:
+      DeleteFiles(actual_images)
     return True, results
 
   def RegenerateIfNeeded_(self, input_filename, source_dir):
@@ -366,6 +374,10 @@
     print
     print 'Test cases not executed: %d' % len(self.execution_suppressed_cases)
 
+  def SetDeleteOutputOnSuccess(self, new_value):
+    """Set whether to delete generated output if the test passes."""
+    self.delete_output_on_success = new_value
+
   def SetEnforceExpectedImages(self, new_value):
     """Set whether to enforce that each test case provide an expected image."""
     self.enforce_expected_images = new_value