From 0de2cdbbc3bc00c6b896a36f4417b97ab5588af0 Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Sat, 5 Jul 2014 06:53:46 +0900 Subject: [PATCH] Should not persist `PYENV_DEACTIVATE` after failed attempt to deactivate and activate --- bin/pyenv-virtualenv-init | 18 ++++++++++++------ test/init.bats | 27 ++++++++++++++++++--------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/bin/pyenv-virtualenv-init b/bin/pyenv-virtualenv-init index 3eba180..68beb50 100755 --- a/bin/pyenv-virtualenv-init +++ b/bin/pyenv-virtualenv-init @@ -81,11 +81,14 @@ function _pyenv_virtualenv_hook --on-event fish_prompt; return 0 end if [ "\$PYENV_ACTIVATE" != (pyenv prefix) ] - eval (pyenv sh-deactivate --no-error --verbose) - eval (pyenv sh-activate --no-error --verbose) + if eval (pyenv sh-deactivate --no-error --verbose) + eval (pyenv sh-activate --no-error --verbose); or set -e PYENV_DEACTIVATE + else + eval (pyenv sh-activate --no-error --verbose) + end end else - if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] + if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] eval (pyenv sh-activate --no-error --verbose) end end @@ -112,11 +115,14 @@ if [[ "$shell" != "fish" ]]; then return 0 fi if [ "x\$PYENV_ACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv deactivate --no-error --verbose - pyenv activate --no-error --verbose + if pyenv deactivate --no-error --verbose; then + pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + else + pyenv activate --no-error --verbose + fi fi else - if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + if [ -z "\$VIRTUAL_ENV" ] && [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then pyenv activate --no-error --verbose fi fi diff --git a/test/init.bats b/test/init.bats index 7c29a5b..f000a2c 100644 --- a/test/init.bats +++ b/test/init.bats @@ -37,11 +37,14 @@ _pyenv_virtualenv_hook() { return 0 fi if [ "x\$PYENV_ACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv deactivate --no-error --verbose - pyenv activate --no-error --verbose + if pyenv deactivate --no-error --verbose; then + pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + else + pyenv activate --no-error --verbose + fi fi else - if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + if [ -z "\$VIRTUAL_ENV" ] && [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then pyenv activate --no-error --verbose fi fi @@ -64,11 +67,14 @@ function _pyenv_virtualenv_hook --on-event fish_prompt; return 0 end if [ "\$PYENV_ACTIVATE" != (pyenv prefix) ] - eval (pyenv sh-deactivate --no-error --verbose) - eval (pyenv sh-activate --no-error --verbose) + if eval (pyenv sh-deactivate --no-error --verbose) + eval (pyenv sh-activate --no-error --verbose); or set -e PYENV_DEACTIVATE + else + eval (pyenv sh-activate --no-error --verbose) + end end else - if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] + if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] eval (pyenv sh-activate --no-error --verbose) end end @@ -88,11 +94,14 @@ _pyenv_virtualenv_hook() { return 0 fi if [ "x\$PYENV_ACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv deactivate --no-error --verbose - pyenv activate --no-error --verbose + if pyenv deactivate --no-error --verbose; then + pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + else + pyenv activate --no-error --verbose + fi fi else - if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then + if [ -z "\$VIRTUAL_ENV" ] && [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then pyenv activate --no-error --verbose fi fi