From 72630a0b10e82b54830be7c2a4b0449e19cbe331 Mon Sep 17 00:00:00 2001 From: Daniel Perez Date: Tue, 2 Aug 2016 16:06:58 +0900 Subject: [PATCH] Improve current command. * Allow to be called without arguments * Fix error in path display * Add --short option --- help.txt | 2 +- lib/commands/current.sh | 54 +++++++++++++++++++++++++++++---------- lib/utils.sh | 7 +++-- test/current_command.bats | 18 ++++++++++--- test/utils.bats | 34 ++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 20 deletions(-) diff --git a/help.txt b/help.txt index b58ed1a8..2b8d209f 100644 --- a/help.txt +++ b/help.txt @@ -11,7 +11,7 @@ MANAGE PACKAGES with no arguments, install all the package versions listed in the .tool-versions file asdf uninstall Remove a specific version of a package - asdf current Display current version set or being used for package + asdf current [--short] [] Display current version set or being used asdf where Display install path for an installed version asdf local Set the package local version asdf global Set the package global version diff --git a/lib/commands/current.sh b/lib/commands/current.sh index d7359a04..0e45db81 100644 --- a/lib/commands/current.sh +++ b/lib/commands/current.sh @@ -1,20 +1,46 @@ current_command() { - local plugin_name=$1 - local version=$(get_preset_version_for $plugin_name) + local exit_code=0 + local short=false - check_if_plugin_exists $plugin_name + local plugin_names="" - if [ "$version" == "" ]; then - echo "No version set for $plugin_name" - exit 1 - else - local version_file_path=$(get_version_file_path_for $plugin_name) - if [ "$version_file_path" == "" ]; then - echo "$version" - else - echo "$version (set by $version_file_path)" - fi + while [ $# -gt 0 ] + do + case "$1" in + --short) short=true ;; + -*) ;; + *) + plugin_names="$1" ;; + esac + shift + done - exit 0 + + if [ -z "$plugin_names" ]; then + read -a plugin_names <<< $(plugin_list_command) fi + + for plugin_name in "${plugin_names[@]}"; do + local version=$(get_preset_version_for $plugin_name) + + check_if_plugin_exists $plugin_name + + if [ "$version" == "" ]; then + echo "No version set for $plugin_name" + exit_code=1 + else + if [ "$short" = true ]; then + echo "$version" + else + local version_file_path=$(get_version_file_path_for $plugin_name) + if [ "$version_file_path" == "" ]; then + echo "$plugin_name $version" + else + echo "$plugin_name $version (set by $version_file_path)" + fi + fi + fi + done + + exit $exit_code } diff --git a/lib/utils.sh b/lib/utils.sh index 727ad66b..91983297 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -80,15 +80,18 @@ get_version_file_path_for() { fi fi - echo $(get_asdf_versions_file_path) + echo $(get_asdf_versions_file_path $tool_name) } get_asdf_versions_file_path() { + local tool_name=$1 local asdf_tool_versions_path="" local search_path=$(pwd) while [ "$search_path" != "/" ]; do - if [ -f "$search_path/.tool-versions" ]; then + local file="$search_path/.tool-versions" + if [ -f $file ] && \ + ( [ -z "$tool_name" ] || grep -q "$tool_name" "$file" ); then asdf_tool_versions_path="$search_path/.tool-versions" break fi diff --git a/test/current_command.bats b/test/current_command.bats index 562baa4b..08e25bc9 100644 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -2,6 +2,7 @@ load test_helpers +. $(dirname $BATS_TEST_DIRNAME)/lib/commands/plugin-list.sh . $(dirname $BATS_TEST_DIRNAME)/lib/commands/current.sh setup() { @@ -11,8 +12,10 @@ setup() { PROJECT_DIR=$HOME/project OTHER_DIR=$HOME/other mkdir -p $ASDF_DIR/installs/dummy/1.0.0 $ASDF_DIR/installs/dummy/1.1.0 $PROJECT_DIR $OTHER_DIR + mkdir -p $ASDF_DIR/installs/other/1.0.0 $ASDF_DIR/plugins/other echo 'dummy 1.0.0' >> $HOME/.tool-versions + echo 'other 1.0.0' >> $HOME/.tool-versions echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions echo '1.2.0' >> $OTHER_DIR/.dummy-version } @@ -21,12 +24,21 @@ teardown() { clean_asdf_dir } +@test "current without arguments should print all versions" { + cd $PROJECT_DIR + + run current_command + expected=$(echo -e "dummy 1.1.0 (set by $PROJECT_DIR/.tool-versions)\nother 1.0.0 (set by $HOME/.tool-versions)") + [ "$status" -eq 0 ] + [ "$output" = "$expected" ] +} + @test "current should derive from the local .tool_versions when it exists" { cd $PROJECT_DIR run current_command "dummy" [ "$status" -eq 0 ] - [ "$output" = "1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] + [ "$output" = "dummy 1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] } @test "current should derive from the global .tool_versions when local doesn't exist" { @@ -34,7 +46,7 @@ teardown() { run current_command "dummy" [ "$status" -eq 0 ] - [ "$output" = "1.0.0 (set by $HOME/.tool-versions)" ] + [ "$output" = "dummy 1.0.0 (set by $HOME/.tool-versions)" ] } @test "current should derive from the legacy file if enabled and hide the file path" { @@ -43,7 +55,7 @@ teardown() { run current_command "dummy" [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] + [ "$output" = "dummy 1.2.0" ] } @test "current should error when the plugin doesn't exist" { diff --git a/test/utils.bats b/test/utils.bats index d8489cdf..a080f05e 100644 --- a/test/utils.bats +++ b/test/utils.bats @@ -4,6 +4,14 @@ load test_helpers setup() { setup_asdf_dir + install_dummy_plugin + + PROJECT_DIR=$HOME/project + mkdir -p $PROJECT_DIR + + echo 'dummy 1.0.0' >> $HOME/.tool-versions + echo 'other 1.0.0' >> $HOME/.tool-versions + echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions } teardown() { @@ -43,3 +51,29 @@ teardown() { [ "$status" -eq 0 ] [ "$output" = "" ] } + +@test "get_asdf_versions_file_path should return closest .tool-versions when no args provided" { + cd $PROJECT_DIR + + run get_asdf_versions_file_path + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +} + +@test "get_asdf_versions_file_path should return closest .tool-versions if contains plugin" { + cd $PROJECT_DIR + + run get_asdf_versions_file_path "dummy" + + [ "$status" -eq 0 ] + [ "$output" = "$PROJECT_DIR/.tool-versions" ] +} + +@test "get_asdf_versions_file_path should return correct .tool-versions if closest does not contain plugin" { + cd $PROJECT_DIR + + run get_asdf_versions_file_path "other" + + [ "$status" -eq 0 ] + [ "$output" = "$HOME/.tool-versions" ] +}