From 6132f4228ee9729f7ea9eada6ed46c1adf833e8a Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sat, 18 Jul 2015 21:17:08 +0900 Subject: [PATCH 1/7] *EXPERIMENTAL* a support for _envs_ created by anaconda/miniconda --- bin/pyenv-virtualenv-prefix | 17 ++++++++++++++--- bin/pyenv-virtualenvs | 10 ++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index a762498..b1ae326 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -31,7 +31,8 @@ base_prefix() { # pyvenv } VIRTUALENV_PREFIX_PATHS=() -for version in "${versions[@]}"; do +for version_string in "${versions[@]}"; do + version="${version_string%%/*}" if [ "$version" = "system" ]; then echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2 exit 1 @@ -40,8 +41,18 @@ for version in "${versions[@]}"; do if [ -f "${PREFIX}/bin/activate" ]; then # Anaconda has `activate` script nevertheless it is not a virtual environment (#65) if [ -f "${PREFIX}/bin/conda" ]; then - echo "pyenv-virtualenv: version \`${version}' is an anaconda/miniconda" 1>&2 - exit 1 + if [[ "${version_string}" != "${version_string%/envs/*}" ]]; then + conda_env="${version_string##*/envs/}" + if [ -f "${PREFIX}/envs/${conda_env}/bin/conda" ]; then + VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}/envs/${conda_env}") + else + echo "pyenv-virtualenv: version \`${version_string}' is not an environment of anaconda/miniconda" 1>&2 + exit 1 + fi + else + echo "pyenv-virtualenv: version \`${version_string}' is not a virtualenv" 1>&2 + exit 1 + fi else VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)" VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH") diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index 614299c..c550b1c 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -51,6 +51,16 @@ for version in $(pyenv-versions --bare); do virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)" if [ -d "${virtualenv_prefix}" ]; then print_version "${version}" " (created from ${virtualenv_prefix})" + else + # envs of anaconda/miniconda + prefix="$(pyenv-prefix "${version}")" + if [ -f "${prefix}/bin/activate" ] && [ -f "${prefix}/bin/conda" ]; then + shopt -s nullglob + for conda_env in "${prefix}/envs/"*; do + print_version "${version##*/}${conda_env#${prefix}}" " (created from ${prefix})" + done + shopt -u nullglob + fi fi fi done From 65c74dbf2d17e1db2ba7c70410f9dd0882a51579 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sat, 18 Jul 2015 23:24:25 +0900 Subject: [PATCH 2/7] activate/deactivate anaconda/miniconda envs --- bin/pyenv-sh-activate | 48 ++++++++++++++++++++++++----- bin/pyenv-sh-deactivate | 67 +++++++++++++++++++++++++++++++---------- 2 files changed, 91 insertions(+), 24 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 323c1b1..3c416ba 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -69,6 +69,29 @@ if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then fi shell="${PYENV_SHELL:-${SHELL##*/}}" +prefix="$(pyenv-prefix "${versions}")" + +unset conda_env + +if [ -f "${prefix}/bin/conda" ]; then + if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then + case "$shell" in + bash ) + conda_env="${prefix##*/envs/}" + ;; + zsh ) + conda_env="${prefix##*/envs/}" + ;; + * ) + echo "false" + exit 1 + ;; + esac + else + echo "false" + exit 1 + fi +fi # Display setup instruction, if pyenv-virtualenv has not been initialized. # if 'pyenv virtualenv-init -' is not found in "$profile" @@ -94,20 +117,29 @@ if [ -z "$no_shell" ]; then esac fi -prefix="$(pyenv-prefix "${versions}")" -case "$shell" in -fish ) +if [ -n "${conda_env}" ]; then + # anaconda/miniconda cat </dev/null 2>&1; then" - ;; -esac - prefix="${PYENV_ACTIVATE:-${VIRTUAL_ENV}}" +unset conda_env + +if [ -f "${prefix}/bin/conda" ]; then + if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then + case "$shell" in + bash ) + conda_env="${prefix##*/envs/}" + ;; + zsh ) + conda_env="${prefix##*/envs/}" + ;; + * ) + echo "false" + exit 1 + ;; + esac + else + echo "false" + exit 1 + fi +fi + +if [ -n "${conda_env}" ]; then + echo "if [ -f \"${prefix%/envs/*}/bin/deactivate\" ]; then" +else + case "$shell" in + fish ) + echo "if functions -q deactivate;" + ;; + * ) + echo "if declare -f deactivate 1>/dev/null 2>&1; then" + ;; + esac +fi + if [ -n "$VERBOSE" ]; then echo " echo \"pyenv-virtualenv: deactivate ${prefix##*/}\" 1>&2;" fi @@ -57,24 +83,33 @@ if [ -n "${PYENV_ACTIVATE_SHELL}" ]; then esac fi -case "$shell" in -fish ) +if [ -n "${conda_env}" ]; then cat <&2;" From e1523811fb83df6249cf9e0ad5669f00457561ba Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 19 Jul 2015 00:07:43 +0900 Subject: [PATCH 3/7] fix broken test --- test/virtualenvs.bats | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/virtualenvs.bats b/test/virtualenvs.bats index 80be0d4..e27fc66 100644 --- a/test/virtualenvs.bats +++ b/test/virtualenvs.bats @@ -17,12 +17,15 @@ setup() { stub pyenv-virtualenv-prefix "3.3.3 : false" stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\"" stub pyenv-virtualenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\"" + stub pyenv-prefix "2.7.6 : echo \"${PYENV_ROOT}/versions/2.7.6\"" + stub pyenv-prefix "3.3.3 : echo \"${PYENV_ROOT}/versions/3.3.3\"" run pyenv-virtualenvs unstub pyenv-version-name unstub pyenv-versions unstub pyenv-virtualenv-prefix + unstub pyenv-prefix assert_success assert_output < Date: Sun, 19 Jul 2015 01:00:24 +0900 Subject: [PATCH 4/7] Fix `Error: too many arguments.` on deactivating anaconda env --- bin/pyenv-sh-activate | 2 +- bin/pyenv-sh-deactivate | 2 +- bin/pyenv-virtualenv-init | 10 +++++----- test/init.bats | 20 ++++++++++---------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 3c416ba..ad1cba5 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -122,7 +122,7 @@ if [ -n "${conda_env}" ]; then cat </dev/null || true)" = "system" ]; then - pyenv deactivate --no-error --verbose + eval "\$(pyenv sh-deactivate --no-error --verbose)" unset PYENV_DEACTIVATE return 0 fi if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - if pyenv deactivate --no-error --verbose; then + if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then unset PYENV_DEACTIVATE - pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE else - pyenv activate --no-error --verbose + eval "\$(pyenv sh-activate --no-error --verbose)" fi fi else if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - pyenv activate --no-error --verbose || true + eval "\$(pyenv sh-activate --no-error --verbose)" || true fi fi }; diff --git a/test/init.bats b/test/init.bats index 6d36e2a..3f2ffa6 100644 --- a/test/init.bats +++ b/test/init.bats @@ -51,21 +51,21 @@ export PYENV_VIRTUALENV_INIT=1; _pyenv_virtualenv_hook() { if [ -n "\$PYENV_ACTIVATE" ]; then if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then - pyenv deactivate --no-error --verbose + eval "\$(pyenv sh-deactivate --no-error --verbose)" unset PYENV_DEACTIVATE return 0 fi if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - if pyenv deactivate --no-error --verbose; then + if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then unset PYENV_DEACTIVATE - pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE else - pyenv activate --no-error --verbose + eval "\$(pyenv sh-activate --no-error --verbose)" fi fi else if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - pyenv activate --no-error --verbose || true + eval "\$(pyenv sh-activate --no-error --verbose)" || true fi fi }; @@ -113,21 +113,21 @@ export PYENV_VIRTUALENV_INIT=1; _pyenv_virtualenv_hook() { if [ -n "\$PYENV_ACTIVATE" ]; then if [ "\$(pyenv version-name 2>/dev/null || true)" = "system" ]; then - pyenv deactivate --no-error --verbose + eval "\$(pyenv sh-deactivate --no-error --verbose)" unset PYENV_DEACTIVATE return 0 fi if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - if pyenv deactivate --no-error --verbose; then + if eval "\$(pyenv sh-deactivate --no-error --verbose)"; then unset PYENV_DEACTIVATE - pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE + eval "\$(pyenv sh-activate --no-error --verbose)" || unset PYENV_DEACTIVATE else - pyenv activate --no-error --verbose + eval "\$(pyenv sh-activate --no-error --verbose)" fi fi else if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null || true)" ]; then - pyenv activate --no-error --verbose || true + eval "\$(pyenv sh-activate --no-error --verbose)" || true fi fi }; From 25395f033106744d392809cf0fdd5a2086ddd387 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 19 Jul 2015 03:38:48 +0900 Subject: [PATCH 5/7] activate: look for a conda environment if given name is not a pyenv's prefix --- bin/pyenv-sh-activate | 17 +++++++++++++---- test/activate.bats | 13 +++++++++++++ test/init.bats | 14 ++++++++------ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index ad1cba5..046d030 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -62,10 +62,19 @@ if [ "${#versions[@]}" -gt 1 ]; then exit 1 fi -if ! pyenv-virtualenv-prefix "${versions}" 1>/dev/null 2>&1; then - [ -n "$NOERROR" ] || echo "pyenv-virtualenv: version \`${versions}' is not a virtualenv" 1>&2 - echo "false" - exit 1 +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 + echo "false" + exit 1 + fi +else + versions="$(pyenv-version-name)/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 + echo "false" + exit 1 + fi fi shell="${PYENV_SHELL:-${SHELL##*/}}" diff --git a/test/activate.bats b/test/activate.bats index 897f189..8823749 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -13,6 +13,7 @@ setup() { stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate @@ -34,6 +35,7 @@ EOS stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate --verbose @@ -56,6 +58,7 @@ EOS stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate @@ -84,6 +87,7 @@ EOS stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate @@ -105,6 +109,7 @@ EOS stub pyenv-version-name "echo venv" stub pyenv-virtualenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" + stub pyenv-prefix "venv : echo \"${PYENV_ROOT}/versions/venv\"" PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate @@ -132,6 +137,7 @@ EOS stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" + stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27" @@ -153,6 +159,7 @@ EOS stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" + stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate "venv27" @@ -179,6 +186,7 @@ EOS stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" + stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27" @@ -200,6 +208,7 @@ EOS stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" + stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate "venv27" @@ -232,10 +241,12 @@ EOS @test "should fail if the version is not a virtualenv" { 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 "3.3.3" unstub pyenv-virtualenv-prefix + unstub pyenv-prefix assert_failure assert_output < ${BATS_TEST_DIRNAME}/script.sh - chmod +x ${BATS_TEST_DIRNAME}/script.sh - run ${BATS_TEST_DIRNAME}/script.sh + printf '#!/bin/sh\necho "$(pyenv-virtualenv-init -)"' > "${TMP}/script.sh" + chmod +x ${TMP}/script.sh + run ${TMP}/script.sh assert_success assert_output_contains_not ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";' + rm -f "${TMP}/script.sh" } @test "detect parent shell from script (bash)" { unset PYENV_SHELL - printf '#!/bin/bash\necho "$(pyenv-virtualenv-init -)"' > ${BATS_TEST_DIRNAME}/script.sh - chmod +x ${BATS_TEST_DIRNAME}/script.sh - run ${BATS_TEST_DIRNAME}/script.sh + printf '#!/bin/bash\necho "$(pyenv-virtualenv-init -)"' > "${TMP}/script.sh" + chmod +x ${TMP}/script.sh + run ${TMP}/script.sh assert_success assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";' + rm -f "${TMP}/script.sh" } @test "sh-compatible instructions" { From cdbf7c9972cd96f829e032ab7139305aee4b44a7 Mon Sep 17 00:00:00 2001 From: "Yamashita, Yuu" Date: Sun, 19 Jul 2015 04:19:05 +0900 Subject: [PATCH 6/7] activate "root" environment of anaconda/miniconda if environment name is not given --- bin/pyenv-sh-activate | 30 ++++++++++++++---------------- bin/pyenv-sh-deactivate | 21 +++++++-------------- bin/pyenv-virtualenv-prefix | 3 +-- bin/pyenv-virtualenvs | 5 ++--- test/deactivate.bats | 2 ++ test/virtualenvs.bats | 10 ++++++++-- 6 files changed, 34 insertions(+), 37 deletions(-) diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 046d030..08c07c9 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -69,7 +69,12 @@ if pyenv-prefix "${versions}" 1>/dev/null 2>&1; then exit 1 fi else - versions="$(pyenv-version-name)/envs/${versions}" + version="$(pyenv-version-name)" + if [[ "${versions}" == "root" ]]; then + versions="${version%/envs/*}" + else + versions="${version%/envs/*}/envs/${versions}" + fi 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 echo "false" @@ -83,23 +88,16 @@ prefix="$(pyenv-prefix "${versions}")" unset conda_env if [ -f "${prefix}/bin/conda" ]; then - if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then - case "$shell" in - bash ) - conda_env="${prefix##*/envs/}" - ;; - zsh ) - conda_env="${prefix##*/envs/}" - ;; - * ) - echo "false" - exit 1 - ;; - esac - else + if [[ "$shell" != "bash" ]] && [[ "$shell" != "zsh" ]]; then + [ -n "$NOERROR" ] || echo "pyenv-virtualenv: Only bash and zsh are supported by Anaconda/Miniconda" 1>&2 echo "false" exit 1 fi + if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then + conda_env="${prefix##*/envs/}" + else + conda_env="root" + fi fi # Display setup instruction, if pyenv-virtualenv has not been initialized. @@ -131,7 +129,7 @@ if [ -n "${conda_env}" ]; then cat <&2 echo "false" exit 1 fi + if [[ "${prefix}" != "${prefix%/envs/*}" ]]; then + conda_env="${prefix##*/envs/}" + else + conda_env="root" + fi fi if [ -n "${conda_env}" ]; then diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index b1ae326..dad9688 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -50,8 +50,7 @@ for version_string in "${versions[@]}"; do exit 1 fi else - echo "pyenv-virtualenv: version \`${version_string}' is not a virtualenv" 1>&2 - exit 1 + VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}") fi else VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)" diff --git a/bin/pyenv-virtualenvs b/bin/pyenv-virtualenvs index c550b1c..6489565 100755 --- a/bin/pyenv-virtualenvs +++ b/bin/pyenv-virtualenvs @@ -51,10 +51,9 @@ for version in $(pyenv-versions --bare); do virtualenv_prefix="$(pyenv-virtualenv-prefix "${version}" 2>/dev/null || true)" if [ -d "${virtualenv_prefix}" ]; then print_version "${version}" " (created from ${virtualenv_prefix})" - else - # envs of anaconda/miniconda prefix="$(pyenv-prefix "${version}")" - if [ -f "${prefix}/bin/activate" ] && [ -f "${prefix}/bin/conda" ]; then + if [ -f "${prefix}/bin/conda" ]; then + # envs of anaconda/miniconda shopt -s nullglob for conda_env in "${prefix}/envs/"*; do print_version "${version##*/}${conda_env#${prefix}}" " (created from ${prefix})" diff --git a/test/deactivate.bats b/test/deactivate.bats index e3366cc..0329031 100644 --- a/test/deactivate.bats +++ b/test/deactivate.bats @@ -106,6 +106,7 @@ EOS @test "deactivate virtualenv which has been activated manually" { export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" + export PYENV_ACTIVATE= export PYENV_ACTIVATE_SHELL= PYENV_SHELL="bash" run pyenv-sh-deactivate @@ -203,6 +204,7 @@ EOS @test "deactivate virtualenv which has been activated manually (fish)" { export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv" + export PYENV_ACTIVATE= export PYENV_ACTIVATE_SHELL= PYENV_SHELL="fish" run pyenv-sh-deactivate diff --git a/test/virtualenvs.bats b/test/virtualenvs.bats index e27fc66..62cd813 100644 --- a/test/virtualenvs.bats +++ b/test/virtualenvs.bats @@ -17,8 +17,8 @@ setup() { stub pyenv-virtualenv-prefix "3.3.3 : false" stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\"" stub pyenv-virtualenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\"" - stub pyenv-prefix "2.7.6 : echo \"${PYENV_ROOT}/versions/2.7.6\"" - stub pyenv-prefix "3.3.3 : echo \"${PYENV_ROOT}/versions/3.3.3\"" + stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/2.7.6\"" + stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/3.3.3\"" run pyenv-virtualenvs @@ -39,12 +39,15 @@ OUT stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\"" stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\"" stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\"" + stub pyenv-prefix "venv27 : echo \"/usr\"" + stub pyenv-prefix "venv33 : echo \"/usr\"" run pyenv-virtualenvs unstub pyenv-version-name unstub pyenv-versions unstub pyenv-virtualenv-prefix + unstub pyenv-prefix assert_success assert_output < Date: Sun, 19 Jul 2015 15:00:59 +0900 Subject: [PATCH 7/7] Add tests for conda --- bin/pyenv-virtualenv-prefix | 2 +- test/conda-activate.bats | 149 ++++++++++++++++++++++++++++++ test/conda-deactivate.bats | 64 +++++++++++++ test/conda-virtualenv-prefix.bats | 41 ++++++++ test/test_helper.bash | 16 ++++ 5 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 test/conda-activate.bats create mode 100644 test/conda-deactivate.bats create mode 100644 test/conda-virtualenv-prefix.bats diff --git a/bin/pyenv-virtualenv-prefix b/bin/pyenv-virtualenv-prefix index dad9688..858ff72 100755 --- a/bin/pyenv-virtualenv-prefix +++ b/bin/pyenv-virtualenv-prefix @@ -44,7 +44,7 @@ for version_string in "${versions[@]}"; do if [[ "${version_string}" != "${version_string%/envs/*}" ]]; then conda_env="${version_string##*/envs/}" if [ -f "${PREFIX}/envs/${conda_env}/bin/conda" ]; then - VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}/envs/${conda_env}") + VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${PREFIX}") else echo "pyenv-virtualenv: version \`${version_string}' is not an environment of anaconda/miniconda" 1>&2 exit 1 diff --git a/test/conda-activate.bats b/test/conda-activate.bats new file mode 100644 index 0000000..8d43f31 --- /dev/null +++ b/test/conda-activate.bats @@ -0,0 +1,149 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + export HOME="${TMP}" + export PYENV_ROOT="${TMP}/pyenv" +} + +@test "activate conda root from current version" { + export PYENV_VIRTUALENV_INIT=1 + + create_conda "anaconda-2.3.0" + stub pyenv-version-name "echo anaconda-2.3.0" + stub pyenv-virtualenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\"" + stub pyenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\"" + stub pyenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\"" + + PYENV_SHELL="bash" PYENV_VERSION="anaconda-2.3.0" run pyenv-sh-activate + + unstub pyenv-version-name + unstub pyenv-virtualenv-prefix + unstub pyenv-prefix + + assert_success + assert_output <&2; + false; +fi; +EOS +} + +@test "deactivate conda root (fish)" { + export PYENV_ACTIVATE="${PYENV_ROOT}/versions/anaconda-2.3.0" + export PYENV_ACTIVATE_SHELL= + + create_conda "anaconda-2.3.0" + + PYENV_SHELL="fish" run pyenv-sh-deactivate + + assert_failure + assert_output <&2; + false; +fi; +EOS +} diff --git a/test/conda-virtualenv-prefix.bats b/test/conda-virtualenv-prefix.bats new file mode 100644 index 0000000..d7a928b --- /dev/null +++ b/test/conda-virtualenv-prefix.bats @@ -0,0 +1,41 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + export PYENV_ROOT="${TMP}/pyenv" +} + +@test "display conda root" { + create_conda "anaconda-2.3.0" + + stub pyenv-version-name "echo anaconda-2.3.0" + stub pyenv-prefix "anaconda-2.3.0 : echo \"${PYENV_ROOT}/versions/anaconda-2.3.0\"" + + PYENV_VERSION="anaconda-2.3.0" run pyenv-virtualenv-prefix + + unstub pyenv-version-name + unstub pyenv-prefix + + assert_success + assert_output <