aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xsrc/test/shell/testenv.sh78
1 files changed, 51 insertions, 27 deletions
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index 1c2313fdee..6b31494f4b 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -23,13 +23,25 @@
PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
function is_windows() {
# On windows, the shell test is actually running on msys
- if [[ "${PLATFORM}" =~ msys_nt* ]]; then
- true
- else
- false
- fi
+ [[ "${PLATFORM}" =~ msys_nt* ]]
+}
+
+function _log_base() {
+ prefix=$1
+ shift
+ echo >&2 "${prefix}[$(basename "$0") $(date "+%H:%M:%S.%N (%z)")] $@"
+}
+
+function log_info() {
+ _log_base "INFO" "$@"
}
+function log_fatal() {
+ _log_base "ERROR" "$@"
+ exit 1
+}
+
+
# Set some environment variables needed on Windows.
if is_windows; then
# TODO(philwo) remove this once we have a Bazel release that includes the CL
@@ -72,17 +84,14 @@ PATH_TO_BAZEL_WRAPPER="$(dirname $(rlocation io_bazel/src/test/shell/bin/bazel))
if is_windows; then
PATH_TO_BAZEL_WRAPPER="$(cygpath -u "$PATH_TO_BAZEL_WRAPPER")"
fi
-if [ ! -f "${PATH_TO_BAZEL_WRAPPER}/bazel" ];
-then
- echo "Unable to find the Bazel binary at $PATH_TO_BAZEL_WRAPPER/bazel" >&2
- exit 1;
-fi
+[ ! -f "${PATH_TO_BAZEL_WRAPPER}/bazel" ] \
+ && log_fatal "Unable to find the Bazel binary at $PATH_TO_BAZEL_WRAPPER/bazel"
export PATH="$PATH_TO_BAZEL_WRAPPER:$PATH"
################### shell/bazel/testenv ##################################
# Setting up the environment for Bazel integration tests.
#
-[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; }
+[ -z "$TEST_SRCDIR" ] && log_fatal "TEST_SRCDIR not set!"
BAZEL_RUNFILES="$TEST_SRCDIR/io_bazel"
if ! type rlocation &> /dev/null; then
@@ -299,7 +308,7 @@ mkdir -p "${bazel_root}"
bazel_javabase="${jdk_dir}"
-echo "bazel binary is at $PATH_TO_BAZEL_WRAPPER"
+log_info "bazel binary is at $PATH_TO_BAZEL_WRAPPER"
# Here we unset variable that were set by the invoking Blaze instance
unset JAVA_RUNFILES
@@ -423,14 +432,29 @@ function create_new_workspace() {
# Set-up a clean default workspace.
function setup_clean_workspace() {
export WORKSPACE_DIR=${TEST_TMPDIR}/workspace
- echo "setting up client in ${WORKSPACE_DIR}" > $TEST_log
+ log_info "setting up client in ${WORKSPACE_DIR}" >> $TEST_log
rm -fr ${WORKSPACE_DIR}
create_new_workspace ${WORKSPACE_DIR}
- [ "${new_workspace_dir}" = "${WORKSPACE_DIR}" ] || \
- { echo "Failed to create workspace" >&2; exit 1; }
- export BAZEL_INSTALL_BASE=$(bazel info install_base)
- export BAZEL_GENFILES_DIR=$(bazel info bazel-genfiles)
- export BAZEL_BIN_DIR=$(bazel info bazel-bin)
+ [ "${new_workspace_dir}" = "${WORKSPACE_DIR}" ] \
+ || log_fatal "Failed to create workspace"
+
+ # On macOS, mktemp expects the template to have the Xs at the end.
+ # On Linux, the Xs may be anywhere.
+ local -r bazel_stdout="$(mktemp "${TEST_TMPDIR}/XXXXXXXX")"
+ local -r bazel_stderr="${bazel_stdout}.err"
+ # On Windows, we mustn't run Bazel in a subshell because of
+ # https://github.com/bazelbuild/bazel/issues/3148.
+ bazel info install_base >"$bazel_stdout" 2>"$bazel_stderr" \
+ && export BAZEL_INSTALL_BASE=$(cat "$bazel_stdout") \
+ || log_fatal "'bazel info install_base' failed, stderr: $(cat "$bazel_stderr")"
+ bazel info bazel-genfiles >"$bazel_stdout" 2>"$bazel_stderr" \
+ && export BAZEL_GENFILES_DIR=$(cat "$bazel_stdout") \
+ || log_fatal "'bazel info bazel-genfiles' failed, stderr: $(cat "$bazel_stderr")"
+ bazel info bazel-bin >"$bazel_stdout" 2>"$bazel_stderr" \
+ && export BAZEL_BIN_DIR=$(cat "$bazel_stdout") \
+ || log_fatal "'bazel info bazel-bin' failed, stderr: $(cat "$bazel_stderr")"
+ rm -f "$bazel_stdout" "$bazel_stderr"
+
if is_windows; then
export BAZEL_SH="$(cygpath --windows /bin/bash)"
fi
@@ -440,9 +464,9 @@ function setup_clean_workspace() {
# from a clean workspace
function cleanup_workspace() {
if [ -d "${WORKSPACE_DIR:-}" ]; then
- echo "Cleaning up workspace" > $TEST_log
+ log_info "Cleaning up workspace" >> $TEST_log
cd ${WORKSPACE_DIR}
- bazel clean >& $TEST_log # Clean up the output base
+ bazel clean >> $TEST_log 2>&1 # Clean up the output base
for i in $(ls); do
if ! is_tools_directory "$i"; then
@@ -469,8 +493,8 @@ function cleanup() {
break
fi
if (( i == 10 )) || (( i == 30 )) || (( i == 60 )) ; then
- echo "Test cleanup: couldn't delete ${BAZEL_INSTALL_BASE} after $i seconds"
- echo "(Timeout in $((120-i)) seconds.)"
+ log_info "Test cleanup: couldn't delete ${BAZEL_INSTALL_BASE} after $i seconds" \
+ "(Timeout in $((120-i)) seconds.)"
fi
sleep 1
done
@@ -496,7 +520,7 @@ function assert_build_output() {
}
function assert_build_fails() {
- bazel build -s $1 >& $TEST_log \
+ bazel build -s $1 >> $TEST_log 2>&1 \
&& fail "Test $1 succeed while expecting failure" \
|| true
if [ -n "${2:-}" ]; then
@@ -505,24 +529,24 @@ function assert_build_fails() {
}
function assert_test_ok() {
- bazel test --test_output=errors $* >& $TEST_log \
+ bazel test --test_output=errors $* >> $TEST_log 2>&1 \
|| fail "Test $1 failed while expecting success"
}
function assert_test_fails() {
- bazel test --test_output=errors $* >& $TEST_log \
+ bazel test --test_output=errors $* >> $TEST_log 2>&1 \
&& fail "Test $* succeed while expecting failure" \
|| true
expect_log "$1.*FAILED"
}
function assert_binary_run() {
- $1 >& $TEST_log || fail "Failed to run $1"
+ $1 >> $TEST_log 2>&1 || fail "Failed to run $1"
[ -z "${2:-}" ] || expect_log "$2"
}
function assert_bazel_run() {
- bazel run $1 >& $TEST_log || fail "Failed to run $1"
+ bazel run $1 >> $TEST_log 2>&1 || fail "Failed to run $1"
[ -z "${2:-}" ] || expect_log "$2"
assert_binary_run "./bazel-bin/$(echo "$1" | sed 's|^//||' | sed 's|:|/|')" "${2:-}"