Replace `enable_errexit` with `set -euo pipefail` in Bazel tests.
Bash unit test framework already supports tests using canonical `set -euo pipefail`, which is semantically equivalent to `enable_errexit`. Remove the usage of the custom function in favor of the canonical "strict Bash" switch.
PiperOrigin-RevId: 402579935
diff --git a/scripts/bash_completion_test.sh b/scripts/bash_completion_test.sh
index ca66c42..6f6929e 100755
--- a/scripts/bash_completion_test.sh
+++ b/scripts/bash_completion_test.sh
@@ -16,6 +16,8 @@
#
# bash_completion_test.sh: tests of bash command completion.
+set -euo pipefail
+
: ${DIR:=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}
source ${DIR}/testenv.sh || { echo "testenv.sh not found!" >&2; exit 1; }
@@ -170,9 +172,8 @@
assert_expansion_function() {
local ws=${PWD}
local function="$1" displacement="$2" type="$3" expected="$4" current="$5"
- disable_errexit
- local actual_result=$(eval "_bazel__${function} \"${ws}\" \"${displacement}\" \"${current}\" \"${type}\"" | sort)
- enable_errexit
+ # Disable the test ERR trap for the generated function itself.
+ local actual_result=$(trap - ERR; "_bazel__${function}" "${ws}" "${displacement}" "${current}" "${type}" | sort)
assert_equals "$(echo -ne "${expected}")" "${actual_result}"
}
diff --git a/scripts/bazel-complete-header.bash b/scripts/bazel-complete-header.bash
index 7058459..7236670 100644
--- a/scripts/bazel-complete-header.bash
+++ b/scripts/bazel-complete-header.bash
@@ -37,9 +37,9 @@
# of tests/benchmarks locally with 'bazel run'.
_bazel_completion_use_query() {
- _bazel__is_true "${BAZEL_COMPLETION_USE_QUERY}"
+ _bazel__is_true "${BAZEL_COMPLETION_USE_QUERY-}"
}
_bazel_completion_allow_tests_for_run() {
- _bazel__is_true "${BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN}"
+ _bazel__is_true "${BAZEL_COMPLETION_ALLOW_TESTS_FOR_RUN-}"
}
diff --git a/src/test/shell/bazel/bazel_workspaces_test.sh b/src/test/shell/bazel/bazel_workspaces_test.sh
index 8f1663f..e739bcf 100755
--- a/src/test/shell/bazel/bazel_workspaces_test.sh
+++ b/src/test/shell/bazel/bazel_workspaces_test.sh
@@ -60,26 +60,23 @@
}
function ensure_contains_exactly() {
- num=`grep "${1}" output.log.txt | wc -l`
- if [ "$num" -ne $2 ]
- then
+ local -r num=$(grep -c "$1" output.log.txt || true)
+ if (( num != $2 )); then
fail "Expected exactly $2 occurrences of $1, got $num: " `cat output.log.txt`
fi
}
function ensure_contains_atleast() {
- num=`grep "${1}" output.log.txt | wc -l`
- if [ "$num" -lt $2 ]
- then
+ local -r num=$(grep -c "$1" output.log.txt || true)
+ if (( num < $2 )); then
fail "Expected at least $2 occurrences of $1, got $num: " `cat output.log.txt`
fi
}
function ensure_output_contains_exactly_once() {
- file_path=$(bazel info output_base)/$1
- num=`grep "$2" $file_path | wc -l`
- if [ "$num" -ne 1 ]
- then
+ local -r file_path=$(bazel info output_base)/$1
+ local -r num=$(grep -c "$2" $file_path || true)
+ if (( num != 1 )); then
fail "Expected to read \"$2\" in $1, but got $num occurrences: " `cat $file_path`
fi
}
diff --git a/src/test/shell/bazel/run_test.sh b/src/test/shell/bazel/run_test.sh
index 3bb3c0d..8f30f03 100755
--- a/src/test/shell/bazel/run_test.sh
+++ b/src/test/shell/bazel/run_test.sh
@@ -111,7 +111,6 @@
cat >$pkg/a.sh <<eof
echo Hello World
eof
- disable_errexit
# This test uses the content of the Bazel error message to test that Bazel correctly handles
# paths to Bash which contain spaces - which is needed when using --run_under as it
# unconditionally results in Bazel wrapping the executable in Bash.
@@ -122,8 +121,7 @@
# rc/main/native/windows/process.cc(202): CreateProcessW("C:\first_
# part second_part"
# ```
- output="$(BAZEL_SH="C:/first_part second_part" bazel run --run_under=":;" $pkg:a 2>&1)"
- enable_errexit
+ output="$(BAZEL_SH="C:/first_part second_part" bazel run --run_under=":;" $pkg:a 2>&1 || true)"
echo "$output" | grep --fixed-strings 'ExecuteProgram(C:\first_part second_part)' || fail "Expected error message to contain unquoted path"
}
diff --git a/src/test/shell/integration/bazel_worker_multiplexer_test.sh b/src/test/shell/integration/bazel_worker_multiplexer_test.sh
index cd8b177..bd00eae 100755
--- a/src/test/shell/integration/bazel_worker_multiplexer_test.sh
+++ b/src/test/shell/integration/bazel_worker_multiplexer_test.sh
@@ -243,7 +243,7 @@
assert_equals "$worker_uuid_1" "$worker_uuid_2"
# Modify the example worker jar to trigger a rebuild of the worker.
- tr -cd '[:alnum:]' < /dev/urandom | head -c32 > dummy_file
+ tr -cd '[:alnum:]' < /dev/urandom | head -c32 > dummy_file || true
zip worker_lib.jar dummy_file
rm dummy_file
diff --git a/src/test/shell/integration/bazel_worker_test.sh b/src/test/shell/integration/bazel_worker_test.sh
index b74f759..d640216 100755
--- a/src/test/shell/integration/bazel_worker_test.sh
+++ b/src/test/shell/integration/bazel_worker_test.sh
@@ -437,7 +437,7 @@
assert_equals "$worker_uuid_1" "$worker_uuid_2"
# Modify the example worker jar to trigger a rebuild of the worker.
- tr -cd '[:alnum:]' < /dev/urandom | head -c32 > dummy_file
+ tr -cd '[:alnum:]' < /dev/urandom | head -c32 > dummy_file || true
zip worker_lib.jar dummy_file
rm dummy_file
diff --git a/src/test/shell/integration/execution_statistics_utils.sh b/src/test/shell/integration/execution_statistics_utils.sh
index d958c32..f3aa087 100644
--- a/src/test/shell/integration/execution_statistics_utils.sh
+++ b/src/test/shell/integration/execution_statistics_utils.sh
@@ -16,9 +16,6 @@
set -euo pipefail
-# Assumes integration_test_setup.sh was loaded elsewhere (can't load it twice)
-enable_errexit
-
readonly CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly STATS_PROTO_PATH="${CURRENT_DIR}/../../../main/protobuf/execution_statistics.proto"
readonly STATS_PROTO_DIR="$(cd "$(dirname "${STATS_PROTO_PATH}")" && pwd)"
diff --git a/src/test/shell/integration/linux-sandbox_network_test.sh b/src/test/shell/integration/linux-sandbox_network_test.sh
index e3c02f8..48a187c 100755
--- a/src/test/shell/integration/linux-sandbox_network_test.sh
+++ b/src/test/shell/integration/linux-sandbox_network_test.sh
@@ -29,8 +29,6 @@
source "${CURRENT_DIR}/../sandboxing_test_utils.sh" \
|| { echo "sandboxing_test_utils.sh not found!" >&2; exit 1; }
-enable_errexit
-
readonly OUT_DIR="${TEST_TMPDIR}/out"
readonly SANDBOX_DIR="${OUT_DIR}/sandbox"
diff --git a/src/test/shell/integration/linux-sandbox_test.sh b/src/test/shell/integration/linux-sandbox_test.sh
index 33a44ec..dd73960 100755
--- a/src/test/shell/integration/linux-sandbox_test.sh
+++ b/src/test/shell/integration/linux-sandbox_test.sh
@@ -28,8 +28,6 @@
source "${CURRENT_DIR}/execution_statistics_utils.sh" \
|| { echo "execution_statistics_utils.sh not found!" >&2; exit 1; }
-enable_errexit
-
readonly OUT_DIR="${TEST_TMPDIR}/out"
readonly OUT="${OUT_DIR}/outfile"
readonly ERR="${OUT_DIR}/errfile"
diff --git a/src/test/shell/integration/process-wrapper_test.sh b/src/test/shell/integration/process-wrapper_test.sh
index 136edd6..e08d43e 100755
--- a/src/test/shell/integration/process-wrapper_test.sh
+++ b/src/test/shell/integration/process-wrapper_test.sh
@@ -23,8 +23,6 @@
source "${CURRENT_DIR}/execution_statistics_utils.sh" \
|| { echo "execution_statistics_utils.sh not found!" >&2; exit 1; }
-enable_errexit
-
readonly CPU_TIME_SPENDER="${CURRENT_DIR}/../../../test/shell/integration/spend_cpu_time"
readonly OUT_DIR="${TEST_TMPDIR}/out"
diff --git a/src/test/shell/unittest_utils.sh b/src/test/shell/unittest_utils.sh
index 6f95f7f..446a3fc 100644
--- a/src/test/shell/unittest_utils.sh
+++ b/src/test/shell/unittest_utils.sh
@@ -16,8 +16,7 @@
#### Set up the test environment.
-# Enable errexit with pretty stack traces.
-enable_errexit
+set -euo pipefail
cat_jvm_log () {
if [[ "$log_content" =~ \
diff --git a/third_party/ijar/test/ijar_test.sh b/third_party/ijar/test/ijar_test.sh
index cafcb5b..f68fcf2 100755
--- a/third_party/ijar/test/ijar_test.sh
+++ b/third_party/ijar/test/ijar_test.sh
@@ -185,23 +185,24 @@
# A.RuntimeAnnotation
# (Note: even private inner classes are retained, so we don't need to change
# the types of members.)
- expected=5
+ local expected=5
+ local lines
lines=$($JAR tvf $A_INTERFACE_JAR | wc -l)
check_eq $expected $lines "Interface jar should have $expected entries!"
# Check that no private class members are found:
- lines=$($JAVAP -private -classpath $A_JAR A | grep priv | wc -l)
+ lines=$($JAVAP -private -classpath $A_JAR A | grep -c priv || true)
check_eq 2 $lines "Input jar should have 2 private members!"
- lines=$($JAVAP -private -classpath $A_INTERFACE_JAR A | grep priv | wc -l)
+ lines=$($JAVAP -private -classpath $A_INTERFACE_JAR A | grep -c priv || true)
check_eq 0 $lines "Interface jar should have no private members!"
- lines=$($JAVAP -private -classpath $A_INTERFACE_JAR A | grep clinit | wc -l)
+ lines=$($JAVAP -private -classpath $A_INTERFACE_JAR A | grep -c clinit || true)
check_eq 0 $lines "Interface jar should have no class initializers!"
# Check that no code is found:
- lines=$($JAVAP -c -private -classpath $A_JAR A | grep Code: | wc -l)
+ lines=$($JAVAP -c -private -classpath $A_JAR A | grep -c Code: || true)
check_eq 5 $lines "Input jar should have 5 method bodies!"
- lines=$($JAVAP -c -private -classpath $A_INTERFACE_JAR A | grep Code: | wc -l)
+ lines=$($JAVAP -c -private -classpath $A_INTERFACE_JAR A | grep -c Code: || true)
check_eq 0 $lines "Interface jar should have no method bodies!"
# Check that constants from code are no longer present:
@@ -231,13 +232,13 @@
# Check that -interface.jar contains nothing but .class files:
- check_eq 0 $($JAR tf $A_INTERFACE_JAR | grep -v \\.class$ | wc -l) \
+ check_eq 0 $($JAR tf $A_INTERFACE_JAR | grep -cv \\.class$ || true) \
"Interface jar should contain only .class files!"
# Check that -interface.jar timestamps are normalized:
check_eq 0 $(TZ=UTC $JAR tvf $A_INTERFACE_JAR |
- grep -v 'Fri Jan 01 00:00:00 UTC 2010' | wc -l) \
+ grep -cv 'Fri Jan 01 00:00:00 UTC 2010' || true) \
"Interface jar contained non-zero timestamps!"
@@ -362,9 +363,9 @@
function test_invokedynamic() {
# Check that ijar works on classes with invokedynamic
$IJAR $INVOKEDYNAMIC_JAR $INVOKEDYNAMIC_IJAR || fail "ijar failed"
- lines=$($JAVAP -c -private -classpath $INVOKEDYNAMIC_JAR ClassWithLambda | grep Code: | wc -l)
+ lines=$($JAVAP -c -private -classpath $INVOKEDYNAMIC_JAR ClassWithLambda | grep -c Code: || true)
check_eq 4 $lines "Input jar should have 4 method bodies!"
- lines=$($JAVAP -c -private -classpath $INVOKEDYNAMIC_IJAR ClassWithLambda | grep Code: | wc -l)
+ lines=$($JAVAP -c -private -classpath $INVOKEDYNAMIC_IJAR ClassWithLambda | grep -c Code: || true)
check_eq 0 $lines "Interface jar should have no method bodies!"
}
@@ -572,14 +573,13 @@
|| fail "ijar failed"
lines=$($JAVAP -classpath $TEST_TMPDIR/keep.jar -c -p \
functions.car.CarInlineUtilsKt |
- grep "// Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull" |
- wc -l)
+ grep -c "// Method kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull" ||
+ true)
check_eq 2 $lines "Output jar should have kept method body"
attr=$($JAVAP -classpath $TEST_TMPDIR/keep.jar -v -p \
functions.car.CarInlineUtilsKt |
strings |
- grep "com.google.devtools.ijar.KeepForCompile" |
- wc -l)
+ grep -c "com.google.devtools.ijar.KeepForCompile" || true)
check_eq 2 $attr "Output jar should have kept KeepForCompile attribute."
}