From f4fade3d268bf73b5260ffb50175c7beca2f16ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 3 Oct 2013 21:42:52 +0200 Subject: [PATCH] better error message for `rbenv prefix system` Have `rbenv prefix` handle the case where system Ruby is not installed, i.e. `rbenv which ruby` doesn't find ruby in PATH. Fixes #362 --- libexec/rbenv-prefix | 12 ++++++++---- test/prefix.bats | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/libexec/rbenv-prefix b/libexec/rbenv-prefix index a0aebab..5376cf5 100755 --- a/libexec/rbenv-prefix +++ b/libexec/rbenv-prefix @@ -22,10 +22,14 @@ elif [ -z "$RBENV_VERSION" ]; then fi if [ "$RBENV_VERSION" = "system" ]; then - RUBY_PATH="$(rbenv-which ruby)" - RUBY_PATH="${RUBY_PATH%/*}" - echo "${RUBY_PATH%/bin}" - exit + if RUBY_PATH="$(rbenv-which ruby 2>/dev/null)"; then + RUBY_PATH="${RUBY_PATH%/*}" + echo "${RUBY_PATH%/bin}" + exit + else + echo "rbenv: system version not found in PATH" >&2 + exit 1 + fi fi RBENV_PREFIX_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}" diff --git a/test/prefix.bats b/test/prefix.bats index b7afa87..0d6cdba 100644 --- a/test/prefix.bats +++ b/test/prefix.bats @@ -23,3 +23,17 @@ load test_helper RBENV_VERSION="system" run rbenv-prefix assert_success "$RBENV_TEST_DIR" } + +@test "prefix for invalid system" { + USRBIN_ALT="${RBENV_TEST_DIR}/usr-bin-alt" + mkdir -p "$USRBIN_ALT" + for util in head readlink greadlink; do + if [ -x "/usr/bin/$util" ]; then + ln -s "/usr/bin/$util" "${USRBIN_ALT}/$util" + fi + done + PATH_WITHOUT_RUBY="${PATH/\/usr\/bin:/$USRBIN_ALT:}" + + PATH="$PATH_WITHOUT_RUBY" run rbenv-prefix system + assert_failure "rbenv: system version not found in PATH" +}