Add --quiet option to activate and deactivate

This commit is contained in:
Yamashita Yuu
2014-07-04 20:47:10 +09:00
parent 5b3b909089
commit f0c400b669
4 changed files with 132 additions and 24 deletions

View File

@@ -20,6 +20,12 @@ if [ "$1" = "--complete" ]; then
exec pyenv-virtualenvs --bare exec pyenv-virtualenvs --bare
fi fi
unset QUIET
if [ "$1" = "--quiet" ]; then
QUIET=1
shift 1
fi
if [ "$1" = "--unset" ]; then if [ "$1" = "--unset" ]; then
echo "pyenv deactivate" echo "pyenv deactivate"
exit exit
@@ -40,12 +46,13 @@ if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
fi fi
if [ "${#versions[@]}" -gt 1 ]; then 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" echo "false"
exit 1 exit 1
fi 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" echo "false"
exit 1 exit 1
fi fi
@@ -70,9 +77,11 @@ fish )
esac esac
# Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile" # Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile"
if [ -z "$QUIET" ]; then
if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then
pyenv-virtualenv-init >&2 || true pyenv-virtualenv-init >&2 || true
fi fi
fi
if [ -z "$no_shell" ]; then if [ -z "$no_shell" ]; then
echo "pyenv shell \"${versions}\";" echo "pyenv shell \"${versions}\";"

View File

@@ -9,6 +9,12 @@
set -e set -e
[ -n "$PYENV_DEBUG" ] && set -x [ -n "$PYENV_DEBUG" ] && set -x
unset QUIET
if [ "$1" = "--quiet" ]; then
QUIET=1
shift 1
fi
shell="$(basename "${PYENV_SHELL:-$SHELL}")" shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in case "$shell" in
fish ) fish )
@@ -27,23 +33,29 @@ fi
case "$shell" in case "$shell" in
fish ) fish )
cat <<EOS echo " setenv PYENV_DEACTIVATE \"\$VIRTUAL_ENV\";"
setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
else
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
false;
end;
EOS
;; ;;
* ) * )
cat <<EOS echo " export PYENV_DEACTIVATE=\"\$VIRTUAL_ENV\";"
export PYENV_DEACTIVATE="\$VIRTUAL_ENV"; ;;
deactivate; esac
else
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2; cat <<EOS
false; deactivate;
fi; else
EOS EOS
if [ -z "$QUIET" ]; then
echo " echo \"pyenv-virtualenv: no virtualenv has been activated.\" 1>&2;"
fi
echo " false;"
case "$shell" in
fish )
echo "end;"
;;
* )
echo "fi;"
;; ;;
esac esac

View File

@@ -137,7 +137,7 @@ EOS
} }
@test "should fail if the version is not a virtualenv" { @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" run pyenv-sh-activate "3.3.3"
@@ -145,7 +145,20 @@ EOS
assert_failure assert_failure
assert_output <<EOS assert_output <<EOS
not a virtualenv pyenv-virtualenv: version \`3.3.3' is not a virtualenv
false
EOS
}
@test "should fail if the version is not a virtualenv (quiet)" {
stub pyenv-virtualenv-prefix "3.3.3 : false"
run pyenv-sh-activate --quiet "3.3.3"
unstub pyenv-virtualenv-prefix
assert_failure
assert_output <<EOS
false false
EOS EOS
} }
@@ -160,6 +173,15 @@ false
EOS EOS
} }
@test "should fail if there are multiple versions (quiet)" {
run pyenv-sh-activate --quiet "venv" "venv27"
assert_failure
assert_output <<EOS
false
EOS
}
@test "should fail if activate is invoked as a command" { @test "should fail if activate is invoked as a command" {
run pyenv-activate run pyenv-activate

View File

@@ -23,7 +23,23 @@ fi;
EOS EOS
} }
@test "deactivate virtualenv (without pyenv-virtualenv-init)" { @test "deactivate virtualenv (quiet)" {
export PYENV_VIRTUALENV_INIT=1
PYENV_SHELL="bash" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
if declare -f deactivate 1>/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= export PYENV_VIRTUALENV_INIT=
PYENV_SHELL="bash" run pyenv-sh-deactivate PYENV_SHELL="bash" run pyenv-sh-deactivate
@@ -41,6 +57,23 @@ fi;
EOS 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 <<EOS
if declare -f deactivate 1>/dev/null 2>&1; then
pyenv shell --unset;
export PYENV_DEACTIVATE="\$VIRTUAL_ENV";
deactivate;
else
false;
fi;
EOS
}
@test "deactivate virtualenv (fish)" { @test "deactivate virtualenv (fish)" {
export PYENV_VIRTUALENV_INIT=1 export PYENV_VIRTUALENV_INIT=1
@@ -58,7 +91,23 @@ end;
EOS 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 <<EOS
if functions -q deactivate
setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
else
false;
end;
EOS
}
@test "deactivate virtualenv (fish) (w/o pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT= export PYENV_VIRTUALENV_INIT=
PYENV_SHELL="fish" run pyenv-sh-deactivate PYENV_SHELL="fish" run pyenv-sh-deactivate
@@ -76,6 +125,22 @@ end;
EOS EOS
} }
@test "deactivate virtualenv (fish) (w/o pyenv-virtualenv-init) (quiet)" {
export PYENV_VIRTUALENV_INIT=
PYENV_SHELL="fish" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
if functions -q deactivate
pyenv shell --unset;
setenv PYENV_DEACTIVATE "\$VIRTUAL_ENV";
deactivate;
else
false;
end;
EOS
}
@test "should fail if deactivate is invoked as a command" { @test "should fail if deactivate is invoked as a command" {
run pyenv-deactivate run pyenv-deactivate