From c3a5f91ed0d9f7a0382ae1821297051bc827e4b8 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Wed, 27 May 2015 23:29:11 -0400 Subject: [PATCH] create hook: version-origin Expose a `version-origin` hook. It is invoked *before* the traditional `rbenv-version-file` lookup. Because `version-origin` is traditionally run immediately after `version-name`, then any plugin hooks that alter `version-name` would have done so. Thus, running `version-origin` prior to printing the origin gives those plugins a chance to alter the `version-origin` to match. If any of the hooks set `$RBENV_VERSION_ORIGIN`, then it is used as the return value. Otherwise, the existing logic continues to return "environment variable" or "filename" as appropriate. This change, in conjunction with the `version-name` hook, makes a clean seam by which plugins can inject their own ruby version setting logic. Using this seam, as opposed to altering `$RBENV_COMMAND_PATH` from the `which` hook, means that the version name and origin are set more reliably and so `version`, `version-name`, `version-origin` and `which` all work as expected. Indeed, even PS1 works now. --- libexec/rbenv-version-origin | 11 ++++++++++- test/version-origin.bats | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/libexec/rbenv-version-origin b/libexec/rbenv-version-origin index ae7abf9c..a9d21c4c 100755 --- a/libexec/rbenv-version-origin +++ b/libexec/rbenv-version-origin @@ -3,7 +3,16 @@ set -e [ -n "$RBENV_DEBUG" ] && set -x -if [ -n "$RBENV_VERSION" ]; then +OLDIFS="$IFS" +IFS=$'\n' scripts=(`rbenv-hooks version-origin`) +IFS="$OLDIFS" +for script in "${scripts[@]}"; do + source "$script" +done + +if [ -n "$RBENV_VERSION_ORIGIN" ]; then + echo "$RBENV_VERSION_ORIGIN" +elif [ -n "$RBENV_VERSION" ]; then echo "RBENV_VERSION environment variable" else rbenv-version-file diff --git a/test/version-origin.bats b/test/version-origin.bats index 8ad4db04..fed0aaa5 100644 --- a/test/version-origin.bats +++ b/test/version-origin.bats @@ -2,6 +2,13 @@ load test_helper +export RBENV_HOOK_PATH="${RBENV_ROOT}/rbenv.d" + +create_hook() { + mkdir -p "${RBENV_ROOT}/rbenv.d/version-origin" + cat > "${RBENV_ROOT}/rbenv.d/version-origin/$1" <<<"$2" +} + setup() { mkdir -p "$RBENV_TEST_DIR" cd "$RBENV_TEST_DIR" @@ -36,3 +43,12 @@ setup() { run rbenv-version-origin assert_success "${PWD}/.rbenv-version" } + +@test "reports from hook" { + touch .ruby-version + create_hook test.bash "RBENV_VERSION_ORIGIN=plugin" + + RBENV_VERSION=1 run rbenv-version-origin + + assert_success "plugin" +}