Actually check `TEST_SHARD_STATUS_FILE` has been touched

Adds the missing check that a test runner running a sharded test actually declares support for sharding by generating the `TEST_SHARD_STATUS_FILE`. Previously, if it didn't, each shard would silently run all tests. This new behavior is guarded by the new `--incompatible_check_sharding_support` flag.

Closes #18236.

PiperOrigin-RevId: 530259354
Change-Id: If3b01b2c796e66ad7a77e542145efe3ab412eae9
diff --git a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
index 2afb45f..30bd217 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/StandaloneTestStrategy.java
@@ -684,6 +684,25 @@
     }
     long endTimeMillis = actionExecutionContext.getClock().currentTimeMillis();
 
+    if (testAction.isSharded()) {
+      if (testAction.checkShardingSupport()
+          && !actionExecutionContext
+              .getPathResolver()
+              .convertPath(resolvedPaths.getTestShard())
+              .exists()) {
+        TestExecException e =
+            createTestExecException(
+                TestAction.Code.LOCAL_TEST_PREREQ_UNMET,
+                "Sharding requested, but the test runner did not advertise support for it by "
+                    + "touching TEST_SHARD_STATUS_FILE. Either remove the 'shard_count' attribute, "
+                    + "use a test runner that supports sharding or temporarily disable this check "
+                    + "via --noincompatible_check_sharding_support.");
+        closeSuppressed(e, streamed);
+        closeSuppressed(e, fileOutErr);
+        throw e;
+      }
+    }
+
     // SpawnActionContext guarantees the first entry to correspond to the spawn passed in (there
     // may be additional entries due to tree artifact handling).
     SpawnResult primaryResult = spawnResults.get(0);