mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-12 21:43:51 -05:00
Activate a virtualenv of current version if specified version is not a virtualenv
```sh % pyenv versions system * miniconda3-3.16.0 (set by PYENV_VERSION environment variable) miniconda3-3.16.0/envs/venv % pyenv activate venv pyenv-virtualenv: deactivate miniconda3-3.16.0 pyenv-virtualenv: activate miniconda3-3.16.0/envs/venv ```
This commit is contained in:
@@ -81,11 +81,21 @@ if [ -n "${VIRTUAL_ENV}" ] && [[ "${VIRTUAL_ENV}" != "${PYENV_ROOT}/versions/"*
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then
|
if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then
|
||||||
if [ -z "${QUIET}" ]; then
|
# fallback to virtualenv of current version
|
||||||
echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2
|
OLDIFS="$IFS"
|
||||||
|
IFS=: current_versions=($(pyenv-version-name))
|
||||||
|
IFS="$OLDIFS"
|
||||||
|
new_venv="${current_versions%/envs/*}/envs/${venv}"
|
||||||
|
if pyenv-virtualenv-prefix "${new_venv}" 1>/dev/null 2>&1; then
|
||||||
|
venv="${new_venv}"
|
||||||
|
versions[0]="${new_venv}"
|
||||||
|
else
|
||||||
|
if [ -z "${QUIET}" ]; then
|
||||||
|
echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2
|
||||||
|
fi
|
||||||
|
echo "false"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "false"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# exit as error if there are multiple virtualenvs
|
# exit as error if there are multiple virtualenvs
|
||||||
|
|||||||
@@ -27,11 +27,6 @@ setup() {
|
|||||||
|
|
||||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
|
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
|
||||||
|
|
||||||
unstub pyenv-version-name
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
stub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -41,6 +36,11 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv) \${PS1}";
|
export PS1="(venv) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-version-name
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from current version (verbose)" {
|
@test "activate virtualenv from current version (verbose)" {
|
||||||
@@ -53,11 +53,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate --verbose
|
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate --verbose
|
||||||
|
|
||||||
unstub pyenv-version-name
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -67,6 +62,11 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv) \${PS1}";
|
export PS1="(venv) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-version-name
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from current version (without pyenv-virtualenv-init)" {
|
@test "activate virtualenv from current version (without pyenv-virtualenv-init)" {
|
||||||
@@ -79,11 +79,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
|
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate
|
||||||
|
|
||||||
unstub pyenv-version-name
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
# Load pyenv-virtualenv automatically by adding
|
# Load pyenv-virtualenv automatically by adding
|
||||||
@@ -100,6 +95,11 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv) \${PS1}";
|
export PS1="(venv) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-version-name
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from current version (fish)" {
|
@test "activate virtualenv from current version (fish)" {
|
||||||
@@ -112,11 +112,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
|
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
|
||||||
|
|
||||||
unstub pyenv-version-name
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -124,6 +119,11 @@ pyenv-virtualenv: activate venv
|
|||||||
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
|
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
|
||||||
pyenv-virtualenv: prompt changing not work for fish.
|
pyenv-virtualenv: prompt changing not work for fish.
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-version-name
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from current version (fish) (without pyenv-virtualenv-init)" {
|
@test "activate virtualenv from current version (fish) (without pyenv-virtualenv-init)" {
|
||||||
@@ -136,11 +136,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
|
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate
|
||||||
|
|
||||||
unstub pyenv-version-name
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
# Load pyenv-virtualenv automatically by adding
|
# Load pyenv-virtualenv automatically by adding
|
||||||
@@ -155,6 +150,11 @@ setenv PYENV_ACTIVATE_SHELL 1;
|
|||||||
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
|
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv";
|
||||||
pyenv-virtualenv: prompt changing not work for fish.
|
pyenv-virtualenv: prompt changing not work for fish.
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-version-name
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from command-line argument" {
|
@test "activate virtualenv from command-line argument" {
|
||||||
@@ -166,10 +166,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -181,6 +177,10 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv27) \${PS1}";
|
export PS1="(venv27) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from command-line argument (without pyenv-virtualenv-init)" {
|
@test "activate virtualenv from command-line argument (without pyenv-virtualenv-init)" {
|
||||||
@@ -192,10 +192,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
# Load pyenv-virtualenv automatically by adding
|
# Load pyenv-virtualenv automatically by adding
|
||||||
@@ -212,6 +208,10 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv27) \${PS1}";
|
export PS1="(venv27) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from command-line argument (fish)" {
|
@test "activate virtualenv from command-line argument (fish)" {
|
||||||
@@ -223,10 +223,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -236,6 +232,10 @@ setenv PYENV_ACTIVATE_SHELL 1;
|
|||||||
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
|
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
|
||||||
pyenv-virtualenv: prompt changing not work for fish.
|
pyenv-virtualenv: prompt changing not work for fish.
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate virtualenv from command-line argument (fish) (without pyenv-virtualenv-init)" {
|
@test "activate virtualenv from command-line argument (fish) (without pyenv-virtualenv-init)" {
|
||||||
@@ -247,10 +247,6 @@ EOS
|
|||||||
|
|
||||||
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
# Load pyenv-virtualenv automatically by adding
|
# Load pyenv-virtualenv automatically by adding
|
||||||
@@ -265,6 +261,10 @@ setenv PYENV_ACTIVATE_SHELL 1;
|
|||||||
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
|
setenv VIRTUAL_ENV "${PYENV_ROOT}/versions/venv27";
|
||||||
pyenv-virtualenv: prompt changing not work for fish.
|
pyenv-virtualenv: prompt changing not work for fish.
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "unset invokes deactivate" {
|
@test "unset invokes deactivate" {
|
||||||
@@ -275,16 +275,18 @@ EOS
|
|||||||
|
|
||||||
run pyenv-sh-activate --unset
|
run pyenv-sh-activate --unset
|
||||||
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 : false"
|
stub pyenv-virtualenv-prefix "3.3.3 : false"
|
||||||
|
stub pyenv-version-name " : echo 3.3.3"
|
||||||
|
stub pyenv-virtualenv-prefix "3.3.3/envs/3.3.3 : false"
|
||||||
|
|
||||||
run pyenv-sh-activate "3.3.3"
|
run pyenv-sh-activate "3.3.3"
|
||||||
|
|
||||||
@@ -293,19 +295,25 @@ EOS
|
|||||||
pyenv-virtualenv: version \`3.3.3' is not a virtualenv
|
pyenv-virtualenv: version \`3.3.3' is not a virtualenv
|
||||||
false
|
false
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-version-name
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "should fail if the version is not a virtualenv (quiet)" {
|
@test "should fail if the version is not a virtualenv (quiet)" {
|
||||||
stub pyenv-virtualenv-prefix "3.3.3 : false"
|
stub pyenv-virtualenv-prefix "3.3.3 : false"
|
||||||
|
stub pyenv-version-name " : echo 3.3.3"
|
||||||
|
stub pyenv-virtualenv-prefix "3.3.3/envs/3.3.3 : false"
|
||||||
|
|
||||||
run pyenv-sh-activate --quiet "3.3.3"
|
run pyenv-sh-activate --quiet "3.3.3"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
|
|
||||||
assert_failure
|
assert_failure
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
false
|
false
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-version-name
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "should fail if there are multiple versions" {
|
@test "should fail if there are multiple versions" {
|
||||||
@@ -314,13 +322,13 @@ EOS
|
|||||||
|
|
||||||
run pyenv-sh-activate "venv" "venv27"
|
run pyenv-sh-activate "venv" "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
|
|
||||||
assert_failure
|
assert_failure
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
pyenv-virtualenv: cannot activate multiple versions at once: venv venv27
|
pyenv-virtualenv: cannot activate multiple versions at once: venv venv27
|
||||||
false
|
false
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "should fail if there are multiple virtualenvs (quiet)" {
|
@test "should fail if there are multiple virtualenvs (quiet)" {
|
||||||
@@ -329,18 +337,20 @@ EOS
|
|||||||
|
|
||||||
run pyenv-sh-activate --quiet "venv" "venv27"
|
run pyenv-sh-activate --quiet "venv" "venv27"
|
||||||
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
|
|
||||||
assert_failure
|
assert_failure
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
false
|
false
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "should fail if the first version is not a virtualenv" {
|
@test "should fail if the first version is not a virtualenv" {
|
||||||
export PYENV_VIRTUALENV_INIT=1
|
export PYENV_VIRTUALENV_INIT=1
|
||||||
|
|
||||||
stub pyenv-virtualenv-prefix "2.7.10 : false"
|
stub pyenv-virtualenv-prefix "2.7.10 : false"
|
||||||
|
stub pyenv-version-name " : echo 2.7.10"
|
||||||
|
stub pyenv-virtualenv-prefix "2.7.10/envs/2.7.10 : false"
|
||||||
|
|
||||||
run pyenv-sh-activate "2.7.10" "venv27"
|
run pyenv-sh-activate "2.7.10" "venv27"
|
||||||
|
|
||||||
@@ -349,6 +359,9 @@ EOS
|
|||||||
pyenv-virtualenv: version \`2.7.10' is not a virtualenv
|
pyenv-virtualenv: version \`2.7.10' is not a virtualenv
|
||||||
false
|
false
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-version-name
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "activate if the first virtualenv is a virtualenv" {
|
@test "activate if the first virtualenv is a virtualenv" {
|
||||||
@@ -361,10 +374,6 @@ EOS
|
|||||||
|
|
||||||
run pyenv-sh-activate "venv27" "2.7.10"
|
run pyenv-sh-activate "venv27" "2.7.10"
|
||||||
|
|
||||||
unstub pyenv-sh-deactivate
|
|
||||||
unstub pyenv-virtualenv-prefix
|
|
||||||
unstub pyenv-prefix
|
|
||||||
|
|
||||||
assert_success
|
assert_success
|
||||||
assert_output <<EOS
|
assert_output <<EOS
|
||||||
deactivated
|
deactivated
|
||||||
@@ -376,6 +385,10 @@ pyenv-virtualenv: prompt changing will be removed from future release. configure
|
|||||||
export _OLD_VIRTUAL_PS1="\${PS1}";
|
export _OLD_VIRTUAL_PS1="\${PS1}";
|
||||||
export PS1="(venv27) \${PS1}";
|
export PS1="(venv27) \${PS1}";
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
unstub pyenv-sh-deactivate
|
||||||
|
unstub pyenv-virtualenv-prefix
|
||||||
|
unstub pyenv-prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "should fail if activate is invoked as a command" {
|
@test "should fail if activate is invoked as a command" {
|
||||||
|
|||||||
Reference in New Issue
Block a user