Reland "Support rerunning all previously failing tests"

This reverts commit 237fc81dbf5a3e0fc101b183bf7c77eedcd63044.

Bug: b/297316723

Change-Id: I59fabab716461650fedc4f89c0909c16267689f1
diff --git a/tools/test.py b/tools/test.py
index a6c34f0..40dc33c 100755
--- a/tools/test.py
+++ b/tools/test.py
@@ -19,7 +19,7 @@
 import download_kotlin_dev
 import gradle
 import notify
-import test_state
+import testing_state
 import utils
 
 if utils.is_python3():
@@ -183,17 +183,13 @@
       help='Print the execution time of the slowest tests..',
       default=False, action='store_true')
   result.add_option(
-      '--testing-state-name',
-      help='Set an explict name for the testing state '
-          '(used in conjunction with --with/reset-testing-state).')
+      '--testing-state-dir',
+      help='Explicitly set the testing state directory '
+           '(defaults to build/test-state/<git-branch>).')
   result.add_option(
-      '--with-testing-state',
-      help='Run/resume tests using testing state.',
-      default=False, action='store_true')
-  result.add_option(
-      '--reset-testing-state',
-      help='Clean the testing state and rerun tests (implies --with-testing-state).',
-      default=False, action='store_true')
+      '--rerun',
+      help='Rerun tests (implicitly enables testing state).',
+      choices=testing_state.CHOICES)
   result.add_option(
       '--stacktrace',
       help='Pass --stacktrace to the gradle run',
@@ -276,9 +272,6 @@
 
   gradle_args = []
 
-  testing_state = False
-  testing_state_path = None
-
   if options.stacktrace or utils.is_bot():
     gradle_args.append('--stacktrace')
 
@@ -366,20 +359,10 @@
     gradle_args.append('-Pdesugar_jdk_libs=' + desugar_jdk_libs)
   if options.no_arttests:
     gradle_args.append('-Pno_arttests=true')
-  if options.reset_testing_state:
-    testing_state = True
-    gradle_args.append('-Preset-testing-state')
-  elif options.with_testing_state:
-    testing_state = True
-  if options.testing_state_name:
-    gradle_args.append('-Ptesting-state-name=' + options.testing_state_name)
-    testing_state_path = "%s/test-state/%s" % (utils.BUILD, options.testing_state_name)
 
-  if testing_state:
-    if options.new_gradle:
-      test_state.set_up_test_state(gradle_args, testing_state_path)
-    else:
-      gradle_args.append('-Ptesting-state')
+  # Testing state is only supported in new-gradle going forward
+  if options.new_gradle and options.rerun:
+    testing_state.set_up_test_state(gradle_args, options.rerun, options.testing_state_dir)
 
   # Enable completeness testing of ART profile rewriting.
   gradle_args.append('-Part_profile_rewriting_completeness_check=true')
diff --git a/tools/test_state.py b/tools/testing_state.py
similarity index 74%
rename from tools/test_state.py
rename to tools/testing_state.py
index 4541533..7c51790 100644
--- a/tools/test_state.py
+++ b/tools/testing_state.py
@@ -8,13 +8,17 @@
 import datetime
 import os
 
-def set_up_test_state(gradle_args, testing_state_path):
-  # In the new build the test state directory must be passed explictitly.
-  # TODO(b/297316723): Simplify this and just support a single flag: --testing-state <path>
+CHOICES = ["all", "failing", "past-failing", "outstanding"]
+DEFAULT_REPORTS_ROOT = os.path.join(utils.BUILD, "testing-state")
+
+def set_up_test_state(gradle_args, testing_state_mode, testing_state_path):
+  if not testing_state_mode:
+    return
   if not testing_state_path:
-    testing_state_path = "%s/test-state/%s" % (utils.BUILD, utils.get_HEAD_branch())
-  gradle_args.append('-Ptesting-state=%s' % testing_state_path)
-  prepare_testing_index(testing_state_path)
+    testing_state_path = os.path.join(DEFAULT_REPORTS_ROOT, utils.get_HEAD_branch())
+  gradle_args.append('-Ptesting-state-mode=%s' % testing_state_mode)
+  gradle_args.append('-Ptesting-state-path=%s' % testing_state_path)
+  prepare_testing_index(testing_state_mode, testing_state_path)
 
 def fresh_testing_index(testing_state_dir):
   number = 0
@@ -24,22 +28,22 @@
     if not os.path.exists(freshIndex):
       return freshIndex
 
-def prepare_testing_index(testing_state_dir):
+def prepare_testing_index(testing_state_mode, testing_state_dir):
   if not os.path.exists(testing_state_dir):
     os.makedirs(testing_state_dir)
   index_path = os.path.join(testing_state_dir, "index.html")
   parent_report = None
   resuming = os.path.exists(index_path)
+  mode = testing_state_mode if resuming else f"starting (flag: {testing_state_mode})"
   if (resuming):
     parent_report = fresh_testing_index(testing_state_dir)
     os.rename(index_path, parent_report)
   index = open(index_path, "a")
-  run_prefix = "Resuming" if resuming else "Starting"
   relative_state_dir = os.path.relpath(testing_state_dir)
-  title = f"{run_prefix} @ {relative_state_dir}"
+  title = relative_state_dir
   # Print a console link to the test report for easy access.
   print("=" * 70)
-  print(f"{run_prefix} test, report written to:")
+  print("Test report written to:")
   print(f"  file://{index_path}")
   print("=" * 70)
   # Print the new index content.
@@ -47,6 +51,7 @@
   index.write("<style> * { font-family: monospace; }</style>")
   index.write("<meta http-equiv='refresh' content='10' />")
   index.write(f"</head><body><h1>{title}</h1>")
+  index.write(f"<h2>Mode: {mode}</h2>")
   # write index links first to avoid jumping when browsing.
   if parent_report:
     index.write(f"<p><a href=\"file://{parent_report}\">Previous result index</a></p>")