From cf446296fb76320fae3c0fbfa7d8006c7ddb5999 Mon Sep 17 00:00:00 2001 From: John Szumski <784312+jszumski@users.noreply.github.com> Date: Fri, 27 Jan 2023 07:45:10 -0500 Subject: [PATCH] Improves portability of buildifier_test (#1128) --- buildifier/runner.bash.template | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/buildifier/runner.bash.template b/buildifier/runner.bash.template index 4b5c204a5..2ddbf3ff4 100644 --- a/buildifier/runner.bash.template +++ b/buildifier/runner.bash.template @@ -11,10 +11,24 @@ buildifier_short_path=$(readlink "$BUILDIFIER_SHORT_PATH") if [[ ! -z "${TEST_WORKSPACE+x}" && -z "${BUILD_WORKSPACE_DIRECTORY+x}" ]]; then FIND_FILE_TYPE="l" # If WORKSPACE was provided, then the script is being run under a test in no_sandbox mode - # cd to the directory containing the WORKSPACE file if [[ ! -z "${WORKSPACE+x}" ]]; then FIND_FILE_TYPE="f" - WORKSPACE_PATH="$(dirname "$(realpath ${WORKSPACE})")" + + # resolve the WORKSPACE symlink + # use `realpath` if available and `readlink` otherwise (typically macOS) + if command -v realpath &> /dev/null; then + WORKSPACE_LINK="$(realpath ${WORKSPACE})" + elif command -v readlink &> /dev/null; then + WORKSPACE_LINK="$(readlink ${WORKSPACE})" + else + echo "Unable to resolve symlink (WORKSPACE: ${WORKSPACE})" + exit 1 + fi + + # Find the directory containing the WORKSPACE file + WORKSPACE_PATH="$(dirname "$WORKSPACE_LINK")" + + # Change the working directory to the WORKSPACE parent if ! cd "$WORKSPACE_PATH" ; then echo "Unable to change to workspace (WORKSPACE_PATH: ${WORKSPACE_PATH})" fi