shell,tests: use Bash runfiles library in one test

See https://github.com/bazelbuild/bazel/issues/4930

Change-Id: I148c0b1e4baa8ff44d86a6ee196bea7e9058320f

Closes #5387.

Change-Id: Iba32f21ff6cad1b538c72cfd08ce24846843c124
PiperOrigin-RevId: 200554084
diff --git a/src/test/shell/bazel/BUILD b/src/test/shell/bazel/BUILD
index 72abc3d..450b3c6 100644
--- a/src/test/shell/bazel/BUILD
+++ b/src/test/shell/bazel/BUILD
@@ -537,7 +537,10 @@
     name = "help_test",
     size = "small",
     srcs = ["help_test.sh"],
-    data = [":test-deps"],
+    data = [
+        ":test-deps",
+        "@bazel_tools//tools/bash/runfiles",
+    ],
 )
 
 sh_test(
@@ -584,6 +587,7 @@
     tests = [
         ":bazel_bootstrap_distfile_test",
         ":bazel_windows_example_test",
+        ":help_test",
     ],
     visibility = ["//src/test/shell:__pkg__"],
 )
diff --git a/src/test/shell/bazel/help_test.sh b/src/test/shell/bazel/help_test.sh
index 3e25703..3f81132 100755
--- a/src/test/shell/bazel/help_test.sh
+++ b/src/test/shell/bazel/help_test.sh
@@ -16,9 +16,29 @@
 #
 # Test the help command.
 
-# Load the test setup defined in the parent directory
-CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-source "${CURRENT_DIR}/../integration_test_setup.sh" \
+set -euo pipefail
+# --- begin runfiles.bash initialization ---
+if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+    if [[ -f "$0.runfiles_manifest" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+    elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+      export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+    elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+      export RUNFILES_DIR="$0.runfiles"
+    fi
+fi
+if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+  source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+  source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+            "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+  echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+  exit 1
+fi
+# --- end runfiles.bash initialization ---
+
+source "$(rlocation "io_bazel/src/test/shell/integration_test_setup.sh")" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
 function test_info_keys() {
diff --git a/src/test/shell/integration_test_setup.sh b/src/test/shell/integration_test_setup.sh
index ba051d4..1b68adc 100755
--- a/src/test/shell/integration_test_setup.sh
+++ b/src/test/shell/integration_test_setup.sh
@@ -19,13 +19,26 @@
   echo $1 >&2; exit 1;
 }
 
-CURRENT_SCRIPT=${BASH_SOURCE[0]}
-# Go to the directory where the script is running
-cd "$(dirname ${CURRENT_SCRIPT})" \
-  || print_message_and_exit "Unable to access $(dirname ${CURRENT_SCRIPT})"
+if type rlocation >&/dev/null; then
+  # If rlocation is defined, use it to look up data-dependencies.
+  # Load the unit test framework
+  source "$(rlocation io_bazel/src/test/shell/unittest.bash)" \
+    || print_message_and_exit "unittest.bash not found!"
+  # Load the test environment
+  source "$(rlocation io_bazel/src/test/shell/testenv.sh)" \
+    || print_message_and_exit "testenv.sh not found!"
+else
+  # If rlocation is undefined, we are probably running under Blaze.
+  # Assume the existence of a runfiles tree.
 
-DIR=$(pwd)
-# Load the unit test framework
-source "$DIR/unittest.bash" || print_message_and_exit "unittest.bash not found!"
-# Load the test environment
-source "$DIR/testenv.sh" || print_message_and_exit "testenv.sh not found!"
+  CURRENT_SCRIPT=${BASH_SOURCE[0]}
+  # Go to the directory where the script is running
+  cd "$(dirname ${CURRENT_SCRIPT})" \
+    || print_message_and_exit "Unable to access $(dirname ${CURRENT_SCRIPT})"
+
+  DIR=$(pwd)
+  # Load the unit test framework
+  source "$DIR/unittest.bash" || print_message_and_exit "unittest.bash not found!"
+  # Load the test environment
+  source "$DIR/testenv.sh" || print_message_and_exit "testenv.sh not found!"
+fi
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index 805304a..cbafb2b 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -83,7 +83,6 @@
 distdir_bzl_file="${BAZEL_RUNFILES}/distdir.bzl"
 
 # Bazel
-bazel_tree="$(rlocation io_bazel/src/test/shell/bazel/doc-srcs.zip)"
 bazel_data="${BAZEL_RUNFILES}"
 
 # Java
@@ -400,6 +399,10 @@
   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")"
+  # Shut down this server in case the tests will run Bazel in a different output
+  # root, otherwise we could not clean up $WORKSPACE_DIR (under $TEST_TMPDIR)
+  # once the test is finished.
+  bazel shutdown
   rm -f "$bazel_stdout" "$bazel_stderr"
 
   if is_windows; then
@@ -414,6 +417,8 @@
     log_info "Cleaning up workspace" >> $TEST_log
     cd ${WORKSPACE_DIR}
     bazel clean >> $TEST_log 2>&1 # Clean up the output base
+    # Shut down this server to allow any cleanup code to delete its output_root.
+    bazel shutdown
 
     for i in *; do
       if ! is_tools_directory "$i"; then
@@ -501,7 +506,6 @@
 }
 
 setup_bazelrc
-setup_clean_workspace
 
 ################### shell/integration/testenv ############################
 # Setting up the environment for our legacy integration tests.