Merge pull request #110 from yyuu/init-refactor

Refactor init code
This commit is contained in:
Yamashita, Yuu
2015-11-07 03:39:19 +09:00
8 changed files with 87 additions and 114 deletions

View File

@@ -14,7 +14,7 @@
set -e
[ -n "$PYENV_DEBUG" ] && set -x
unset NOERROR
unset QUIET
unset VERBOSE
while [ $# -gt 0 ]; do
@@ -24,8 +24,8 @@ while [ $# -gt 0 ]; do
echo --unset
exec pyenv-virtualenvs --bare
;;
"--no-error" )
NOERROR=1
"--no-error" | "--quiet" )
QUIET=1
;;
"--unset" )
echo "pyenv deactivate"
@@ -57,14 +57,14 @@ if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
fi
if [ "${#versions[@]}" -gt 1 ]; then
[ -n "$NOERROR" ] || 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-prefix "${versions}" 1>/dev/null 2>&1; then
if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then
[ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2
[ -n "$QUIET" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2
echo "false"
exit 1
fi
@@ -72,7 +72,7 @@ else
version="$(pyenv-version-name)"
versions="${version%/envs/*}/envs/${versions}"
if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then
[ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not an environment of anaconda/miniconda" 1>&2
[ -n "$QUIET" ] || echo "pyenv-virtualenv: version \`${versions}' is not an environment of anaconda/miniconda" 1>&2
echo "false"
exit 1
fi
@@ -87,9 +87,7 @@ if [ -z "$PYENV_VIRTUALENV_INIT" ]; then
pyenv-virtualenv-init >&2 || true
fi
if [ -n "$VERBOSE" ]; then
echo "pyenv-virtualenv: activate ${versions}" 1>&2
fi
if [ -z "$no_shell" ]; then
echo "pyenv shell \"${versions}\";"
@@ -163,7 +161,3 @@ EOS
;;
esac
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT}" ]; then
: # TODO: change prompt?
fi

View File

@@ -9,13 +9,13 @@
set -e
[ -n "$PYENV_DEBUG" ] && set -x
unset NOERROR
unset QUIET
unset VERBOSE
while [ $# -gt 0 ]; do
case "$1" in
"--no-error" )
NOERROR=1
"--no-error" | "--quiet")
QUIET=1
;;
"--verbose" )
VERBOSE=1
@@ -28,17 +28,20 @@ while [ $# -gt 0 ]; do
done
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
prefix="${PYENV_ACTIVATE:-${VIRTUAL_ENV}}"
prefix="${VIRTUAL_ENV:-${PYENV_ACTIVATE}}"
if [ -z "${VIRTUAL_ENV}" ]; then
[ -n "$NOERROR" ] || echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2
if [ -z "${prefix}" ]; then
[ -n "$QUIET" ] || echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2
echo "false"
exit 1
fi
if [ -n "$VERBOSE" ]; then
echo "pyenv-virtualenv: deactivate ${prefix##*/}" 1>&2
if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then
version="$(basename "${prefix%/envs/*}")/envs/${prefix##*/}"
else
version="${prefix##*/}"
fi
echo "pyenv-virtualenv: deactivate ${version}" 1>&2
if [ -n "${PYENV_ACTIVATE_SHELL}" ]; then
# shell version set in pyenv-sh-activate should be unset
@@ -108,7 +111,3 @@ EOS
;;
esac
fi
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT}" ]; then
: # TODO: change prompt?
fi

View File

