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

View File

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