From 116e133c906cfdfa60cf59a4f458f6f5f3b45e5e Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Thu, 26 Nov 2015 23:53:00 +0000 Subject: [PATCH] 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 ``` --- bin/pyenv-sh-activate | 18 +++++-- test/activate.bats | 119 +++++++++++++++++++++++------------------- 2 files changed, 80 insertions(+), 57 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index be7e453..6f8e90c 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -81,11 +81,21 @@ if [ -n "${VIRTUAL_ENV}" ] && [[ "${VIRTUAL_ENV}" != "${PYENV_ROOT}/versions/"* fi if ! pyenv-virtualenv-prefix "${venv}" 1>/dev/null 2>&1; then - if [ -z "${QUIET}" ]; then - echo "pyenv-virtualenv: version \`${venv}' is not a virtualenv" 1>&2 + # fallback to virtualenv of current version + 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 - echo "false" - exit 1 fi # exit as error if there are multiple virtualenvs diff --git a/test/activate.bats b/test/activate.bats index 4b48130..e249178 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -27,11 +27,6 @@ setup() { 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_output <