diff --git a/lib/utils.sh b/lib/utils.sh index e0438c1d..ffe23586 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -46,7 +46,7 @@ list_installed_versions() { check_if_plugin_exists() { # Check if we have a non-empty argument if [ -z "${1+set}" ]; then - display_error "No such plugin" + display_error "No plugin given" exit 1 fi @@ -57,7 +57,7 @@ check_if_plugin_exists() { } check_if_version_exists() { - local plugin=$1 + local plugin_name=$1 local version=$2 local version_dir=$(asdf_dir)/installs/$plugin/$version @@ -66,8 +66,10 @@ check_if_version_exists() { version_dir=$(echo $version | cut -d: -f 2) fi + check_if_plugin_exists $plugin_name + if [ $version != "system" -a ! -d $version_dir ]; then - display_error "version $version is not installed for $plugin" + display_error "version $version is not installed for $plugin_name" exit 1 fi } @@ -173,6 +175,8 @@ get_executable_path() { local version=$2 local executable_path=$3 + check_if_version_exists $plugin_name $version + if [ $version = "system" ]; then path=$(echo $PATH | sed -e "s|$(asdf_dir)/shims:\?||g") cmd=$(basename $executable_path) @@ -226,29 +230,29 @@ get_preset_version_for() { } get_asdf_config_value_from_file() { - local config_path=$1 - local key=$2 + local config_path=$1 + local key=$2 - if [ ! -f $config_path ]; then - return 0 - fi + if [ ! -f $config_path ]; then + return 0 + fi - local result=$(grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }') - if [ -n "$result" ]; then - echo $result - fi + local result=$(grep -E "^\s*$key\s*=" $config_path | awk -F '=' '{ gsub(/ /, "", $2); print $2 }') + if [ -n "$result" ]; then + echo $result + fi } get_asdf_config_value() { - local key=$1 - local config_path=${AZDF_CONFIG_FILE:-"$HOME/.asdfrc"} - local default_config_path=${AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"} + local key=$1 + local config_path=${AZDF_CONFIG_FILE:-"$HOME/.asdfrc"} + local default_config_path=${AZDF_CONFIG_DEFAULT_FILE:-"$(asdf_dir)/defaults"} - local result=$(get_asdf_config_value_from_file $config_path $key) + local result=$(get_asdf_config_value_from_file $config_path $key) - if [ -n "$result" ]; then - echo $result - else - get_asdf_config_value_from_file $default_config_path $key - fi + if [ -n "$result" ]; then + echo $result + else + get_asdf_config_value_from_file $default_config_path $key + fi } diff --git a/test/test_helpers.bash b/test/test_helpers.bash index f220e3d4..d87beaa9 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -6,6 +6,8 @@ setup_asdf_dir() { ASDF_DIR=$HOME/.asdf mkdir -p $ASDF_DIR/plugins mkdir -p $ASDF_DIR/installs + mkdir -p $ASDF_DIR/shims + PATH=$ASDF_DIR/shims:$PATH } install_dummy_plugin() { diff --git a/test/utils.bats b/test/utils.bats index af9f581f..16b92a5a 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -34,10 +34,17 @@ teardown() { [ "$output" = "" ] } +@test "check_if_version_exists should be noop if version is system" { + mkdir -p $ASDF_DIR/plugins/foo + run check_if_version_exists "foo" "system" + [ "$status" -eq 0 ] + [ "$output" = "" ] +} + @test "check_if_plugin_exists should exit with 1 when plugin is empty string" { run check_if_plugin_exists [ "$status" -eq 1 ] - [ "$output" = "No such plugin" ] + [ "$output" = "No plugin given" ] } @test "check_if_plugin_exists should be noop if plugin exists" { @@ -139,3 +146,34 @@ teardown() { [ "$status" -eq 0 ] [ "$output" = "path:/some/place with spaces" ] } + +@test "get_executable_path for system version should return system path" { + mkdir -p $ASDF_DIR/plugins/foo + run get_executable_path "foo" "system" "ls" + [ "$status" -eq 0 ] + [ "$output" = $(which ls) ] +} + +@test "get_executable_path for system version should not use asdf shims" { + mkdir -p $ASDF_DIR/plugins/foo + touch $ASDF_DIR/shims/dummy_executable + chmod +x $ASDF_DIR/shims/dummy_executable + + run which dummy_executable + [ "$status" -eq 0 ] + + run get_executable_path "foo" "system" "dummy_executable" + [ "$status" -eq 1 ] +} + +@test "get_executable_path for non system version should return relative path from plugin" { + mkdir -p $ASDF_DIR/plugins/foo + mkdir -p $ASDF_DIR/installs/foo/1.0.0/bin + executable_path=$ASDF_DIR/installs/foo/1.0.0/bin/dummy + touch $executable_path + chmod +x $executable_path + + run get_executable_path "foo" "1.0.0" "bin/dummy" + [ "$status" -eq 0 ] + [ "$output" = "$executable_path" ] +}