diff --git a/src/test/shell/bazel/bazel_test_test.sh b/src/test/shell/bazel/bazel_test_test.sh
index 8608147764802c..10cf7fa9f2bd60 100755
--- a/src/test/shell/bazel/bazel_test_test.sh
+++ b/src/test/shell/bazel/bazel_test_test.sh
@@ -300,6 +300,33 @@ EOF
[ -s $xml_log ] || fail "$xml_log was not present after test"
}
+# Tests that the test.xml is here in case of timeout
+function test_xml_is_present_when_timingout() {
+ mkdir -p dir
+
+ cat <<'EOF' > dir/test.sh
+#!/bin/sh
+sleep 10
+EOF
+
+ chmod +x dir/test.sh
+
+ cat <<'EOF' > dir/BUILD
+ sh_test(
+ name = "test",
+ srcs = [ "test.sh" ],
+ )
+EOF
+
+ bazel test -s --test_timeout=1 \
+ //dir:test &> $TEST_log && fail "should have failed" || true
+
+ xml_log=bazel-testlogs/dir/test/test.xml
+ [ -s "${xml_log}" ] || fail "${xml_log} was not present after test"
+ cat "${xml_log}" > $TEST_log
+ expect_log '"Timed out"'
+}
+
# Check that fallback xml output is correctly generated for sharded tests.
function test_xml_fallback_for_sharded_test() {
mkdir -p dir
diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh
index 058c459cc121b4..c43d5ee9c12c99 100755
--- a/tools/test/test-setup.sh
+++ b/tools/test/test-setup.sh
@@ -140,6 +140,42 @@ if [[ -z "$no_echo" ]]; then
echo "-----------------------------------------------------------------------------"
fi
+function write_xml_output_file {
+ local duration=$(expr $(date +%s) - $start)
+ local errors=0
+ local error_msg=
+ local signal="${1-}"
+ if [ -n "${XML_OUTPUT_FILE-}" -a ! -f "${XML_OUTPUT_FILE-}" ]; then
+ # Create a default XML output file if the test runner hasn't generated it
+ if [ -n "${signal}" ]; then
+ errors=1
+ if [ "${signal}" = "SIGTERM" ]; then
+ error_msg=""
+ else
+ error_msg=""
+ fi
+ elif (( $exitCode != 0 )); then
+ errors=1
+ error_msg=""
+ fi
+ # Ensure that test shards have unique names in the xml output.
+ if [[ -n "${TEST_TOTAL_SHARDS+x}" ]] && ((TEST_TOTAL_SHARDS != 0)); then
+ ((shard_num=TEST_SHARD_INDEX+1))
+ TEST_NAME="$TEST_NAME"_shard_"$shard_num"/"$TEST_TOTAL_SHARDS"
+ fi
+ cat <${XML_OUTPUT_FILE}
+
+
+
+ ${error_msg}
+ $(<"${XML_OUTPUT_FILE}.log")
+
+
+EOF
+ fi
+ rm -f "${XML_OUTPUT_FILE}.log"
+}
+
# The path of this command-line is usually relative to the exec-root,
# but when using --run_under it can be a "/bin/bash -c" command-line.
@@ -161,37 +197,21 @@ fi
[[ -n "$RUNTEST_PRESERVE_CWD" ]] && EXE="${TEST_NAME}"
exitCode=0
+signals="$(trap -l | sed -E 's/[0-9]+\)//g')"
+for signal in $signals; do
+ trap "write_xml_output_file ${signal}" ${signal}
+done
start=$(date +%s)
+
if [ -z "$COVERAGE_DIR" ]; then
- "${TEST_PATH}" "$@" || exitCode=$?
+ "${TEST_PATH}" "$@" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
else
- "$1" "$TEST_PATH" "${@:3}" || exitCode=$?
+ "$1" "$TEST_PATH" "${@:3}" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
fi
-duration=$(expr $(date +%s) - $start)
-
-if [ -n "${XML_OUTPUT_FILE-}" -a ! -f "${XML_OUTPUT_FILE-}" ]; then
- # Create a default XML output file if the test runner hasn't generated it
- if (( $exitCode != 0 )); then
- errors=1
- error_msg=""
- else
- errors=0
- error_msg=
- fi
- # Ensure that test shards have unique names in the xml output.
- if [[ -n "${TEST_TOTAL_SHARDS+x}" ]] && ((TEST_TOTAL_SHARDS != 0)); then
- ((shard_num=TEST_SHARD_INDEX+1))
- TEST_NAME="$TEST_NAME"_shard_"$shard_num"/"$TEST_TOTAL_SHARDS"
- fi
- cat <${XML_OUTPUT_FILE}
-
-
-
- $error_msg
-
-
-EOF
-fi
+for signal in $signals; do
+ trap - ${signal}
+done
+write_xml_output_file
exit $exitCode