diff --git a/libexec/rbenv-which b/libexec/rbenv-which index 5d73673c..f656537d 100755 --- a/libexec/rbenv-which +++ b/libexec/rbenv-which @@ -15,40 +15,17 @@ if [ "$1" = "--complete" ]; then exec rbenv shims --short fi -expand_path() { - if [ ! -d "$1" ]; then - return 1 - fi - - local cwd="$(pwd)" - cd "$1" - pwd - cd "$cwd" -} - remove_from_path() { - local path_to_remove="$(expand_path "$1")" - local result="" - - if [ -z "$path_to_remove" ]; then - echo "${PATH}" - return - fi - - local paths - IFS=: paths=($PATH) - - for path in "${paths[@]}"; do - path="$(expand_path "$path" || true)" - if [ -n "$path" ] && [ "$path" != "$path_to_remove" ]; then - result="${result}${path}:" - fi + local path_to_remove="$1" + local path_before + local result=":$PATH:" + while [ "$path_before" != "$result" ]; do + path_before="$result" + result="${result//:$path_to_remove:/:}" done - echo "${result%:}" } -RBENV_VERSION="$(rbenv-version-name)" RBENV_COMMAND="$1" if [ -z "$RBENV_COMMAND" ]; then @@ -56,6 +33,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 +51,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..6e68a0e6 100644 --- a/test/which.bats +++ b/test/which.bats @@ -31,6 +31,31 @@ create_executable() { assert_success "${RBENV_TEST_DIR}/bin/kill-all-humans" } +@test "searches PATH for system version (shims prepended)" { + create_executable "${RBENV_TEST_DIR}/bin" "kill-all-humans" + create_executable "${RBENV_ROOT}/shims" "kill-all-humans" + + PATH="${RBENV_ROOT}/shims:$PATH" RBENV_VERSION=system run rbenv-which kill-all-humans + assert_success "${RBENV_TEST_DIR}/bin/kill-all-humans" +} + +@test "searches PATH for system version (shims appended)" { + create_executable "${RBENV_TEST_DIR}/bin" "kill-all-humans" + create_executable "${RBENV_ROOT}/shims" "kill-all-humans" + + PATH="$PATH:${RBENV_ROOT}/shims" RBENV_VERSION=system run rbenv-which kill-all-humans + assert_success "${RBENV_TEST_DIR}/bin/kill-all-humans" +} + +@test "searches PATH for system version (shims spread)" { + create_executable "${RBENV_TEST_DIR}/bin" "kill-all-humans" + create_executable "${RBENV_ROOT}/shims" "kill-all-humans" + + PATH="${RBENV_ROOT}/shims:${RBENV_ROOT}/shims:/tmp/non-existent:$PATH:${RBENV_ROOT}/shims" \ + RBENV_VERSION=system run rbenv-which kill-all-humans + assert_success "${RBENV_TEST_DIR}/bin/kill-all-humans" +} + @test "version not installed" { create_executable "2.0" "rspec" RBENV_VERSION=1.9 run rbenv-which rspec @@ -72,3 +97,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" +}