From 6bb7f07d2d74aee3bb02421265bd28767e94dcce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 13 Oct 2014 03:11:12 +0200 Subject: [PATCH] Avoid `rbenv-exec` calling out to `rbenv-version-name` twice Running any shim (and thus `rbenv-exec`) would always execute `rbenv-version-name` twice: once in `rbenv-exec` and another time in `rbenv-which`, even though RBENV_VERSION variable would have already been populated at this point. Now RBENV_VERSION is respected within `rbenv-which`. --- libexec/rbenv-which | 6 +++++- test/which.bats | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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" +}