Skip to content

Commit

Permalink
Do not crash tests when one of the {JAVA,PYTHON}_RUNFILES variables…
Browse files Browse the repository at this point in the history
… is overridden.

Environment collection uses `ImmutableMap.Builder::buildOrThrow` for the variables map which will crash when user specifies only one of the runfiles variables on the command line. Change the logic to pick up the values specified by the user instead.

PiperOrigin-RevId: 446207370
  • Loading branch information
alexjski authored and copybara-github committed May 3, 2022
1 parent a801426 commit fec7962
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public ImmutableMap<String, String> getEnvironment() {
return environment;
} else {
ImmutableMap.Builder<String, String> env = ImmutableMap.builder();
env.putAll(environment);
// TODO(bazel-team): Unify these into a single env variable.
String runfilesRootString = runfilesRoot.getPathString();
env.put("JAVA_RUNFILES", runfilesRootString);
env.put("PYTHON_RUNFILES", runfilesRootString);
return env.buildOrThrow();
env.putAll(environment);
return env.buildKeepingLast();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ protected Path getExecutableLocation(String target)
*
* @param target the label of the target whose artifacts are requested.
*/
protected Iterable<Artifact> getArtifacts(String target)
protected ImmutableList<Artifact> getArtifacts(String target)
throws LabelSyntaxException, NoSuchPackageException, NoSuchTargetException,
InterruptedException, TransitionException, InvalidConfigurationException {
return getFilesToBuild(getConfiguredTarget(target)).toList();
Expand Down
32 changes: 32 additions & 0 deletions src/test/shell/bazel/bazel_test_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ EOF
expect_log "ws: bar$"
}

function test_runfiles_java_runfiles_merges_env_vars() {
runfiles_merges_runfiles_env_vars JAVA_RUNFILES PYTHON_RUNFILES
}

function test_runfiles_python_runfiles_merges_env_vars() {
runfiles_merges_runfiles_env_vars PYTHON_RUNFILES JAVA_RUNFILES
}

# Usage: runfiles_merges_runfiles_env_vars overridden unchanged
function runfiles_merges_runfiles_env_vars() {
local -r overridden=$1
local -r unchanged=$2
cat > WORKSPACE <<EOF
workspace(name = "bar")
EOF
add_rules_cc_to_workspace WORKSPACE
mkdir -p foo
cat > foo/foo.sh <<'EOF'
#!/bin/sh
echo "JAVA_RUNFILES: ${JAVA_RUNFILES}"
echo "PYTHON_RUNFILES: ${PYTHON_RUNFILES}"
EOF
chmod +x foo/foo.sh
echo 'sh_test(name = "foo", srcs = ["foo.sh"])' > foo/BUILD

bazel test --test_env="${overridden}"=override --test_output=all \
//foo >& "${TEST_log}" || fail "Test failed"

expect_log "${overridden}: /.*/execroot/bar/override"
expect_log "${unchanged}: /.*/execroot/bar/bazel-out/[^/]\+-fastbuild/bin/foo/foo.runfiles"
}

function test_run_under_external_label_with_options() {
mkdir -p testing run || fail "mkdir testing run failed"
cat <<EOF > run/BUILD
Expand Down

0 comments on commit fec7962

Please sign in to comment.