Skip to content

Commit 3bdbc35

Browse files
committed
Fix test_env_attribute_expansion.sh on Windows
As it turns out, if Bazel is actually `bazel.exe`, then Bash _will not_ export its environment variables to the Bazel process on Windows. This means that `env_inherit` functionality will have no effect, breaking test_scala_test_env_attribute_with_env_inherit_and_test_env. In this case, we _must_ export them as Command Prompt variables. Setting `bazel_bin` in `setup_suite` and using it in `test_scala_binary_env_attribute_expansion` made that test case more portable as well.
1 parent 74e95b4 commit 3bdbc35

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

test/shell/test_env_attribute_expansion.sh

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ setup_suite() {
1515
setup_test_tmpdir_for_file "$original_dir" "$test_source"
1616
test_tmpdir="$PWD"
1717
cd "$original_dir"
18+
bazel_bin="$(command -v bazel{,.exe})"
1819
}
1920

2021
teardown_suite() {
2122
rm -rf "$test_tmpdir"
2223
}
2324

2425
test_scala_binary_env_attribute_expansion() {
25-
local bindir="$(bazel info bazel-bin)"
26+
local bindir="$("$bazel_bin" info bazel-bin)"
2627
bindir="bazel-out/${bindir#*/bazel-out/}"
2728

28-
bazel run //test:EnvAttributeBinary > "${test_tmpdir}/actual.txt"
29+
"$bazel_bin" run //test:EnvAttributeBinary > "${test_tmpdir}/actual.txt"
2930

3031
printf '%s\n' \
3132
'LOCATION: West of House' \
@@ -40,13 +41,41 @@ test_scala_binary_env_attribute_expansion() {
4041
diff -u --strip-trailing-cr "${test_tmpdir}"/{expected,actual}.txt
4142
}
4243

43-
test_scala_test_env_attribute_with_env_inherit_and_test_env() {
44+
# If Bazel is actually bazel.exe, then Bash _will not_ export its environment
45+
# variables to the Bazel process. In this case, we _must_ export them as
46+
# Command Prompt variables.
47+
_bazel_bat() {
48+
local bazel_bat="${test_tmpdir}/bazel.bat"
49+
50+
printf '%s\n' \
51+
'set FROM_ENV_INHERIT=inherited value' \
52+
'set NOT_IN_ENV_INHERIT=inherited value' \
53+
'set FROM_ENV_INHERIT_AND_ENV_ATTR=inherited value' \
54+
'set FROM_ENV_INHERIT_AND_TEST_ENV=inherited value' \
55+
'set FROM_ALL_SOURCES=inherited value' \
56+
"bazel.exe %*" \
57+
> "$bazel_bat"
58+
bazel_bat="${bazel_bat#$PWD/}"
59+
cmd.exe '/c' "${bazel_bat//\//\\}" "$@"
60+
}
61+
62+
_bazel_bash() {
4463
FROM_ENV_INHERIT="inherited value" \
4564
NOT_IN_ENV_INHERIT="inherited value" \
4665
FROM_ENV_INHERIT_AND_ENV_ATTR="inherited value" \
4766
FROM_ENV_INHERIT_AND_TEST_ENV="inherited value" \
4867
FROM_ALL_SOURCES="inherited value" \
49-
bazel test \
68+
bazel "$@"
69+
}
70+
71+
test_scala_test_env_attribute_with_env_inherit_and_test_env() {
72+
local bazel='_bazel_bash'
73+
74+
if [[ "${bazel_bin##*.}" == 'exe' ]]; then
75+
bazel='_bazel_bat'
76+
fi
77+
78+
"$bazel" test \
5079
--test_env=FROM_TEST_ENV="test env value" \
5180
--test_env=FROM_TEST_ENV_AND_ENV_ATTR="test env value" \
5281
--test_env=FROM_ENV_INHERIT_AND_TEST_ENV="test env value" \

0 commit comments

Comments
 (0)