diff --git a/lib/commands/current.sh b/lib/commands/current.sh index d7359a04..3e1cf210 100644 --- a/lib/commands/current.sh +++ b/lib/commands/current.sh @@ -1,20 +1,15 @@ current_command() { local plugin_name=$1 - local version=$(get_preset_version_for $plugin_name) check_if_plugin_exists $plugin_name + local version_file_path=$(find_version_file_for $plugin_name) + local version=$(parse_version_file $version_file_path $plugin_name) + check_if_version_exists $plugin_name $version - if [ "$version" == "" ]; then + if [ -z "$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 - - exit 0 + echo "$version (set by $version_file_path)" fi } diff --git a/test/current_command.bats b/test/current_command.bats index 562baa4b..8491bafb 100644 --- a/test/current_command.bats +++ b/test/current_command.bats @@ -7,55 +7,54 @@ load test_helpers setup() { setup_asdf_dir install_dummy_plugin + install_dummy_version "1.1.0" + install_dummy_version "1.2.0" 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 - - echo 'dummy 1.0.0' >> $HOME/.tool-versions - echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions - echo '1.2.0' >> $OTHER_DIR/.dummy-version + mkdir $PROJECT_DIR } teardown() { clean_asdf_dir } -@test "current should derive from the local .tool_versions when it exists" { +@test "current should derive from the current .tool-versions" { cd $PROJECT_DIR + echo 'dummy 1.1.0' >> $PROJECT_DIR/.tool-versions run current_command "dummy" [ "$status" -eq 0 ] [ "$output" = "1.1.0 (set by $PROJECT_DIR/.tool-versions)" ] } -@test "current should derive from the global .tool_versions when local doesn't exist" { - cd $OTHER_DIR - - run current_command "dummy" - [ "$status" -eq 0 ] - [ "$output" = "1.0.0 (set by $HOME/.tool-versions)" ] -} - -@test "current should derive from the legacy file if enabled and hide the file path" { +@test "current should derive from the legacy file if enabled" { + cd $PROJECT_DIR echo 'legacy_version_file = yes' > $HOME/.asdfrc - cd $OTHER_DIR + echo '1.2.0' >> $PROJECT_DIR/.dummy-version run current_command "dummy" [ "$status" -eq 0 ] - [ "$output" = "1.2.0" ] + [ "$output" = "1.2.0 (set by $PROJECT_DIR/.dummy-version)" ] } @test "current should error when the plugin doesn't exist" { run current_command "foobar" [ "$status" -eq 1 ] + [ "$output" = "No such plugin" ] } @test "current should error when no version is set" { - cd $OTHER_DIR - rm $HOME/.tool-versions + cd $PROJECT_DIR run current_command "dummy" [ "$status" -eq 1 ] - [ "$output" = "No version set for dummy" ] +} + +@test "current should error when a version is set that isn't installed" { + cd $PROJECT_DIR + echo 'dummy 9.9.9' >> $PROJECT_DIR/.tool-versions + + run current_command "dummy" + [ "$status" -eq 1 ] + [ "$output" = "version 9.9.9 is not installed for dummy" ] }