Automated rollback of commit b8514f533d4546d3bfbec3700012f2bbeffd1c37.

*** Reason for rollback ***

Breaks XML format when there is weird characters in the output (Jenkins fails to read XML output for re2 test)

*** Original change description ***

Add stdout to default XML file and generate XML file on timeout

This should fix #1027 and get better error result on Jenkins.

Change-Id: I5ce30b64f634e01dd350af10748c4a9455a6bea8
PiperOrigin-RevId: 162598130
diff --git a/src/test/shell/bazel/bazel_test_test.sh b/src/test/shell/bazel/bazel_test_test.sh
index 10cf7fa..8608147 100755
--- a/src/test/shell/bazel/bazel_test_test.sh
+++ b/src/test/shell/bazel/bazel_test_test.sh
@@ -300,33 +300,6 @@
   [ -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 c43d5ee..058c459 100755
--- a/tools/test/test-setup.sh
+++ b/tools/test/test-setup.sh
@@ -140,42 +140,6 @@
   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="<error message=\"Timed out\"></error>"
-      else
-        error_msg="<error message=\"Terminated by signal ${signal}\"></error>"
-      fi
-    elif (( $exitCode != 0 )); then
-      errors=1
-      error_msg="<error message=\"exited with error code $exitCode\"></error>"
-    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 <<EOF >${XML_OUTPUT_FILE}
-<?xml version="1.0" encoding="UTF-8"?>
-<testsuites>
-  <testsuite name="$TEST_NAME" tests="1" failures="0" errors="${errors}">
-    <testcase name="$TEST_NAME" status="run" duration="${duration}">${error_msg}</testcase>
-    <system-out>$(<"${XML_OUTPUT_FILE}.log")</system-out>
-  </testsuite>
-</testsuites>
-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.
 
@@ -197,21 +161,37 @@
 [[ -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}" "$@" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
+  "${TEST_PATH}" "$@" || exitCode=$?
 else
-  "$1" "$TEST_PATH" "${@:3}" 2> >(tee -a "${XML_OUTPUT_FILE}.log" >&2) 1> >(tee -a "${XML_OUTPUT_FILE}.log") 2>&1 || exitCode=$?
+  "$1" "$TEST_PATH" "${@:3}" || exitCode=$?
 fi
+duration=$(expr $(date +%s) - $start)
 
-for signal in $signals; do
-  trap - ${signal}
-done
-write_xml_output_file
+
+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="<error message=\"exited with error code $exitCode\"></error>"
+  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 <<EOF >${XML_OUTPUT_FILE}
+<?xml version="1.0" encoding="UTF-8"?>
+<testsuites>
+  <testsuite name="$TEST_NAME" tests="1" failures="0" errors="$errors">
+    <testcase name="$TEST_NAME" status="run" duration="$duration">$error_msg</testcase>
+  </testsuite>
+</testsuites>
+EOF
+fi
 
 exit $exitCode