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.
This commit is contained in:
Mislav Marohnić 2016-11-25 20:24:16 +01:00
parent 6a912bf104
commit c4d97ad392
2 changed files with 68 additions and 62 deletions

View file

@ -3,6 +3,7 @@
# Summary: Set or show the shell-specific Ruby version # Summary: Set or show the shell-specific Ruby version
# #
# Usage: rbenv shell <version> # Usage: rbenv shell <version>
# rbenv shell -
# rbenv shell --unset # rbenv shell --unset
# #
# Sets a shell-specific Ruby version by setting the `RBENV_VERSION' # Sets a shell-specific Ruby version by setting the `RBENV_VERSION'
@ -12,6 +13,11 @@
# <version> should be a string matching a Ruby version known to rbenv. # <version> should be a string matching a Ruby version known to rbenv.
# The special version string `system' will use your default system Ruby. # The special version string `system' will use your default system Ruby.
# Run `rbenv versions' for a list of available Ruby versions. # 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 set -e
[ -n "$RBENV_DEBUG" ] && set -x [ -n "$RBENV_DEBUG" ] && set -x
@ -31,7 +37,7 @@ if [ -z "$version" ]; then
echo "rbenv: no shell-specific version configured" >&2 echo "rbenv: no shell-specific version configured" >&2
exit 1 exit 1
else else
echo "echo \"\$RBENV_VERSION\"" echo 'echo "$RBENV_VERSION"'
exit exit
fi fi
fi fi
@ -39,11 +45,11 @@ fi
if [ "$version" = "--unset" ]; then if [ "$version" = "--unset" ]; then
case "$shell" in case "$shell" in
fish ) fish )
echo "set -e OLD_RBENV_VERSION" echo 'set -gu OLD_RBENV_VERSION "$RBENV_VERSION"'
echo "set -e RBENV_VERSION" echo "set -e RBENV_VERSION"
;; ;;
* ) * )
echo "unset OLD_RBENV_VERSION" echo 'OLD_RBENV_VERSION="$RBENV_VERSION"'
echo "unset RBENV_VERSION" echo "unset RBENV_VERSION"
;; ;;
esac esac
@ -51,20 +57,42 @@ if [ "$version" = "--unset" ]; then
fi fi
if [ "$version" = "-" ]; then if [ "$version" = "-" ]; then
if [ -z "$OLD_RBENV_VERSION" ]; then
echo "rbenv: OLD_RBENV_VERSION not set" >&2
exit 1;
fi
case "$shell" in case "$shell" in
fish ) fish )
rbenv_version=$RBENV_VERSION cat <<EOS
echo "set -e OLD_RBENV_VERSION \"$rbenv_version\"" if set -q OLD_RBENV_VERSION
echo "set -e RBENV_VERSION \"$OLD_RBENV_VERSION\"" if [ -n "\$OLD_RBENV_VERSION" ]
set OLD_RBENV_VERSION_ "\$RBENV_VERSION"
set -gx RBENV_VERSION "\$OLD_RBENV_VERSION"
set -gu OLD_RBENV_VERSION "\$OLD_RBENV_VERSION_"
set -e OLD_RBENV_VERSION_
else
set -gu OLD_RBENV_VERSION "\$RBENV_VERSION"
set -e RBENV_VERSION
end
else
echo "rbenv: OLD_RBENV_VERSION is not set" >&2
false
end
EOS
;; ;;
* ) * )
rbenv_version=$RBENV_VERSION cat <<EOS
echo "export OLD_RBENV_VERSION=\"$rbenv_version\"" if [ -n "\${OLD_RBENV_VERSION+x}" ]; then
echo "export RBENV_VERSION=\"$OLD_RBENV_VERSION\"" if [ -n "\$OLD_RBENV_VERSION" ]; then
OLD_RBENV_VERSION_="\$RBENV_VERSION"
export RBENV_VERSION="\$OLD_RBENV_VERSION"
OLD_RBENV_VERSION="\$OLD_RBENV_VERSION_"
unset OLD_RBENV_VERSION_
else
OLD_RBENV_VERSION="\$RBENV_VERSION"
unset RBENV_VERSION
fi
else
echo "rbenv: OLD_RBENV_VERSION is not set" >&2
false
fi
EOS
;; ;;
esac esac
exit exit
@ -75,11 +103,12 @@ if rbenv-prefix "$version" >/dev/null; then
if [ "$version" != "$RBENV_VERSION" ]; then if [ "$version" != "$RBENV_VERSION" ]; then
case "$shell" in case "$shell" in
fish ) 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 'OLD_RBENV_VERSION="$RBENV_VERSION"'
echo "export RBENV_VERSION=\"${version}\"" echo "export RBENV_VERSION=\"$version\""
;; ;;
esac esac
fi fi

