From f0c400b6692fddd366e1665e2e4a6258f10f022e Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Fri, 4 Jul 2014 20:47:10 +0900 Subject: [PATCH] Add `--quiet` option to `activate` and `deactivate` --- bin/pyenv-sh-activate | 17 +++++++--- bin/pyenv-sh-deactivate | 44 ++++++++++++++++---------- test/activate.bats | 26 ++++++++++++++-- test/deactivate.bats | 69 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 132 insertions(+), 24 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 8d98900..4535941 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -20,6 +20,12 @@ if [ "$1" = "--complete" ]; then exec pyenv-virtualenvs --bare fi +unset QUIET +if [ "$1" = "--quiet" ]; then + QUIET=1 + shift 1 +fi + if [ "$1" = "--unset" ]; then echo "pyenv deactivate" exit @@ -40,12 +46,13 @@ if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then fi if [ "${#versions[@]}" -gt 1 ]; then - echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 + [ -n "$QUIET" ] || echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2 echo "false" exit 1 fi -if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null; then +if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then + [ -n "$QUIET" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2 echo "false" exit 1 fi @@ -70,8 +77,10 @@ fish ) esac # Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile" -if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then - pyenv-virtualenv-init >&2 || true +if [ -z "$QUIET" ]; then + if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then + pyenv-virtualenv-init >&2 || true + fi fi if [ -z "$no_shell" ]; then diff --git a/bin/pyenv-sh-deactivate b/bin/pyenv-sh-deactivate index 3626b14..506f885 100755 --- a/bin/pyenv-sh-deactivate +++ b/bin/pyenv-sh-deactivate @@ -9,6 +9,12 @@ set -e [ -n "$PYENV_DEBUG" ] && set -x +unset QUIET +if [ "$1" = "--quiet" ]; then + QUIET=1 + shift 1 +fi + shell="$(basename "${PYENV_SHELL:-$SHELL}")" case "$shell" in fish ) @@ -27,23 +33,29 @@ fi case "$shell" in fish ) - cat <&2; - false; -end; -EOS + echo " setenv PYENV_DEACTIVATE \"\$VIRTUAL_ENV\";" ;; * ) - cat <&2; - false; -fi; -EOS + echo " export PYENV_DEACTIVATE=\"\$VIRTUAL_ENV\";" + ;; +esac + +cat <&2;" +fi + +echo " false;" + +case "$shell" in +fish ) + echo "end;" + ;; +* ) + echo "fi;" ;; esac diff --git a/test/activate.bats b/test/activate.bats index 44a8b15..03e3a64 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -137,7 +137,7 @@ EOS } @test "should fail if the version is not a virtualenv" { - stub pyenv-virtualenv-prefix "3.3.3 : echo \"not a virtualenv\" 1>&2; false" + stub pyenv-virtualenv-prefix "3.3.3 : false" run pyenv-sh-activate "3.3.3" @@ -145,7 +145,20 @@ EOS assert_failure assert_output </dev/null 2>&1; then + export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + deactivate; +else + false; +fi; +EOS +} + +@test "deactivate virtualenv (w/o pyenv-virtualenv-init)" { export PYENV_VIRTUALENV_INIT= PYENV_SHELL="bash" run pyenv-sh-deactivate @@ -41,6 +57,23 @@ fi; EOS } +@test "deactivate virtualenv (w/o pyenv-virtualenv-init) (quiet)" { + export PYENV_VIRTUALENV_INIT= + + PYENV_SHELL="bash" run pyenv-sh-deactivate --quiet + + assert_success + assert_output </dev/null 2>&1; then + pyenv shell --unset; + export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; + deactivate; +else + false; +fi; +EOS +} + @test "deactivate virtualenv (fish)" { export PYENV_VIRTUALENV_INIT=1 @@ -58,7 +91,23 @@ end; EOS } -@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init)" { +@test "deactivate virtualenv (fish) (quiet)" { + export PYENV_VIRTUALENV_INIT=1 + + PYENV_SHELL="fish" run pyenv-sh-deactivate --quiet + + assert_success + assert_output <