Skip to content

Commit 01a90d6

Browse files
keithcopybara-github
authored andcommitted
Make env on test rules override --test_env
Fixes #14418 Closes #22420. PiperOrigin-RevId: 635740423 Change-Id: Iffd4d172c4175be2e1b6cfad04ddad2759adb987
1 parent 0a65893 commit 01a90d6

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

src/main/java/com/google/devtools/build/lib/exec/TestPolicy.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ public Map<String, String> computeTestEnvironment(
8787
env.put(entry.getKey(), val);
8888
}
8989

90-
// Rule-specified test env.
91-
testAction.getExtraTestEnv().resolve(env, clientEnv);
92-
9390
// Overwrite with the environment common to all actions, see --action_env.
9491
testAction.getConfiguration().getActionEnvironment().resolve(env, clientEnv);
9592

9693
// Overwrite with the environment common to all tests, see --test_env.
9794
testAction.getConfiguration().getTestActionEnvironment().resolve(env, clientEnv);
9895

96+
// Rule-specified test env.
97+
testAction.getExtraTestEnv().resolve(env, clientEnv);
98+
9999
// Setup any test-specific env variables; note that this does not overwrite existing values for
100100
// TEST_RANDOM_SEED or TEST_SIZE if they're already set.
101101
testAction.setupEnvVariables(env, timeout);
102+
102103
return env;
103104
}
104105
}

src/test/shell/bazel/bazel_test_test.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,59 @@ EOF
144144
expect_log "ws: _main$"
145145
}
146146

147+
function test_env_vars_override() {
148+
cat > WORKSPACE <<EOF
149+
workspace(name = "bar")
150+
EOF
151+
add_rules_cc_to_workspace WORKSPACE
152+
mkdir -p foo
153+
cat > foo/testenv.sh <<'EOF'
154+
#!/bin/sh
155+
echo "foo: $FOO"
156+
echo "bar: $BAR"
157+
echo "baz: $BAZ"
158+
echo "test_size: $TEST_SIZE"
159+
echo "ws: $TEST_WORKSPACE"
160+
EOF
161+
chmod +x foo/testenv.sh
162+
cat > foo/BUILD <<EOF
163+
sh_test(
164+
name = "foo",
165+
srcs = ["testenv.sh"],
166+
size = "small",
167+
env = {
168+
"FOO": "frombuild",
169+
"TEST_SIZE": "ignored",
170+
},
171+
env_inherit = [
172+
"BAZ"
173+
],
174+
)
175+
EOF
176+
177+
# Test BAR is set from --action_env
178+
BAZ=fromaction bazel --ignore_all_rc_files test --test_output=all \
179+
--action_env=BAR=fromcli --action_env=BAZ \
180+
//foo &> $TEST_log || fail "Test failed"
181+
expect_log "foo: frombuild"
182+
expect_log "bar: fromcli"
183+
expect_log "baz: fromaction"
184+
expect_log "test_size: small"
185+
expect_log "ws: _main$"
186+
187+
# Test FOO from the BUILD file wins
188+
# Test BAR is set from --test_env
189+
# Test BAZ is set from --test_env
190+
BAZ=fromtest bazel --ignore_all_rc_files test --test_output=all \
191+
--action_env=FOO=fromcli --test_env=FOO=fromcli --test_env=BAR=fromcli \
192+
--test_env=BAZ //foo &> $TEST_log || fail "Test failed"
193+
expect_log "foo: frombuild"
194+
expect_log "bar: fromcli"
195+
expect_log "baz: fromtest"
196+
expect_log "test_size: small"
197+
expect_log "ws: _main$"
198+
}
199+
147200
function test_runfiles_java_runfiles_merges_env_vars() {
148201
runfiles_merges_runfiles_env_vars JAVA_RUNFILES PYTHON_RUNFILES
149202
}

0 commit comments

Comments
 (0)