Link the host/target dir to the execution files

Start preparing to replace the search logic.

Test: unit tests
Bug: 146355153
Change-Id: I7ebb6b3546884c02d601302d7139decdc20effd7
diff --git a/invocation_interfaces/com/android/tradefed/invoker/ExecutionFiles.java b/invocation_interfaces/com/android/tradefed/invoker/ExecutionFiles.java
index 42926b6..e5d4012 100644
--- a/invocation_interfaces/com/android/tradefed/invoker/ExecutionFiles.java
+++ b/invocation_interfaces/com/android/tradefed/invoker/ExecutionFiles.java
@@ -25,12 +25,21 @@
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ConcurrentSkipListSet;
 
-/** Files generated during the execution of a test or invocation that need to be carried */
+/**
+ * Files dependencies generated during the execution of a test or invocation that need to be carried
+ * for testing.
+ */
 public class ExecutionFiles {
 
     /** Enumeration of known standard key for the map. */
     public static enum FilesKey {
-        ADB_BINARY
+        ADB_BINARY,
+        // Describes the directory containing the build target tests artifacts.
+        TESTS_DIRECTORY,
+        // Sub-directory of TESTS_DIRECTORY that contains target artifacts
+        TARGET_TESTS_DIRECTORY,
+        // Sub-directory of TESTS_DIRECTORY that contains host-side artifacts
+        HOST_TESTS_DIRECTORY
     }
 
     private final ConcurrentMap<String, File> mFiles = new ConcurrentHashMap<>();
diff --git a/src/com/android/tradefed/invoker/InvocationExecution.java b/src/com/android/tradefed/invoker/InvocationExecution.java
index 53cb0e3..f6efeee 100644
--- a/src/com/android/tradefed/invoker/InvocationExecution.java
+++ b/src/com/android/tradefed/invoker/InvocationExecution.java
@@ -36,6 +36,7 @@
 import com.android.tradefed.device.metric.CollectorHelper;
 import com.android.tradefed.device.metric.IMetricCollector;
 import com.android.tradefed.device.metric.IMetricCollectorReceiver;
+import com.android.tradefed.invoker.ExecutionFiles.FilesKey;
 import com.android.tradefed.invoker.TestInvocation.Stage;
 import com.android.tradefed.invoker.logger.InvocationMetricLogger;
 import com.android.tradefed.invoker.logger.InvocationMetricLogger.InvocationMetricKey;
@@ -144,6 +145,7 @@
                 }
                 // TODO: remove build update when reporting is done on context
                 updateBuild(info, config);
+                linkExternalDirs(info, testInfo);
                 info.setTestResourceBuild(config.isDeviceConfiguredFake(currentDeviceName));
             }
         } catch (BuildRetrievalError e) {
@@ -657,27 +659,6 @@
                     "shard_index", config.getCommandOptions().getShardIndex().toString());
         }
         setTestTag(info, config);
-
-        if (info.getProperties().contains(BuildInfoProperties.DO_NOT_LINK_TESTS_DIR)) {
-            CLog.d("Skip linking external directory as FileProperty was set.");
-            return;
-        }
-        // Load environment tests dir.
-        if (info instanceof IDeviceBuildInfo) {
-            File testsDir = ((IDeviceBuildInfo) info).getTestsDir();
-            if (testsDir != null && testsDir.exists()) {
-                handleLinkingExternalDirs(
-                        (IDeviceBuildInfo) info,
-                        testsDir,
-                        EnvVariable.ANDROID_TARGET_OUT_TESTCASES,
-                        BuildInfoFileKey.TARGET_LINKED_DIR.getFileKey());
-                handleLinkingExternalDirs(
-                        (IDeviceBuildInfo) info,
-                        testsDir,
-                        EnvVariable.ANDROID_HOST_OUT_TESTCASES,
-                        BuildInfoFileKey.HOST_LINKED_DIR.getFileKey());
-            }
-        }
     }
 
     private void runTest(
@@ -744,7 +725,40 @@
                 InvocationMetricKey.AUTO_RETRY_TIME, Long.toString(totalRetryMs));
     }
 
-    private void handleLinkingExternalDirs(
+    private void linkExternalDirs(IBuildInfo info, TestInformation testInfo) {
+        if (info.getProperties().contains(BuildInfoProperties.DO_NOT_LINK_TESTS_DIR)) {
+            CLog.d("Skip linking external directory as FileProperty was set.");
+            return;
+        }
+        // Load environment tests dir.
+        if (info instanceof IDeviceBuildInfo) {
+            File testsDir = ((IDeviceBuildInfo) info).getTestsDir();
+            if (testsDir != null && testsDir.exists()) {
+                File targetTestCases =
+                        handleLinkingExternalDirs(
+                                (IDeviceBuildInfo) info,
+                                testsDir,
+                                EnvVariable.ANDROID_TARGET_OUT_TESTCASES,
+                                BuildInfoFileKey.TARGET_LINKED_DIR.getFileKey());
+                if (targetTestCases != null) {
+                    testInfo.executionFiles()
+                            .put(FilesKey.TARGET_TESTS_DIRECTORY, targetTestCases, true);
+                }
+                File hostTestCases =
+                        handleLinkingExternalDirs(
+                                (IDeviceBuildInfo) info,
+                                testsDir,
+                                EnvVariable.ANDROID_HOST_OUT_TESTCASES,
+                                BuildInfoFileKey.HOST_LINKED_DIR.getFileKey());
+                if (hostTestCases != null) {
+                    testInfo.executionFiles()
+                            .put(FilesKey.HOST_TESTS_DIRECTORY, hostTestCases, true);
+                }
+            }
+        }
+    }
+
+    private File handleLinkingExternalDirs(
             IDeviceBuildInfo info, File testsDir, EnvVariable var, String baseName) {
         File externalDir = getExternalTestCasesDirs(var);
         if (externalDir == null) {
@@ -757,8 +771,9 @@
                         varDir,
                         /** version */
                         "v1");
+                return varDir;
             }
-            return;
+            return null;
         }
         try {
             // Avoid conflict by creating a randomized name for the arriving symlink file.
@@ -773,10 +788,12 @@
                     "v1");
             // Ensure we always delete the linking, no matter how the JVM exits.
             subDir.deleteOnExit();
+            return subDir;
         } catch (IOException e) {
             CLog.e("Failed to load external test dir %s. Ignoring it.", externalDir);
             CLog.e(e);
         }
+        return null;
     }
 
     /** Populate the shared resources directory for all non-resource build */