Refactor test action preparation

Move all deletion / directory creation to prepareFileSystem.

PiperOrigin-RevId: 245374483
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java b/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java
index 2c45402..ead826f 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java
@@ -923,6 +923,10 @@
       return getPath(xmlOutputPath);
     }
 
+    public Path getCoverageDirectory() {
+      return getPath(TestRunnerAction.this.getCoverageDirectory());
+    }
+
     public Path getCoverageDataPath() {
       return getPath(getCoverageData().getExecPath());
     }
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 0483952..2eb8c08 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
@@ -97,7 +97,6 @@
       TestRunnerAction action, ActionExecutionContext actionExecutionContext)
       throws ExecException, InterruptedException {
     Path execRoot = actionExecutionContext.getExecRoot();
-    Path coverageDir = execRoot.getRelative(action.getCoverageDirectory());
     Path runfilesDir =
         getLocalRunfilesDirectory(
             action,
@@ -143,7 +142,7 @@
             ImmutableList.copyOf(action.getSpawnOutputs()),
             localResourceUsage);
     return new StandaloneTestRunnerSpawn(
-        action, actionExecutionContext, spawn, tmpDir, coverageDir, workingDirectory, execRoot);
+        action, actionExecutionContext, spawn, tmpDir, workingDirectory, execRoot);
   }
 
   private StandaloneFailedAttemptResult processFailedTestAttempt(
@@ -454,7 +453,6 @@
     private final ActionExecutionContext actionExecutionContext;
     private final Spawn spawn;
     private final Path tmpDir;
-    private final Path coverageDir;
     private final Path workingDirectory;
     private final Path execRoot;
 
@@ -463,14 +461,12 @@
         ActionExecutionContext actionExecutionContext,
         Spawn spawn,
         Path tmpDir,
-        Path coverageDir,
         Path workingDirectory,
         Path execRoot) {
       this.testAction = testAction;
       this.actionExecutionContext = actionExecutionContext;
       this.spawn = spawn;
       this.tmpDir = tmpDir;
-      this.coverageDir = coverageDir;
       this.workingDirectory = workingDirectory;
       this.execRoot = execRoot;
     }
@@ -483,7 +479,7 @@
     @Override
     public TestAttemptContinuation beginExecution()
         throws InterruptedException, IOException, ExecException {
-      prepareFileSystem(testAction, tmpDir, coverageDir, workingDirectory);
+      prepareFileSystem(testAction, actionExecutionContext.getExecRoot(), tmpDir, workingDirectory);
       return beginTestAttempt(testAction, spawn, actionExecutionContext, execRoot);
     }
 
diff --git a/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java b/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java
index 0dd2189..c846254 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/TestStrategy.java
@@ -32,6 +32,7 @@
 import com.google.devtools.build.lib.analysis.test.TestConfiguration;
 import com.google.devtools.build.lib.analysis.test.TestResult;
 import com.google.devtools.build.lib.analysis.test.TestRunnerAction;
+import com.google.devtools.build.lib.analysis.test.TestRunnerAction.ResolvedPaths;
 import com.google.devtools.build.lib.analysis.test.TestTargetExecutionSettings;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.events.Event;
@@ -63,24 +64,32 @@
    * not result in stale files.
    */
   protected void prepareFileSystem(
-      TestRunnerAction testAction, Path tmpDir, Path coverageDir, Path workingDirectory)
+      TestRunnerAction testAction, Path execRoot, Path tmpDir, Path workingDirectory)
       throws IOException {
-    if (testAction.isCoverageMode()) {
-      recreateDirectory(coverageDir);
+    if (tmpDir != null) {
+      recreateDirectory(tmpDir);
     }
-    recreateDirectory(tmpDir);
-    workingDirectory.createDirectoryAndParents();
+    if (workingDirectory != null) {
+      workingDirectory.createDirectoryAndParents();
+    }
+
+    ResolvedPaths resolvedPaths = testAction.resolve(execRoot);
+    if (testAction.isCoverageMode()) {
+      recreateDirectory(resolvedPaths.getCoverageDirectory());
+    }
+
+    resolvedPaths.getBaseDir().createDirectoryAndParents();
+    resolvedPaths.getUndeclaredOutputsDir().createDirectoryAndParents();
+    resolvedPaths.getUndeclaredOutputsAnnotationsDir().createDirectoryAndParents();
+    resolvedPaths.getSplitLogsDir().createDirectoryAndParents();
   }
 
   /**
    * Ensures that all directories used to run test are in the correct state and their content will
    * not result in stale files. Only use this if no local tmp and working directory are required.
    */
-  protected void prepareFileSystem(TestRunnerAction testAction, Path coverageDir)
-      throws IOException {
-    if (testAction.isCoverageMode()) {
-      recreateDirectory(coverageDir);
-    }
+  protected void prepareFileSystem(TestRunnerAction testAction, Path execRoot) throws IOException {
+    prepareFileSystem(testAction, execRoot, null, null);
   }
 
   /** Removes directory if it exists and recreates it. */
diff --git a/src/test/shell/bazel/bazel_test_test.sh b/src/test/shell/bazel/bazel_test_test.sh
index f2f9a0a..07c1f82 100755
--- a/src/test/shell/bazel/bazel_test_test.sh
+++ b/src/test/shell/bazel/bazel_test_test.sh
@@ -717,12 +717,12 @@
   bazel test -s //dir:test &> $TEST_log || fail "expected success"
 
   # Check that the undeclared outputs directory doesn't exist.
-  outputs_dir=bazel-testlogs/dir/test/test.outputs/
-  [ ! -d $outputs_dir ] || fail "$outputs_dir was present after test"
+  outputs_zip=bazel-testlogs/dir/test/test.outputs/outputs.zip
+  [ ! -e $outputs_zip ] || fail "$outputs_zip was present after test"
 
   # Check that the undeclared outputs manifest directory doesn't exist.
-  outputs_manifest_dir=bazel-testlogs/dir/test/test.outputs_manifest/
-  [ ! -d $outputs_manifest_dir ] || fail "$outputs_manifest_dir was present after test"
+  outputs_manifest=bazel-testlogs/dir/test/test.outputs_manifest/MANIFEST
+  [ ! -d $outputs_manifest ] || fail "$outputs_manifest was present after test"
 }
 
 function test_test_with_nobuild_runfile_manifests() {