Add strict mode to lib/commands/version_commands.sh

This commit is contained in:
Trevor Brown 2019-05-11 14:14:12 -04:00
parent bb9055025f
commit b74cdfb826
2 changed files with 32 additions and 14 deletions

View File

@ -1,3 +1,6 @@
set -o nounset -o pipefail -o errexit
IFS=$'\t\n' # Stricter IFS settings
version_command() {
local cmd=$1
local plugin=$2
@ -12,7 +15,8 @@ version_command() {
fi
shift 2
local versions=("$@")
local versions
IFS=$' ' versions=("$@")
local file
if [ "$cmd" = "global" ]; then
@ -37,10 +41,10 @@ version_command() {
if [ -f "$file" ] && grep "^$plugin " "$file" > /dev/null; then
sed -i.bak -e "s/^$plugin .*$/$plugin ${versions[*]}/" "$file"
IFS=$' ' sed -i.bak -e "s/^$plugin .*$/$plugin ${versions[*]}/" "$file"
rm "$file".bak
else
echo "$plugin ${versions[*]}" >> "$file"
IFS=$' ' echo "$plugin ${versions[*]}" >> "$file"
fi
}
@ -86,8 +90,8 @@ shell_command() {
exit 1
fi
local plugin=$1
local version=$2
local plugin=${1:-}
local version=${2:-}
if ! (check_if_version_exists "$plugin" "$version"); then
echo 'false'
@ -98,7 +102,7 @@ shell_command() {
upcase_name=$(echo "$plugin" | tr '[:lower:]-' '[:upper:]_')
local version_env_var="ASDF_${upcase_name}_VERSION"
case $ASDF_SHELL in
case ${ASDF_SHELL:-} in
fish )
echo "set -gx $version_env_var \"$version\"";;
* )

View File

@ -23,6 +23,10 @@ teardown() {
clean_asdf_dir
}
source_asdf_sh() {
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
}
# Warn users who invoke the old style command without arguments.
@test "local should emit an error when called with incorrect arity" {
run asdf local "dummy"
@ -232,17 +236,27 @@ teardown() {
}
@test "shell wrapper function should export ENV var" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "dummy" "1.1.0"
[ $(echo $ASDF_DUMMY_VERSION) = "1.1.0" ]
unset ASDF_DUMMY_VERSION
result=$(
set +euo > /dev/null
unset -f asdf
unset ASDF_DIR
source_asdf_sh
asdf shell "dummy" "1.1.0"
echo $ASDF_DUMMY_VERSION
)
[ "$result" = "1.1.0" ]
}
@test "shell wrapper function should return an error for missing plugins" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
run asdf shell "nonexistent" "1.0.0"
[ "$status" -eq 1 ]
[ "$output" = "No such plugin: nonexistent" ]
result=$(
set +euo > /dev/null
unset -f asdf
unset ASDF_DIR
source_asdf_sh
asdf shell "nonexistent" "1.0.0" 2>&1
echo "; exit status: $?"
)
[ "$result" = $'No such plugin: nonexistent\n; exit status: 1' ]
}
@test "shell should emit an error when wrapper function is not loaded" {