@@ -102,25 +102,15 @@ case "$shell" in
fish )
cat <<EOS
function _pyenv_virtualenv_hook --on-event fish_prompt;
set -l PYENV_PREFIX (pyenv prefix 2>/dev/null; or true)
set -l ret \$status
if [ -n "\$PYENV_ACTIVATE" ]
if [ (pyenv version-name 2>/dev/null; or true) = "system" ]
pyenv deactivate --no-error --verbose
set -e PYENV_DEACTIVATE
return \$ret
end
if [ "\$PYENV_ACTIVATE" != "\$PYENV_PREFIX" ]
if pyenv deactivate --no-error --verbose
set -e PYENV_DEACTIVATE
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
else
pyenv activate --no-error --verbose
end
if [ -n "\$VIRTUAL_ENV" ]
if [ "\$VIRTUAL_ENV" != (pyenv prefix 2>/dev/null; or true) ]
pyenv deactivate --quiet; or true
pyenv activate --quiet; or true
end
else
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != "\$PYENV_PREFIX" ]
pyenv activate --no-error --verbose; or true
if [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
pyenv activate --quiet; or true
end
end
return \$ret
@@ -142,23 +132,14 @@ esac
if [[ "$shell" != "fish" ]]; then
cat <<EOS
local ret=\$?
if [ -n "\$PYENV_ACTIVATE" ]; then
if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then
eval "\$(pyenv sh-deactivate --no-error --verbose)"
unset PYENV_DEACTIVATE
return \$ret
fi
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then
unset PYENV_DEACTIVATE
eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE
else
eval "\$(pyenv sh-activate --no-error --verbose)"
fi
if [ -n "\$VIRTUAL_ENV" ]; then
if [ "\$VIRTUAL_ENV" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-deactivate --quiet || true)" || true
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
else
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --no-error --verbose)" || true
if [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
fi
return \$ret

View File

@@ -23,6 +23,7 @@ setup() {
assert_success
assert_output <<EOS
pyenv-virtualenv: activate venv
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv";
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv";
@@ -73,6 +74,7 @@ EOS
eval "\$(pyenv virtualenv-init -)"
pyenv-virtualenv: activate venv
pyenv shell "venv";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;
@@ -97,6 +99,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate venv
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${PYENV_ROOT}/versions/venv";
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
@@ -124,6 +127,7 @@ EOS
status --is-interactive; and . (pyenv virtualenv-init -|psub)
pyenv-virtualenv: activate venv
pyenv shell "venv";
setenv PYENV_ACTIVATE_SHELL 1;
set -e PYENV_DEACTIVATE;
@@ -146,6 +150,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate venv27
pyenv shell "venv27";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;
@@ -173,6 +178,7 @@ EOS
eval "\$(pyenv virtualenv-init -)"
pyenv-virtualenv: activate venv27
pyenv shell "venv27";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;
@@ -195,6 +201,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate venv27
pyenv shell "venv27";
setenv PYENV_ACTIVATE_SHELL 1;
set -e PYENV_DEACTIVATE;
@@ -222,6 +229,7 @@ EOS
status --is-interactive; and . (pyenv virtualenv-init -|psub)
pyenv-virtualenv: activate venv27
pyenv shell "venv27";
setenv PYENV_ACTIVATE_SHELL 1;
set -e PYENV_DEACTIVATE;
@@ -255,11 +263,11 @@ false
EOS
}
@test "should fail if the version is not a virtualenv (no-error)" {
@test "should fail if the version is not a virtualenv (quiet)" {
stub pyenv-virtualenv-prefix "3.3.3 : false"
stub pyenv-prefix "3.3.3 : echo \"${PYENV_ROOT}/versions/3.3.3\""
run pyenv-sh-activate --no-error "3.3.3"
run pyenv-sh-activate --quiet "3.3.3"
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix
@@ -280,8 +288,8 @@ false
EOS
}
@test "should fail if there are multiple versions (no-error)" {
run pyenv-sh-activate --no-error "venv" "venv27"
@test "should fail if there are multiple versions (quiet)" {
run pyenv-sh-activate --quiet "venv" "venv27"
assert_failure
assert_output <<EOS

View File

@@ -24,6 +24,7 @@ setup() {
assert_success
assert_output <<EOS
pyenv-virtualenv: activate anaconda-2.3.0
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/anaconda-2.3.0";
export VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0";
@@ -48,6 +49,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate anaconda-2.3.0
set -e PYENV_DEACTIVATE;
setenv PYENV_ACTIVATE "${TMP}/pyenv/versions/anaconda-2.3.0";
setenv VIRTUAL_ENV "${TMP}/pyenv/versions/anaconda-2.3.0";
@@ -71,6 +73,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate miniconda-3.9.1
pyenv shell "miniconda-3.9.1";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;
@@ -97,6 +100,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate anaconda-2.3.0/envs/foo
unset PYENV_DEACTIVATE;
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/anaconda-2.3.0/envs/foo";
export VIRTUAL_ENV="${PYENV_ROOT}/versions/anaconda-2.3.0/envs/foo";
@@ -120,6 +124,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate miniconda-3.9.1/envs/bar
pyenv shell "miniconda-3.9.1/envs/bar";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;
@@ -146,6 +151,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: activate miniconda-3.9.1/envs/bar
pyenv shell "miniconda-3.9.1/envs/bar";
export PYENV_ACTIVATE_SHELL=1;
unset PYENV_DEACTIVATE;

View File

@@ -18,6 +18,7 @@ setup() {
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate anaconda-2.3.0
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
unset PYENV_ACTIVATE;
unset VIRTUAL_ENV;
@@ -38,6 +39,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate anaconda-2.3.0
setenv PYENV_DEACTIVATE "${TMP}/pyenv/versions/anaconda-2.3.0";
set -e PYENV_ACTIVATE;
set -e VIRTUAL_ENV;
@@ -58,6 +60,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate anaconda-2.3.0/envs/foo
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
unset PYENV_ACTIVATE;
unset VIRTUAL_ENV;

View File

@@ -15,6 +15,7 @@ setup() {
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
unset PYENV_ACTIVATE;
unset VIRTUAL_ENV;
@@ -37,15 +38,16 @@ unset VIRTUAL_ENV;
EOS
}
@test "deactivate virtualenv (no-error)" {
@test "deactivate virtualenv (quiet)" {
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE_SHELL=
PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error
PYENV_SHELL="bash" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
unset PYENV_ACTIVATE;
unset VIRTUAL_ENV;
@@ -61,6 +63,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
pyenv shell --unset;
unset PYENV_ACTIVATE_SHELL;
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
@@ -69,15 +72,16 @@ unset VIRTUAL_ENV;
EOS
}
@test "deactivate virtualenv (with shell activation) (no-error)" {
@test "deactivate virtualenv (with shell activation) (quiet)" {
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE_SHELL=1
PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error
PYENV_SHELL="bash" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
pyenv shell --unset;
unset PYENV_ACTIVATE_SHELL;
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
@@ -95,6 +99,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
export PYENV_DEACTIVATE="$VIRTUAL_ENV";
unset PYENV_ACTIVATE;
unset VIRTUAL_ENV;
@@ -110,21 +115,23 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
set -e PYENV_ACTIVATE;
set -e VIRTUAL_ENV;
EOS
}
@test "deactivate virtualenv (fish) (no-error)" {
@test "deactivate virtualenv (fish) (quiet)" {
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE_SHELL=
PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error
PYENV_SHELL="fish" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
set -e PYENV_ACTIVATE;
set -e VIRTUAL_ENV;
@@ -140,6 +147,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
pyenv shell --unset;
set -e PYENV_ACTIVATE_SHELL;
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
@@ -148,15 +156,16 @@ set -e VIRTUAL_ENV;
EOS
}
@test "deactivate virtualenv (fish) (with shell activation) (no-error)" {
@test "deactivate virtualenv (fish) (with shell activation) (quiet)" {
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
export PYENV_ACTIVATE_SHELL=1
PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error
PYENV_SHELL="fish" run pyenv-sh-deactivate --quiet
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
pyenv shell --unset;
set -e PYENV_ACTIVATE_SHELL;
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
@@ -174,6 +183,7 @@ EOS
assert_success
assert_output <<EOS
pyenv-virtualenv: deactivate venv
setenv PYENV_DEACTIVATE "$VIRTUAL_ENV";
set -e PYENV_ACTIVATE;
set -e VIRTUAL_ENV;

View File

@@ -54,23 +54,14 @@ export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:${PATH}";
export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() {
local ret=\$?
if [ -n "\$PYENV_ACTIVATE" ]; then
if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then
eval "\$(pyenv sh-deactivate --no-error --verbose)"
unset PYENV_DEACTIVATE
return \$ret
fi
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then
unset PYENV_DEACTIVATE
eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE
else
eval "\$(pyenv sh-activate --no-error --verbose)"
fi
if [ -n "\$VIRTUAL_ENV" ]; then
if [ "\$VIRTUAL_ENV" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-deactivate --quiet || true)" || true
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
else
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --no-error --verbose)" || true
if [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
fi
return \$ret
@@ -89,25 +80,15 @@ EOS
setenv PATH '${TMP}/pyenv/plugins/pyenv-virtualenv/shims' \$PATH;
setenv PYENV_VIRTUALENV_INIT 1;
function _pyenv_virtualenv_hook --on-event fish_prompt;
set -l PYENV_PREFIX (pyenv prefix 2>/dev/null; or true)
set -l ret \$status
if [ -n "\$PYENV_ACTIVATE" ]
if [ (pyenv version-name 2>/dev/null; or true) = "system" ]
pyenv deactivate --no-error --verbose
set -e PYENV_DEACTIVATE
return \$ret
end
if [ "\$PYENV_ACTIVATE" != "\$PYENV_PREFIX" ]
if pyenv deactivate --no-error --verbose
set -e PYENV_DEACTIVATE
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
else
pyenv activate --no-error --verbose
end
if [ -n "\$VIRTUAL_ENV" ]
if [ "\$VIRTUAL_ENV" != (pyenv prefix 2>/dev/null; or true) ]
pyenv deactivate --quiet; or true
pyenv activate --quiet; or true
end
else
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != "\$PYENV_PREFIX" ]
pyenv activate --no-error --verbose; or true
if [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null; or true) ]
pyenv activate --quiet; or true
end
end
return \$ret
@@ -124,23 +105,14 @@ export PATH="${TMP}/pyenv/plugins/pyenv-virtualenv/shims:${PATH}";
export PYENV_VIRTUALENV_INIT=1;
_pyenv_virtualenv_hook() {
local ret=\$?
if [ -n "\$PYENV_ACTIVATE" ]; then
if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then
eval "\$(pyenv sh-deactivate --no-error --verbose)"
unset PYENV_DEACTIVATE
return \$ret
fi
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then
unset PYENV_DEACTIVATE
eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE
else
eval "\$(pyenv sh-activate --no-error --verbose)"
fi
if [ -n "\$VIRTUAL_ENV" ]; then
if [ "\$VIRTUAL_ENV" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-deactivate --quiet || true)" || true
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
else
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --no-error --verbose)" || true
if [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then
eval "\$(pyenv sh-activate --quiet || true)" || true
fi
fi
return \$ret