Don't archive html files for non failing tests

Bug: 230308492
Change-Id: Idb164f9267474bb6aba2d9444f59b40224acabcf
diff --git a/tools/test.py b/tools/test.py
index 0e3bf0f..55dbac0 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -198,17 +198,40 @@
                     default=False, action='store_true')
   return result.parse_args()
 
+def has_failures(classes_file):
+  with open(classes_file) as f:
+    contents = f.read()
+    # The report has a div tag with the percentage of tests that succeeded.
+    assert '<div class="percent">' in contents
+    return '<div class="percent">100%</div>' not in contents
+
+def should_upload(filename, absolute_filename):
+  # filename is relative to REPO_ROOT/build/reports/tests
+  if filename.startswith('test/packages'):
+    # We don't upload the package overview
+    return False
+  if filename.startswith('test/classes'):
+    return has_failures(absolute_filename)
+  # Always upload index, css and js
+  return True
+
 def archive_failures(options):
   upload_dir = os.path.join(utils.REPO_ROOT, 'build', 'reports', 'tests')
   file_name = options.archive_failures_file_name
-  destination = 'gs://%s/%s' % (BUCKET, file_name)
-  utils.upload_dir_to_cloud_storage(upload_dir, destination, is_html=True)
+  destination_dir = 'gs://%s/%s/' % (BUCKET, file_name)
+  for (dir_path, dir_names, file_names) in os.walk(upload_dir):
+    for f in file_names:
+      absolute_file = os.path.join(dir_path, f)
+      relative_file = absolute_file[len(upload_dir)+1:]
+      if (should_upload(relative_file, absolute_file)):
+        utils.upload_file_to_cloud_storage(absolute_file,
+                                           destination_dir + relative_file,
+                                           public_read=False)
   url = 'https://storage.googleapis.com/%s/%s/test/index.html' % (BUCKET, file_name)
   print('Test results available at: %s' % url)
 
 def Main():
   (options, args) = ParseOptions()
-
   if utils.is_bot():
     gradle.RunGradle(['--no-daemon', 'clean'])
     print('Running with python ' + str(sys.version_info))