From b74cdfb8268aa2ad5dfc2b792cdef897a6e973ba Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 11 May 2019 14:14:12 -0400 Subject: [PATCH] Add strict mode to lib/commands/version_commands.sh --- lib/commands/version_commands.sh | 16 ++++++++++------ test/version_commands.bats | 30 ++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/commands/version_commands.sh b/lib/commands/version_commands.sh index c4690633..db76f839 100644 --- a/lib/commands/version_commands.sh +++ b/lib/commands/version_commands.sh @@ -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\"";; * ) diff --git a/test/version_commands.bats b/test/version_commands.bats index 2c083a4f..06dacea8 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -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" {