From b7dd291c983af321af20550fa89bf1cfbc888aec Mon Sep 17 00:00:00 2001 From: Joe Horsnell Date: Wed, 19 Jan 2022 22:00:53 +0000 Subject: [PATCH] fix: Prevent unbound variable error with nounset in asdf.sh (#1158) `asdf` [v0.9.0][1] included a [bug fix][2] to not override an existing ASDF_DIR. However, if `ASDF_DIR` is not set at all, then this causes an error when using bash `set -u`, or `set -o nounset` - see [here][3] for additional info. [1]: https://github.com/asdf-vm/asdf/releases/tag/v0.9.0 [2]: https://github.com/asdf-vm/asdf/pull/1008 [3]: https://mywiki.wooledge.org/BashFAQ/112 --- asdf.sh | 2 +- test/asdf_sh.bats | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/asdf.sh b/asdf.sh index 4d7b2aa5..7a01f5e8 100644 --- a/asdf.sh +++ b/asdf.sh @@ -3,7 +3,7 @@ # 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 [ -z "$ASDF_DIR" ]; then +if [ -z "${ASDF_DIR:-}" ]; then if [ -n "${BASH_SOURCE[0]}" ]; then current_script_path="${BASH_SOURCE[0]}" elif [[ "$_under" == *".sh" ]]; then diff --git a/test/asdf_sh.bats b/test/asdf_sh.bats index 02ff44a6..9e06e154 100644 --- a/test/asdf_sh.bats +++ b/test/asdf_sh.bats @@ -26,6 +26,22 @@ cleaned_path() { [ "$output" != "" ] } +@test "does not error if nounset is enabled" { + result=$( + unset -f asdf + unset ASDF_DIR + PATH=$(cleaned_path) + set -o nounset + + source_asdf_sh + echo $ASDF_DIR + ) + + output=$(echo "$result" | grep "asdf") + [ "$?" -eq 0 ] + [ "$output" != "" ] +} + @test "adds asdf dirs to PATH" { result=$( unset -f asdf