aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/test/generate-xml.sh
diff options
context:
space:
mode:
authorGravatar ulfjack <ulfjack@google.com>2018-07-27 02:37:53 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-27 02:39:03 -0700
commit0858ae1f6eb890c1e203a2aa21130ba34ca36a27 (patch)
tree98dd4c592049bf545ecabd6582f2a93d8d43f1d7 /tools/test/generate-xml.sh
parentd0190d3503f3adb0655579312ee359c67291992d (diff)
Add a flag to split test.xml generation into a separate Spawn
At this time, this is only implemented for the StandaloneTestStrategy. This solves a race condition on Posix-like systems, where we cannot guarantee that the pipes are actually fully flushed to disk when the test process exits, and this can cause the test.xml to be empty, which makes it hard to debug issues. (The test.log files do not show up in normal CI systems, only the test.xml files.) Progress on #4608. PiperOrigin-RevId: 206292179
Diffstat (limited to 'tools/test/generate-xml.sh')
-rw-r--r--tools/test/generate-xml.sh55
1 files changed, 55 insertions, 0 deletions
diff --git a/tools/test/generate-xml.sh b/tools/test/generate-xml.sh
new file mode 100644
index 0000000000..730b131f38
--- /dev/null
+++ b/tools/test/generate-xml.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+
+# Copyright 2018 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+TEST_LOG="$1"
+XML_OUTPUT_FILE="$2"
+DURATION_IN_SECONDS="$3"
+EXIT_CODE="$4"
+
+# Keep this in sync with test-setup.sh!
+function encode_output_file {
+ if [[ -f "$1" ]]; then
+ # Replace invalid XML characters and invalid sequence in CDATA
+ # cf. https://stackoverflow.com/a/7774512/4717701
+ perl -CSDA -pe's/[^\x9\xA\xD\x20-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/?/g;' "$1" \
+ | sed 's|]]>|]]>]]<![CDATA[>|g'
+ fi
+}
+
+test_name="${TEST_BINARY#./}"
+errors=0
+error_msg=""
+if (( $EXIT_CODE != 0 )); then
+ errors=1
+ error_msg="<error message=\"exited with error code $EXIT_CODE\"></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_IN_SECONDS}" time="${DURATION_IN_SECONDS}">${error_msg}</testcase>
+ <system-out><![CDATA[$(encode_output_file "${TEST_LOG}")]]></system-out>
+</testsuite>
+</testsuites>
+EOF
+