StandaloneTestStrategy: Pass relative JAVA_RUNFILES and PYTHON_RUNFILES to tests

--
Change-Id: I97319dc2c676100fb8f1224b9dcd6fbc8ec7e5e5
Reviewed-on: https://bazel-review.googlesource.com/#/c/4200/3
MOS_MIGRATED_REVID=128973499
diff --git a/src/main/java/com/google/devtools/build/lib/actions/BaseSpawn.java b/src/main/java/com/google/devtools/build/lib/actions/BaseSpawn.java
index 2e3a69d..3cfff11 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/BaseSpawn.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/BaseSpawn.java
@@ -25,12 +25,10 @@
 import com.google.devtools.build.lib.util.CommandFailureUtils;
 import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.lib.vfs.PathFragment;
-
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
 import javax.annotation.concurrent.Immutable;
 
 /**
@@ -212,7 +210,9 @@
   @Override
   public ImmutableMap<String, String> getEnvironment() {
     PathFragment runfilesRoot = getRunfilesRoot();
-    if (runfilesRoot == null) {
+    if (runfilesRoot == null
+        || (environment.containsKey("JAVA_RUNFILES")
+            && environment.containsKey("PYTHON_RUNFILES"))) {
       return environment;
     } else {
       ImmutableMap.Builder<String, String> env = ImmutableMap.builder();
diff --git a/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java b/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
index a6562d5..acce97a 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/test/StandaloneTestStrategy.java
@@ -156,7 +156,10 @@
     vars.putAll(config.getLocalShellEnvironment());
     vars.putAll(action.getTestEnv());
 
-    vars.put("TEST_SRCDIR", runfilesDir.relativeTo(execRoot).getPathString());
+    String testSrcDir = runfilesDir.relativeTo(execRoot).getPathString();
+    vars.put("JAVA_RUNFILES", testSrcDir);
+    vars.put("PYTHON_RUNFILES", testSrcDir);
+    vars.put("TEST_SRCDIR", testSrcDir);
     vars.put("TEST_TMPDIR", tmpDir.relativeTo(execRoot).getPathString());
     vars.put("TEST_WORKSPACE", action.getRunfilesPrefix());
     vars.put("XML_OUTPUT_FILE", xmlOutputPath.relativeTo(execRoot).getPathString());