diff --git a/libexec/pyenv---version b/libexec/pyenv---version index 778cf109..36883f91 100755 --- a/libexec/pyenv---version +++ b/libexec/pyenv---version @@ -12,7 +12,7 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x -version="1.0.10" +version="1.1.0" git_revision="" if cd "${BASH_SOURCE%/*}" 2>/dev/null && git remote -v 2>/dev/null | grep -q pyenv; then diff --git a/libexec/pyenv-init b/libexec/pyenv-init index 9bdcb9b7..df196295 100755 --- a/libexec/pyenv-init +++ b/libexec/pyenv-init @@ -86,8 +86,8 @@ mkdir -p "${PYENV_ROOT}/"{shims,versions} case "$shell" in fish ) - echo "setenv PATH '${PYENV_ROOT}/shims' \$PATH" - echo "setenv PYENV_SHELL $shell" + echo "set -gx PATH '${PYENV_ROOT}/shims' \$PATH" + echo "set -gx PYENV_SHELL $shell" ;; * ) echo 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"' @@ -97,10 +97,7 @@ esac completion="${root}/completions/pyenv.${shell}" if [ -r "$completion" ]; then - case "$shell" in - fish ) echo ". '$completion'" ;; - * ) echo "source '$completion'" ;; - esac + echo "source '$completion'" fi if [ -z "$no_rehash" ]; then @@ -117,7 +114,7 @@ function pyenv switch "\$command" case ${commands[*]} - . (pyenv "sh-\$command" \$argv|psub) + source (pyenv "sh-\$command" \$argv|psub) case '*' command pyenv "\$command" \$argv end diff --git a/libexec/pyenv-sh-shell b/libexec/pyenv-sh-shell index e02383f9..b38abbf3 100755 --- a/libexec/pyenv-sh-shell +++ b/libexec/pyenv-sh-shell @@ -2,7 +2,8 @@ # # Summary: Set or show the shell-specific Python version # -# Usage: pyenv shell +# Usage: pyenv shell ... +# pyenv shell - # pyenv shell --unset # # Sets a shell-specific Python version by setting the `PYENV_VERSION' @@ -12,6 +13,11 @@ # should be a string matching a Python version known to pyenv. # The special version string `system' will use your default system Python. # Run `pyenv versions' for a list of available Python versions. +# +# When `-` is passed instead of the version string, the previously set +# version will be restored. With `--unset`, the `PYENV_VERSION` +# environment variable gets unset, restoring the environment to the +# state before the first `pyenv shell` call. set -e [ -n "$PYENV_DEBUG" ] && set -x @@ -31,7 +37,7 @@ if [ -z "$versions" ]; then echo "pyenv: no shell-specific version configured" >&2 exit 1 else - echo "echo \"\$PYENV_VERSION\"" + echo 'echo "$PYENV_VERSION"' exit fi fi @@ -39,28 +45,76 @@ fi if [ "$versions" = "--unset" ]; then case "$shell" in fish ) + echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"' echo "set -e PYENV_VERSION" ;; * ) + echo 'PYENV_VERSION_OLD="$PYENV_VERSION"' echo "unset PYENV_VERSION" ;; esac exit fi +if [ "$versions" = "-" ]; then + case "$shell" in + fish ) + cat <&2 + false +end +EOS + ;; + * ) + cat <&2 + false +fi +EOS + ;; + esac + exit +fi + # Make sure the specified version is installed. if pyenv-prefix "${versions[@]}" >/dev/null; then OLDIFS="$IFS" IFS=: version="${versions[*]}" IFS="$OLDIFS" - case "$shell" in - fish ) - echo "setenv PYENV_VERSION \"${version}\"" - ;; - * ) - echo "export PYENV_VERSION=\"${version}\"" - ;; - esac + if [ "$version" != "$PYENV_VERSION" ]; then + case "$shell" in + fish ) + echo 'set -gu PYENV_VERSION_OLD "$PYENV_VERSION"' + echo "set -gx PYENV_VERSION \"$version\"" + ;; + * ) + echo 'PYENV_VERSION_OLD="$PYENV_VERSION"' + echo "export PYENV_VERSION=\"${version}\"" + ;; + esac + fi else echo "false" exit 1 diff --git a/libexec/pyenv-which b/libexec/pyenv-which index a8553e7d..9551f6cf 100755 --- a/libexec/pyenv-which +++ b/libexec/pyenv-which @@ -18,7 +18,7 @@ fi remove_from_path() { local path_to_remove="$1" local path_before - local result=":$PATH:" + local result=":${PATH//\~/$HOME}:" while [ "$path_before" != "$result" ]; do path_before="$result" result="${result//:$path_to_remove:/:}" diff --git a/src/configure b/src/configure index 86e25ac1..10d0ff57 100755 --- a/src/configure +++ b/src/configure @@ -21,6 +21,9 @@ case "$(uname -s)" in Darwin* ) host_os="darwin$(uname -r)" ;; +FreeBSD* ) + host_os="freebsd$(uname -r)" + ;; OpenBSD* ) host_os="openbsd$(uname -r)" ;; diff --git a/test/init.bats b/test/init.bats old mode 100644 new mode 100755 index c7026f65..ead63d82 --- a/test/init.bats +++ b/test/init.bats @@ -47,7 +47,7 @@ OUT root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" run pyenv-init - fish assert_success - assert_line ". '${root}/test/../libexec/../completions/pyenv.fish'" + assert_line "source '${root}/test/../libexec/../completions/pyenv.fish'" } @test "fish instructions" { @@ -73,7 +73,7 @@ OUT export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin:/usr/local/bin" run pyenv-init - fish assert_success - assert_line 0 "setenv PATH '${PYENV_ROOT}/shims' \$PATH" + assert_line 0 "set -gx PATH '${PYENV_ROOT}/shims' \$PATH" } @test "can add shims to PATH more than once" { @@ -87,7 +87,7 @@ OUT export PATH="${PYENV_ROOT}/shims:$PATH" run pyenv-init - fish assert_success - assert_line 0 "setenv PATH '${PYENV_ROOT}/shims' \$PATH" + assert_line 0 "set -gx PATH '${PYENV_ROOT}/shims' \$PATH" } @test "outputs sh-compatible syntax" { diff --git a/test/shell.bats b/test/shell.bats index 30bf127b..82383aad 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -20,14 +20,34 @@ load test_helper assert_success 'echo "$PYENV_VERSION"' } +@test "shell revert" { + PYENV_SHELL=bash run pyenv-sh-shell - + assert_success + assert_line 0 'if [ -n "${PYENV_VERSION_OLD+x}" ]; then' +} + +@test "shell revert (fish)" { + PYENV_SHELL=fish run pyenv-sh-shell - + assert_success + assert_line 0 'if set -q PYENV_VERSION_OLD' +} + @test "shell unset" { PYENV_SHELL=bash run pyenv-sh-shell --unset - assert_success "unset PYENV_VERSION" + assert_success + assert_output <