From ae31774c69a598badfb2265d7816b166f5508f20 Mon Sep 17 00:00:00 2001 From: rafaelliu Date: Wed, 6 Jun 2018 18:37:57 -0700 Subject: [PATCH 01/10] Fixes #333, there was a unnecessary cd issued (typo?) within a $ subshell on asdf.sh --- asdf.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asdf.sh b/asdf.sh index 18591b79..3622efc1 100644 --- a/asdf.sh +++ b/asdf.sh @@ -7,7 +7,7 @@ else fi export ASDF_DIR -ASDF_DIR="$(cd "$(dirname "$current_script_path")" &> /dev/null || exit 1; pwd)" +ASDF_DIR="$(dirname "$current_script_path" &> /dev/null || exit 1; pwd)" # Add asdf to PATH # From fb4fc79be7b15cd1106bf2eb43fa5af15fa76358 Mon Sep 17 00:00:00 2001 From: rafaelliu Date: Wed, 6 Jun 2018 19:06:13 -0700 Subject: [PATCH 02/10] Adding back check if directory exists --- asdf.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/asdf.sh b/asdf.sh index 3622efc1..7faf32d9 100644 --- a/asdf.sh +++ b/asdf.sh @@ -7,7 +7,8 @@ else fi export ASDF_DIR -ASDF_DIR="$(dirname "$current_script_path" &> /dev/null || exit 1; pwd)" +ASDF_DIR="$(dirname "$current_script_path")" +[ -d "$ASDF_DIR" ] || return # Add asdf to PATH # From feaebe53dfce1978d984e43bbf4068ddaedb1e35 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 16 Jun 2018 20:19:33 -0400 Subject: [PATCH 03/10] Add readlink to list of banned commands. --- test/banned_commands.bats | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/banned_commands.bats b/test/banned_commands.bats index 5ef7379a..f15aa8e1 100644 --- a/test/banned_commands.bats +++ b/test/banned_commands.bats @@ -2,7 +2,14 @@ load test_helpers -banned_commands=(realpath eval) +banned_commands=( + realpath + # readlink on OSX behaves differently from readlink on other Unix systems + readlink + # It's best to avoid eval as it makes it easier to accidentally execute + # arbitrary strings + eval + ) setup() { setup_asdf_dir From f7b71ba86a57783e91a53da8373d7e967f795597 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 16 Jun 2018 22:48:16 -0400 Subject: [PATCH 04/10] Add tests for symlink preservation. --- test/version_commands.bats | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/version_commands.bats b/test/version_commands.bats index f86587f0..4c834774 100644 --- a/test/version_commands.bats +++ b/test/version_commands.bats @@ -139,3 +139,47 @@ teardown() { [ "$(cat $ASDF_DEFAULT_TOOL_VERSIONS_FILENAME)" = "dummy 1.1.0" ] [ "$(cat $HOME/.tool-versions)" = "" ] } + +@test "local should preserve symlinks when setting versions" { + mkdir other-dir + touch other-dir/.tool-versions + ln -s other-dir/.tool-versions .tool-versions + run local_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ -L .tool-versions ] + [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ] +} + +@test "local should preserve symlinks when updating versions" { + mkdir other-dir + touch other-dir/.tool-versions + ln -s other-dir/.tool-versions .tool-versions + run local_command "dummy" "1.1.0" + run local_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ -L .tool-versions ] + [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ] +} + +@test "global should preserve symlinks when setting versions" { + mkdir other-dir + touch other-dir/.tool-versions + ln -s other-dir/.tool-versions $HOME/.tool-versions + + run global_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ -L $HOME/.tool-versions ] + [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ] +} + +@test "global should preserve symlinks when updating versions" { + mkdir other-dir + touch other-dir/.tool-versions + ln -s other-dir/.tool-versions $HOME/.tool-versions + + run global_command "dummy" "1.1.0" + run global_command "dummy" "1.1.0" + [ "$status" -eq 0 ] + [ -L $HOME/.tool-versions ] + [ "$(cat other-dir/.tool-versions)" = "dummy 1.1.0" ] +} From 34ad91153956918b6ccb8eeb43e14b09c99e645b Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 16 Jun 2018 22:59:37 -0400 Subject: [PATCH 05/10] Resolve symlink paths before altering .tool-version files. --- lib/commands/version_commands.sh | 6 ++++++ lib/utils.sh | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/commands/version_commands.sh b/lib/commands/version_commands.sh index 55aa7068..1a0ce7c6 100644 --- a/lib/commands/version_commands.sh +++ b/lib/commands/version_commands.sh @@ -17,6 +17,11 @@ version_command() { file="$(pwd)/.tool-versions" fi + if [ -L "$file" ]; then + # Resolve file path if symlink + file="$(resolve_symlink "$file")" + fi + check_if_plugin_exists "$plugin" local version @@ -24,6 +29,7 @@ version_command() { check_if_version_exists "$plugin" "$version" done + if [ -f "$file" ] && grep "^$plugin " "$file" > /dev/null; then sed -i.bak -e "s/^$plugin .*$/$plugin ${versions[*]}/" "$file" rm "$file".bak diff --git a/lib/utils.sh b/lib/utils.sh index bcb186dd..369840cc 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -354,3 +354,12 @@ find_tool_versions() { search_path=$(dirname "$search_path") done } + +resolve_symlink() { + local symlink + symlink="$1" + + # This seems to be the only cross-platform way to resolve symlink paths to + # the real file path + ls -l "$symlink" | sed -e "s|.*-> \(.*\)|\1|" +} From b88110469f4f2586049fc05f6b3ed88286e8fe31 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 16 Jun 2018 15:04:38 -0400 Subject: [PATCH 06/10] Fix shellcheck warnings. --- lib/commands/install.sh | 4 ++-- lib/commands/list.sh | 4 ++-- lib/commands/plugin-list-all.sh | 4 ++-- lib/commands/plugin-list.sh | 4 ++-- lib/commands/plugin-push.sh | 2 +- lib/commands/reshim.sh | 4 ++-- lib/utils.sh | 2 +- test/test_helpers.bash | 2 +- test/update_command.bats | 8 ++++---- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/commands/install.sh b/lib/commands/install.sh index 91b49d68..537bdfaf 100644 --- a/lib/commands/install.sh +++ b/lib/commands/install.sh @@ -25,9 +25,9 @@ install_command() { } get_concurrency() { - if which nproc > /dev/null 2>&1; then + if command -v nproc > /dev/null 2>&1; then nproc - elif which sysctl > /dev/null 2>&1 && sysctl hw.ncpu > /dev/null 2>&1; then + elif command -v sysctl > /dev/null 2>&1 && sysctl hw.ncpu > /dev/null 2>&1; then sysctl -n hw.ncpu elif [ -f /proc/cpuinfo ]; then grep -c processor /proc/cpuinfo diff --git a/lib/commands/list.sh b/lib/commands/list.sh index 656241a0..25683a82 100644 --- a/lib/commands/list.sh +++ b/lib/commands/list.sh @@ -6,7 +6,7 @@ list_command() { plugins_path=$(get_plugin_path) if ls "$plugins_path" &> /dev/null; then - for plugin_path in $plugins_path/* ; do + for plugin_path in "$plugins_path"/* ; do plugin_name=$(basename "$plugin_path") echo "$plugin_name" display_installed_versions "$plugin_name" @@ -32,4 +32,4 @@ display_installed_versions() { display_error 'No versions installed' exit 1 fi -} \ No newline at end of file +} diff --git a/lib/commands/plugin-list-all.sh b/lib/commands/plugin-list-all.sh index ee7bc244..83a4a1f0 100644 --- a/lib/commands/plugin-list-all.sh +++ b/lib/commands/plugin-list-all.sh @@ -8,12 +8,12 @@ plugin_list_all_command() { plugins_local_path="$(get_plugin_path)" if ls "$plugins_index_path" &> /dev/null; then - for index_plugin in $plugins_index_path/*; do + for index_plugin in "$plugins_index_path"/*; do index_plugin_name=$(basename "$index_plugin") source_url=$(get_plugin_source_url "$index_plugin_name") installed_flag="" - for local_plugin in $plugins_local_path/*; do + for local_plugin in "$plugins_local_path"/*; do local_plugin_name=$(basename "$local_plugin") [[ "$index_plugin_name" == "$local_plugin_name" ]] && installed_flag="*" done diff --git a/lib/commands/plugin-list.sh b/lib/commands/plugin-list.sh index cc277ca0..28913068 100644 --- a/lib/commands/plugin-list.sh +++ b/lib/commands/plugin-list.sh @@ -2,14 +2,14 @@ plugin_list_command() { local flag=$1 # 0 || 1 with flag - if [ $# -eq 0 ] || ([ $# -eq 1 ] && [ "$flag" == "--urls" ]); then + if [ $# -eq 0 ] || { [ $# -eq 1 ] && [ "$flag" = "--urls" ]; }; then # valid command local plugins_path plugins_path=$(get_plugin_path) if ls "$plugins_path" &> /dev/null; then - for plugin_path in $plugins_path/* ; do + for plugin_path in "$plugins_path"/* ; do plugin_name=$(basename "$plugin_path") if [ $# -eq 0 ]; then diff --git a/lib/commands/plugin-push.sh b/lib/commands/plugin-push.sh index 2cd9b184..7c257e24 100644 --- a/lib/commands/plugin-push.sh +++ b/lib/commands/plugin-push.sh @@ -1,7 +1,7 @@ plugin_push_command() { local plugin_name=$1 if [ "$plugin_name" = "--all" ]; then - for dir in $(asdf_dir)/plugins/*; do + for dir in "$(asdf_dir)"/plugins/*; do echo "Pushing $(basename "$dir")..." (cd "$dir" && git push) done diff --git a/lib/commands/reshim.sh b/lib/commands/reshim.sh index 02f1bc74..3b4ce37d 100644 --- a/lib/commands/reshim.sh +++ b/lib/commands/reshim.sh @@ -139,7 +139,7 @@ generate_shims_for_version() { install_path=$(get_install_path "$plugin_name" "$install_type" "$version") for bin_path in "${all_bin_paths[@]}"; do - for executable_file in $install_path/$bin_path/*; do + for executable_file in "$install_path/$bin_path"/*; do # because just $executable_file gives absolute path; We don't want version hardcoded in shim local executable_path_relative_to_install_path executable_path_relative_to_install_path="$bin_path"/$(basename "$executable_file") @@ -238,7 +238,7 @@ remove_shims_for_version() { IFS=' ' read -r -a all_bin_paths <<< "$space_separated_list_of_bin_paths" for bin_path in "${all_bin_paths[@]}"; do - for executable_file in $install_path/$bin_path/*; do + for executable_file in "$install_path/$bin_path"/*; do local executable_name executable_name="$(basename "$executable_file")" remove_shim_for_version "$plugin_name" "$executable_name" "$version" diff --git a/lib/utils.sh b/lib/utils.sh index 104eb034..bcb186dd 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -214,7 +214,7 @@ get_executable_path() { if [ "$version" = "system" ]; then path=$(echo "$PATH" | sed -e "s|$ASDF_DIR/shims||g; s|::|:|g") cmd=$(basename "$executable_path") - cmd_path=$(PATH=$path which "$cmd" 2>&1) + cmd_path=$(PATH=$path command -v "$cmd" 2>&1) # shellcheck disable=SC2181 if [ $? -ne 0 ]; then return 1 diff --git a/test/test_helpers.bash b/test/test_helpers.bash index 87dacd7d..9e8fae7a 100644 --- a/test/test_helpers.bash +++ b/test/test_helpers.bash @@ -1,7 +1,7 @@ #!/usr/bin/env bash # shellcheck source=lib/utils.sh -. $(dirname $BATS_TEST_DIRNAME)/lib/utils.sh +. "$(dirname "$BATS_TEST_DIRNAME")"/lib/utils.sh setup_asdf_dir() { BASE_DIR=$(mktemp -dt asdf.XXXX) diff --git a/test/update_command.bats b/test/update_command.bats index 228662a9..f952b07d 100644 --- a/test/update_command.bats +++ b/test/update_command.bats @@ -2,10 +2,10 @@ load test_helpers -. $(dirname $BATS_TEST_DIRNAME)/lib/commands/update.sh -. $(dirname $BATS_TEST_DIRNAME)/lib/commands/reshim.sh -. $(dirname $BATS_TEST_DIRNAME)/lib/commands/install.sh -. $(dirname $BATS_TEST_DIRNAME)/lib/commands/uninstall.sh +. $(dirname "$BATS_TEST_DIRNAME")/lib/commands/update.sh +. $(dirname "$BATS_TEST_DIRNAME")/lib/commands/reshim.sh +. $(dirname "$BATS_TEST_DIRNAME")/lib/commands/install.sh +. $(dirname "$BATS_TEST_DIRNAME")/lib/commands/uninstall.sh setup() { setup_asdf_dir From efa5c5c3e6f9a0b31842a77bd8f5358905d8a240 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 16 Jun 2018 23:07:17 -0400 Subject: [PATCH 07/10] Ignore shellcheck warning. --- lib/utils.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/utils.sh b/lib/utils.sh index 369840cc..a7d167bd 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -360,6 +360,7 @@ resolve_symlink() { symlink="$1" # This seems to be the only cross-platform way to resolve symlink paths to - # the real file path + # the real file path. + # shellcheck disable=SC2012 ls -l "$symlink" | sed -e "s|.*-> \(.*\)|\1|" } From 696f7b0f69199e5e61d14a10e6a1a0184109f6f0 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sun, 17 Jun 2018 19:31:36 -0400 Subject: [PATCH 08/10] Switch to single quotes for sed regex. --- lib/utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.sh b/lib/utils.sh index a7d167bd..53361c18 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -362,5 +362,5 @@ resolve_symlink() { # This seems to be the only cross-platform way to resolve symlink paths to # the real file path. # shellcheck disable=SC2012 - ls -l "$symlink" | sed -e "s|.*-> \(.*\)|\1|" + ls -l "$symlink" | sed -e 's|.*-> \(.*\)|\1|' } From 3a4b9042e39549c1837a21bf8df4c67576340343 Mon Sep 17 00:00:00 2001 From: Sasha Friedenberg Date: Wed, 11 Jul 2018 13:14:30 -0400 Subject: [PATCH 09/10] Fix issue with Fish shell shim loading script --- asdf.fish | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/asdf.fish b/asdf.fish index 09118ef3..569b7983 100644 --- a/asdf.fish +++ b/asdf.fish @@ -5,7 +5,9 @@ set -l asdf_dir (dirname (status -f)) # we get an ugly warning when setting the path if shims does not exist mkdir -p $asdf_dir/shims -if not contains $asdf_dir/{bin,shims} $PATH -and test -d $asdf/{bin,shims} - set -gx PATH $asdf_dir/{bin,shims} $PATH +for x in $asdf_dir/{bin,shims} + if not contains $x $PATH + and test -d $x + set -gx PATH $x $PATH + end end From 6d63496528910cde05b23f38462b59e40e178ff9 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Sat, 14 Jul 2018 09:47:33 -0400 Subject: [PATCH 10/10] Switch from `return` to `exit 1` when ASDF_DIR is not a dir. --- asdf.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asdf.sh b/asdf.sh index 7faf32d9..3908df01 100644 --- a/asdf.sh +++ b/asdf.sh @@ -8,7 +8,7 @@ fi export ASDF_DIR ASDF_DIR="$(dirname "$current_script_path")" -[ -d "$ASDF_DIR" ] || return +[ -d "$ASDF_DIR" ] || exit 1 # Add asdf to PATH #