diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 7e58d66..20d66a0 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -14,23 +14,32 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x -# Provide pyenv completions -if [ "$1" = "--complete" ]; then - echo --unset - exec pyenv-virtualenvs --bare -fi - unset NOERROR +unset VERBOSE -if [ "$1" = "--no-error" ]; then - NOERROR=1 +while [ $# -gt 0 ]; do + case "$1" in + "--complete" ) + # Provide pyenv completions + echo --unset + exec pyenv-virtualenvs --bare + ;; + "--no-error" ) + NOERROR=1 + ;; + "--unset" ) + echo "pyenv deactivate" + exit + ;; + "--verbose" ) + VERBOSE=1 + ;; + * ) + break + ;; + esac shift 1 -fi - -if [ "$1" = "--unset" ]; then - echo "pyenv deactivate" - exit -fi +done versions=("$@") if [ -z "$versions" ]; then @@ -78,10 +87,12 @@ fish ) esac # Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile" -if [ -z "$NOERROR" ]; then - if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then - pyenv-virtualenv-init >&2 || true - fi +if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then + pyenv-virtualenv-init >&2 || true +fi + +if [ -n "$VERBOSE" ]; then + echo "pyenv-virtualenv: activate ${versions}" 1>&2 fi if [ -z "$no_shell" ]; then diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate index 20998c8..8892305 100755 --- a/bin/pyenv-sh-deactivate +++ b/bin/pyenv-sh-deactivate @@ -10,11 +10,22 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x unset NOERROR +unset VERBOSE -if [ "$1" = "--no-error" ]; then - NOERROR=1 +while [ $# -gt 0 ]; do + case "$1" in + "--no-error" ) + NOERROR=1 + ;; + "--verbose" ) + VERBOSE=1 + ;; + * ) + break + ;; + esac shift 1 -fi +done shell="$(basename "${PYENV_SHELL:-$SHELL}")" case "$shell" in @@ -26,6 +37,10 @@ fish ) ;; esac +if [ -n "$VERBOSE" ]; then + echo " echo \"pyenv-virtualenv: deactivate ${VIRTUAL_ENV##*/}\" 1>&2;" +fi + if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then # Backward compatibility issue # https://github.com/yyuu/pyenv-virtualenv/issues/26 @@ -35,14 +50,14 @@ fi case "$shell" in fish ) cat </dev/null); or true + eval (pyenv sh-deactivate --no-error --verbose) + eval (pyenv sh-activate --no-error --verbose) end else if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] - eval (pyenv sh-activate 2>/dev/null); or true + eval (pyenv sh-activate --no-error --verbose) end end end @@ -108,16 +108,16 @@ if [[ "$shell" != "fish" ]]; then cat </dev/null || true + pyenv deactivate --no-error --verbose + pyenv activate --no-error --verbose fi else if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv activate 2>/dev/null || true + pyenv activate --no-error --verbose fi fi }; diff --git a/test/activate.bats b/test/activate.bats index 4a5b419..5b71407 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -27,6 +27,27 @@ source "${PYENV_ROOT}/versions/venv/bin/activate"; EOS } +@test "activate virtualenv from current version (verbose)" { + export PYENV_VIRTUALENV_INIT=1 + + stub pyenv-version-name "echo venv" + stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + + PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate --verbose + + unstub pyenv-version-name + unstub pyenv-virtualenv-prefix + unstub pyenv-prefix + + assert_success + assert_output </dev/null 2>&1; then - export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + export PYENV_DEACTIVATE="$VIRTUAL_ENV"; + deactivate; +else + echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; + false; +fi; +EOS +} + +@test "deactivate virtualenv (verbose)" { + export PYENV_VIRTUALENV_INIT=1 + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" + + PYENV_SHELL="bash" run pyenv-sh-deactivate --verbose + + assert_success + assert_output </dev/null 2>&1; then + echo "pyenv-virtualenv: deactivate venv" 1>&2; + export PYENV_DEACTIVATE="$VIRTUAL_ENV"; deactivate; else echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; @@ -25,13 +45,14 @@ EOS @test "deactivate virtualenv (no-error)" { export PYENV_VIRTUALENV_INIT=1 + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error assert_success assert_output </dev/null 2>&1; then - export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + export PYENV_DEACTIVATE="$VIRTUAL_ENV"; deactivate; else false; @@ -39,8 +60,9 @@ fi; EOS } -@test "deactivate virtualenv (w/o pyenv-virtualenv-init)" { +@test "deactivate virtualenv (without pyenv-virtualenv-init)" { export PYENV_VIRTUALENV_INIT= + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="bash" run pyenv-sh-deactivate @@ -48,7 +70,7 @@ EOS assert_output </dev/null 2>&1; then pyenv shell --unset; - export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + export PYENV_DEACTIVATE="$VIRTUAL_ENV"; deactivate; else echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; @@ -57,8 +79,9 @@ fi; EOS } -@test "deactivate virtualenv (w/o pyenv-virtualenv-init) (no-error)" { +@test "deactivate virtualenv (without pyenv-virtualenv-init) (no-error)" { export PYENV_VIRTUALENV_INIT= + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error @@ -66,7 +89,7 @@ EOS assert_output </dev/null 2>&1; then pyenv shell --unset; - export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + export PYENV_DEACTIVATE="$VIRTUAL_ENV"; deactivate; else false; @@ -76,13 +99,14 @@ EOS @test "deactivate virtualenv (fish)" { export PYENV_VIRTUALENV_INIT=1 + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="fish" run pyenv-sh-deactivate assert_success assert_output <&2; @@ -93,13 +117,14 @@ EOS @test "deactivate virtualenv (fish) (no-error)" { export PYENV_VIRTUALENV_INIT=1 + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error assert_success assert_output <&2; @@ -125,8 +151,9 @@ end; EOS } -@test "deactivate virtualenv (fish) (w/o pyenv-virtualenv-init) (no-error)" { +@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init) (no-error)" { export PYENV_VIRTUALENV_INIT= + export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error @@ -134,7 +161,7 @@ EOS assert_output </dev/null || true + pyenv deactivate --no-error --verbose + pyenv activate --no-error --verbose fi else if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv activate 2>/dev/null || true + pyenv activate --no-error --verbose fi fi }; @@ -60,16 +60,16 @@ setenv PYENV_VIRTUALENV_INIT 1; function _pyenv_virtualenv_hook --on-event fish_prompt; if [ -n "\$VIRTUAL_ENV" ] if [ (pyenv version-name) = "system" ] - eval (pyenv sh-deactivate); or true + eval (pyenv sh-deactivate --no-error --verbose) return 0 end if [ "\$VIRTUAL_ENV" != (pyenv prefix) ] - eval (pyenv sh-deactivate); or true - eval (pyenv sh-activate 2>/dev/null); or true + eval (pyenv sh-deactivate --no-error --verbose) + eval (pyenv sh-activate --no-error --verbose) end else if [ "\$PYENV_DEACTIVATE" != (pyenv prefix) ] - eval (pyenv sh-activate 2>/dev/null); or true + eval (pyenv sh-activate --no-error --verbose) end end end @@ -84,16 +84,16 @@ export PYENV_VIRTUALENV_INIT=1; _pyenv_virtualenv_hook() { if [ -n "\$VIRTUAL_ENV" ]; then if [ "x\`pyenv version-name\`" = "xsystem" ]; then - pyenv deactivate || true + pyenv deactivate --no-error --verbose return 0 fi if [ "x\$VIRTUAL_ENV" != "x\`pyenv prefix\`" ]; then - pyenv deactivate || true - pyenv activate 2>/dev/null || true + pyenv deactivate --no-error --verbose + pyenv activate --no-error --verbose fi else if [ "x\$PYENV_DEACTIVATE" != "x\`pyenv prefix\`" ]; then - pyenv activate 2>/dev/null || true + pyenv activate --no-error --verbose fi fi };