From 05f4cce44f1793b1a783c9d546a6d1f086ad0d28 Mon Sep 17 00:00:00 2001 From: Eric Nielsen Date: Fri, 23 Jul 2021 17:28:06 -0500 Subject: [PATCH] fix: don't override existing ASDF_DIR allowing users to define its value, e.g. when installed with homebrew. Also unset locally used variables at the end. --- asdf.sh | 20 ++++++++++++-------- test/version_commands.bats | 3 +++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/asdf.sh b/asdf.sh index 7450c183b..ff6ecfd91 100644 --- a/asdf.sh +++ b/asdf.sh @@ -3,16 +3,18 @@ # For Bash, ${BASH_SOURCE[0]} will be used to obtain this script's path. # For Zsh and others, $0 (the path to the shell or script) will be used. _under="$_" -if [ "${BASH_SOURCE[0]}" != "" ]; then - current_script_path="${BASH_SOURCE[0]}" -elif [[ "$_under" == *".sh" ]]; then - current_script_path="$_under" -else - current_script_path="$0" -fi +if [ -z "$ASDF_DIR" ]; then + if [ -n "${BASH_SOURCE[0]}" ]; then + current_script_path="${BASH_SOURCE[0]}" + elif [[ "$_under" == *".sh" ]]; then + current_script_path="$_under" + else + current_script_path="$0" + fi + ASDF_DIR="$(dirname "$current_script_path")" +fi export ASDF_DIR -ASDF_DIR="$(dirname "$current_script_path")" # shellcheck disable=SC2016 [ -d "$ASDF_DIR" ] || echo '$ASDF_DIR is not a directory' @@ -31,3 +33,5 @@ PATH="${ASDF_USER_SHIMS}:$PATH" # shellcheck source=lib/asdf.sh # Load the asdf wrapper function . "${ASDF_DIR}/lib/asdf.sh" + +unset _under current_script_path ASDF_BIN ASDF_USER_SHIMS diff --git a/test/version_commands.bats b/test/version_commands.bats index 35fcb7a3a..a0aef1fe7 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -21,6 +21,9 @@ setup() { mkdir -p $CHILD_DIR cd $PROJECT_DIR + + # asdf lib needed to run asdf.sh + cp -rf $BATS_TEST_DIRNAME/../{bin,lib} $ASDF_DIR/ } teardown() {