From 6a912bf104a1585978d1f79d39642d042853277f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 12 Jan 2016 11:03:50 -0800 Subject: [PATCH 1/2] add support for rbenv shell - `rbenv shell -` allows you to switch to the previously activated ruby version. Similar to `cd -` or `git checkout -`. This tries to implement `rbenv shell -` as proposed in #854. However, adding support seemed to break the "shell change version" test. I'm not very good at Bash programming, can someone tell me what is wrong with what I'm doing? I'd like to add a bit more functionality to this, but I'm really just cargo cult programming Bash. Thank you! fix tests --- libexec/rbenv-sh-shell | 33 +++++++++++++++++++++--- test/shell.bats | 57 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 7 deletions(-) diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index f4e0098f..9c885e93 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -39,25 +39,50 @@ fi if [ "$version" = "--unset" ]; then case "$shell" in fish ) + echo "set -e OLD_RBENV_VERSION" echo "set -e RBENV_VERSION" ;; * ) + echo "unset OLD_RBENV_VERSION" echo "unset RBENV_VERSION" ;; esac exit fi -# Make sure the specified version is installed. -if rbenv-prefix "$version" >/dev/null; then +if [ "$version" = "-" ]; then + if [ -z "$OLD_RBENV_VERSION" ]; then + echo "rbenv: OLD_RBENV_VERSION not set" >&2 + exit 1; + fi case "$shell" in fish ) - echo "setenv RBENV_VERSION \"${version}\"" + rbenv_version=$RBENV_VERSION + echo "set -e OLD_RBENV_VERSION \"$rbenv_version\"" + echo "set -e RBENV_VERSION \"$OLD_RBENV_VERSION\"" ;; * ) - echo "export RBENV_VERSION=\"${version}\"" + rbenv_version=$RBENV_VERSION + echo "export OLD_RBENV_VERSION=\"$rbenv_version\"" + echo "export RBENV_VERSION=\"$OLD_RBENV_VERSION\"" ;; esac + exit +fi + +# Make sure the specified version is installed. +if rbenv-prefix "$version" >/dev/null; then + if [ "$version" != "$RBENV_VERSION" ]; then + case "$shell" in + fish ) + echo "setenv RBENV_VERSION \"${version}\"" + ;; + * ) + echo "export OLD_RBENV_VERSION=\"$RBENV_VERSION\"" + echo "export RBENV_VERSION=\"${version}\"" + ;; + esac + fi else echo "false" exit 1 diff --git a/test/shell.bats b/test/shell.bats index 0f776745..3c7313c1 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -22,12 +22,18 @@ load test_helper @test "shell unset" { RBENV_SHELL=bash run rbenv-sh-shell --unset - assert_success "unset RBENV_VERSION" + assert_output < Date: Fri, 25 Nov 2016 20:24:16 +0100 Subject: [PATCH 2/2] Finalize `rbenv shell -` implementation This ensures that OLD_RBENV_VERSION is never exported. This makes the implementation a little bit more complex, since more logic needs to be pushed down into eval'd code. --- libexec/rbenv-sh-shell | 61 +++++++++++++++++++++++++++---------- test/shell.bats | 69 ++++++++++++++---------------------------- 2 files changed, 68 insertions(+), 62 deletions(-) diff --git a/libexec/rbenv-sh-shell b/libexec/rbenv-sh-shell index 9c885e93..04f81866 100755 --- a/libexec/rbenv-sh-shell +++ b/libexec/rbenv-sh-shell @@ -3,6 +3,7 @@ # Summary: Set or show the shell-specific Ruby version # # Usage: rbenv shell +# rbenv shell - # rbenv shell --unset # # Sets a shell-specific Ruby version by setting the `RBENV_VERSION' @@ -12,6 +13,11 @@ # should be a string matching a Ruby version known to rbenv. # The special version string `system' will use your default system Ruby. # Run `rbenv versions' for a list of available Ruby versions. +# +# When `-` is passed instead of the version string, the previously set +# version will be restored. With `--unset`, the `RBENV_VERSION` +# environment variable gets unset, restoring the environment to the +# state before the first `rbenv shell` call. set -e [ -n "$RBENV_DEBUG" ] && set -x @@ -31,7 +37,7 @@ if [ -z "$version" ]; then echo "rbenv: no shell-specific version configured" >&2 exit 1 else - echo "echo \"\$RBENV_VERSION\"" + echo 'echo "$RBENV_VERSION"' exit fi fi @@ -39,11 +45,11 @@ fi if [ "$version" = "--unset" ]; then case "$shell" in fish ) - echo "set -e OLD_RBENV_VERSION" + echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' echo "set -e RBENV_VERSION" ;; * ) - echo "unset OLD_RBENV_VERSION" + echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' echo "unset RBENV_VERSION" ;; esac @@ -51,20 +57,42 @@ if [ "$version" = "--unset" ]; then fi if [ "$version" = "-" ]; then - if [ -z "$OLD_RBENV_VERSION" ]; then - echo "rbenv: OLD_RBENV_VERSION not set" >&2 - exit 1; - fi case "$shell" in fish ) - rbenv_version=$RBENV_VERSION - echo "set -e OLD_RBENV_VERSION \"$rbenv_version\"" - echo "set -e RBENV_VERSION \"$OLD_RBENV_VERSION\"" + cat <&2 + false +end +EOS ;; * ) - rbenv_version=$RBENV_VERSION - echo "export OLD_RBENV_VERSION=\"$rbenv_version\"" - echo "export RBENV_VERSION=\"$OLD_RBENV_VERSION\"" + cat <&2 + false +fi +EOS ;; esac exit @@ -75,11 +103,12 @@ if rbenv-prefix "$version" >/dev/null; then if [ "$version" != "$RBENV_VERSION" ]; then case "$shell" in fish ) - echo "setenv RBENV_VERSION \"${version}\"" + echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"' + echo "set -gx RBENV_VERSION \"$version\"" ;; * ) - echo "export OLD_RBENV_VERSION=\"$RBENV_VERSION\"" - echo "export RBENV_VERSION=\"${version}\"" + echo 'OLD_RBENV_VERSION="$RBENV_VERSION"' + echo "export RBENV_VERSION=\"$version\"" ;; esac fi diff --git a/test/shell.bats b/test/shell.bats index 3c7313c1..a3c16b66 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -20,18 +20,32 @@ load test_helper assert_success 'echo "$RBENV_VERSION"' } +@test "shell revert" { + RBENV_SHELL=bash run rbenv-sh-shell - + assert_success + assert_line 0 'if [ -n "${OLD_RBENV_VERSION+x}" ]; then' +} + +@test "shell revert (fish)" { + RBENV_SHELL=fish run rbenv-sh-shell - + assert_success + assert_line 0 'if set -q OLD_RBENV_VERSION' +} + @test "shell unset" { RBENV_SHELL=bash run rbenv-sh-shell --unset + assert_success assert_output <