Add the ability to force the path to the test binary to a fixed length by using a symlink.

This is enabled with --test_env=TEST_SHORT_EXEC_PATH=true.

Passing CI run: https://buildkite.com/bazel/google-bazel-presubmit/builds/3576

RELNOTES: N/A
PiperOrigin-RevId: 200050318
diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh
index b403513..fda8f00 100755
--- a/tools/test/test-setup.sh
+++ b/tools/test/test-setup.sh
@@ -32,6 +32,11 @@
   [[ "$1" = /* ]] || [[ "$1" =~ ^[a-zA-Z]:[/\\].* ]]
 }
 
+# The original execution root. Usually this script changes directory into the
+# runfiles directory, so using $PWD is not a reliable way to find the execution
+# root.
+EXEC_ROOT="$PWD"
+
 # Bazel sets some environment vars to relative paths to improve caching and
 # support remote execution, where the absolute path may not be known to Bazel.
 # Convert them to absolute paths here before running the actual test.
@@ -213,6 +218,20 @@
   TEST_PATH="$(rlocation $TEST_WORKSPACE/$EXE)"
 fi
 
+# TODO(jsharpe): Use --test_env=TEST_SHORT_EXEC_PATH=true to activate this code
+# path to workaround a bug with long executable paths when executing remote
+# tests on Windows.
+if [ ! -z "$TEST_SHORT_EXEC_PATH" ]; then
+  # Use a short path like "t0" in the execution root. Use the smallest numeric
+  # suffix that doesn't collide with an existing file or directory.
+  QUALIFIER=0
+  while [[ -e "${EXEC_ROOT}/t${QUALIFIER}" ]]; do
+    ((QUALIFIER++))
+  done
+  ln -s "${TEST_PATH}" "${EXEC_ROOT}/t${QUALIFIER}"
+  TEST_PATH="${EXEC_ROOT}/t${QUALIFIER}"
+fi
+
 exitCode=0
 signals="$(trap -l | sed -E 's/[0-9]+\)//g')"
 for signal in $signals; do