diff --git a/libexec/rbenv-which b/libexec/rbenv-which index 5d73673c..49906df6 100755 --- a/libexec/rbenv-which +++ b/libexec/rbenv-which @@ -48,7 +48,6 @@ remove_from_path() { echo "${result%:}" } -RBENV_VERSION="$(rbenv-version-name)" RBENV_COMMAND="$1" if [ -z "$RBENV_COMMAND" ]; then @@ -56,6 +55,8 @@ if [ -z "$RBENV_COMMAND" ]; then exit 1 fi +RBENV_VERSION="${RBENV_VERSION:-$(rbenv-version-name)}" + if [ "$RBENV_VERSION" = "system" ]; then PATH="$(remove_from_path "${RBENV_ROOT}/shims")" RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND" || true)" @@ -72,6 +73,9 @@ done if [ -x "$RBENV_COMMAND_PATH" ]; then echo "$RBENV_COMMAND_PATH" +elif ! [ -d "${RBENV_ROOT}/versions/${RBENV_VERSION}" ]; then + echo "rbenv: version \`$RBENV_VERSION' is not installed" >&2 + exit 1 else echo "rbenv: $RBENV_COMMAND: command not found" >&2 diff --git a/test/which.bats b/test/which.bats index a9d9f82a..e52d5005 100644 --- a/test/which.bats +++ b/test/which.bats @@ -72,3 +72,12 @@ SH assert_success assert_output "HELLO=:hello:ugly:world:again" } + +@test "discovers version from rbenv-version-name" { + mkdir -p "$RBENV_ROOT" + cat > "${RBENV_ROOT}/version" <<<"1.8" + create_executable "1.8" "ruby" + + RBENV_VERSION= run rbenv-which ruby + assert_success "${RBENV_ROOT}/versions/1.8/bin/ruby" +}