Test strategy unification - extract env setup to a new class
Introduce a new class TestPolicy that computes the test environment and use it
from all the test strategies instead of the previous duplicated code.
Note how the TestPolicy constructs the environment in four steps, each of which
can override variables set in a previous step:
1. set env variables for which we have a hard-coded policy
2. set env variables from --action_env
3. set env variables from --test_env
4. set env variables that are specific to the test (path locations, coverage
collection protocol, test sharding protocol, etc.); the code for this lives
in TestRunnerAction
This changes the order in which env variables are set, which can change the
resulting environment if --test_env or --action_env are set. We should consider
not allowing the command-line flags to override TEST_SRCDIR, RUNFILES_DIR, or
TEST_TMPDIR.
We should also try to find a way to drop the RUNTEST_PRESERVE_CWD variable in
coverage mode. I'm fairly sure that it's current use breaks the combination
of coverage and --run_under. My current thinking is that we change the test
setup script to source the coverage collector before changing the CWD. Another
option is to merge the coverage collector into the test setup script.
--
PiperOrigin-RevId: 146237448
MOS_MIGRATED_REVID=146237448
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 41004a7..2f754a8 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
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.exec;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.Artifact;
@@ -30,7 +31,6 @@
import com.google.devtools.build.lib.actions.TestExecException;
import com.google.devtools.build.lib.analysis.RunfilesSupplierImpl;
import com.google.devtools.build.lib.analysis.config.BinTools;
-import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.events.Reporter;
@@ -41,6 +41,7 @@
import com.google.devtools.build.lib.util.io.FileOutErr;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
import com.google.devtools.build.lib.view.test.TestStatus.TestCase;
import com.google.devtools.build.lib.view.test.TestStatus.TestResultData;
@@ -61,6 +62,20 @@
public static final String COLLECT_COVERAGE =
"external/bazel_tools/tools/test/collect_coverage.sh";
+ private static final ImmutableMap<String, String> ENV_VARS =
+ ImmutableMap.<String, String>builder()
+ .put("TZ", "UTC")
+ .put("USER", TestPolicy.SYSTEM_USER_NAME)
+ .put("TEST_SRCDIR", TestPolicy.RUNFILES_DIR)
+ // TODO(lberki): Remove JAVA_RUNFILES and PYTHON_RUNFILES.
+ .put("JAVA_RUNFILES", TestPolicy.RUNFILES_DIR)
+ .put("PYTHON_RUNFILES", TestPolicy.RUNFILES_DIR)
+ .put("RUNFILES_DIR", TestPolicy.RUNFILES_DIR)
+ .put("TEST_TMPDIR", TestPolicy.TEST_TMP_DIR)
+ .build();
+
+ public static final TestPolicy DEFAULT_LOCAL_POLICY = new TestPolicy(ENV_VARS);
+
protected final Path tmpDirRoot;
public StandaloneTestStrategy(
@@ -231,27 +246,18 @@
private Map<String, String> setupEnvironment(
TestRunnerAction action, Path execRoot, Path runfilesDir, Path tmpDir) {
- Map<String, String> env = getDefaultTestEnvironment(action);
- BuildConfiguration config = action.getConfiguration();
-
- env.putAll(config.getLocalShellEnvironment());
- env.putAll(action.getTestEnv());
-
- String tmpDirString;
+ PathFragment relativeTmpDir;
if (tmpDir.startsWith(execRoot)) {
- tmpDirString = tmpDir.relativeTo(execRoot).getPathString();
+ relativeTmpDir = tmpDir.relativeTo(execRoot);
} else {
- tmpDirString = tmpDir.getPathString();
+ relativeTmpDir = tmpDir.asFragment();
}
-
- String testSrcDir = runfilesDir.relativeTo(execRoot).getPathString();
- env.put("JAVA_RUNFILES", testSrcDir);
- env.put("PYTHON_RUNFILES", testSrcDir);
- env.put("TEST_SRCDIR", testSrcDir);
- env.put("TEST_TMPDIR", tmpDirString);
-
- action.setupEnvVariables(env, getTimeout(action));
- return env;
+ return DEFAULT_LOCAL_POLICY.computeTestEnvironment(
+ action,
+ clientEnv,
+ getTimeout(action),
+ runfilesDir.relativeTo(execRoot),
+ relativeTmpDir);
}
protected TestResultData executeTest(