asdf/test/asdf_sh.bats
Joe Horsnell b7dd291c98
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
2022-01-19 17:00:53 -05:00

102 lines
1.7 KiB
Bash

#!/usr/bin/env bats
load test_helpers
# Helper function to handle sourcing of asdf.sh
source_asdf_sh() {
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
}
cleaned_path() {
echo $PATH | tr ':' '\n' | grep -v "asdf" | tr '\n' ':'
}
@test "exports ASDF_DIR" {
result=$(
unset -f asdf
unset ASDF_DIR
PATH=$(cleaned_path)
source_asdf_sh
echo $ASDF_DIR
)
output=$(echo "$result" | grep "asdf")
[ "$?" -eq 0 ]
[ "$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
unset ASDF_DIR
PATH=$(cleaned_path)
source_asdf_sh
echo $PATH
)
output=$(echo "$result" | grep "asdf")
[ "$?" -eq 0 ]
[ "$output" != "" ]
}
@test "does not add paths to PATH more than once" {
result=$(
unset -f asdf
unset ASDF_DIR
PATH=$(cleaned_path)
source_asdf_sh
source_asdf_sh
echo $PATH
)
output=$(echo $PATH | tr ':' '\n' | grep "asdf" | sort | uniq -d)
[ "$?" -eq 0 ]
[ "$output" = "" ]
}
@test "defines the asdf function" {
output=$(
unset -f asdf
unset ASDF_DIR
PATH=$(cleaned_path)
source_asdf_sh
type asdf
)
[[ "$output" =~ "is a function" ]]
}
@test "function calls asdf command" {
result=$(
unset -f asdf
ASDF_DIR=$(pwd)
PATH=$(cleaned_path)
source_asdf_sh
asdf info
)
[ "$?" -eq 0 ]
output=$(echo "$result" | grep "ASDF INSTALLED PLUGINS:")
[ "$output" != "" ]
}