View file

@ -20,18 +20,32 @@ load test_helper
assert_success 'echo "$RBENV_VERSION"' 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" { @test "shell unset" {
RBENV_SHELL=bash run rbenv-sh-shell --unset RBENV_SHELL=bash run rbenv-sh-shell --unset
assert_success
assert_output <<OUT assert_output <<OUT
unset OLD_RBENV_VERSION OLD_RBENV_VERSION="\$RBENV_VERSION"
unset RBENV_VERSION unset RBENV_VERSION
OUT OUT
} }
@test "shell unset (fish)" { @test "shell unset (fish)" {
RBENV_SHELL=fish run rbenv-sh-shell --unset RBENV_SHELL=fish run rbenv-sh-shell --unset
assert_success
assert_output <<OUT assert_output <<OUT
set -e OLD_RBENV_VERSION set -gu OLD_RBENV_VERSION "\$RBENV_VERSION"
set -e RBENV_VERSION set -e RBENV_VERSION
OUT OUT
} }
@ -48,56 +62,19 @@ SH
@test "shell change version" { @test "shell change version" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3" mkdir -p "${RBENV_ROOT}/versions/1.2.3"
RBENV_SHELL=bash run rbenv-sh-shell 1.2.3 RBENV_SHELL=bash run rbenv-sh-shell 1.2.3
assert_success
assert_output <<OUT assert_output <<OUT
export OLD_RBENV_VERSION="" OLD_RBENV_VERSION="\$RBENV_VERSION"
export RBENV_VERSION="1.2.3" export RBENV_VERSION="1.2.3"
OUT OUT
} }
@test "shell change version pushes away previous OLD_RBENV_VERSION" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3"
mkdir -p "${RBENV_ROOT}/versions/1.2.4"
mkdir -p "${RBENV_ROOT}/versions/1.2.5"
export OLD_RBENV_VERSION="1.2.3"
export RBENV_VERSION="1.2.4"
RBENV_SHELL=bash run rbenv-sh-shell 1.2.5
assert_output <<OUT
export OLD_RBENV_VERSION="1.2.4"
export RBENV_VERSION="1.2.5"
OUT
}
@test "shell change version to the same version does not lose OLD_RBENV_VERSION" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3"
mkdir -p "${RBENV_ROOT}/versions/1.2.4"
export OLD_RBENV_VERSION="1.2.3"
export RBENV_VERSION="1.2.4"
RBENV_SHELL=bash run rbenv-sh-shell 1.2.4
assert_output ''
}
@test "shell change version to - swaps old and new versions" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3"
mkdir -p "${RBENV_ROOT}/versions/1.2.4"
export OLD_RBENV_VERSION="1.2.3"
export RBENV_VERSION="1.2.4"
RBENV_SHELL=bash run rbenv-sh-shell -
assert_output <<OUT
export OLD_RBENV_VERSION="1.2.4"
export RBENV_VERSION="1.2.3"
OUT
}
@test "shell change version to - with no previous is an error" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3"
RBENV_SHELL=bash run rbenv-sh-shell -
assert_failure <<OUT
rbenv: OLD_RBENV_VERSION not set
OUT
}
@test "shell change version (fish)" { @test "shell change version (fish)" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3" mkdir -p "${RBENV_ROOT}/versions/1.2.3"
RBENV_SHELL=fish run rbenv-sh-shell 1.2.3 RBENV_SHELL=fish run rbenv-sh-shell 1.2.3
assert_success 'setenv RBENV_VERSION "1.2.3"' assert_success
assert_output <<OUT
set -gu OLD_RBENV_VERSION "\$RBENV_VERSION"
set -gx RBENV_VERSION "1.2.3"
OUT
} }