Skip to content

Commit

Permalink
Performe as same as before v20140614 if pyenv virtualenv-init is no…
Browse files Browse the repository at this point in the history
…t configured (#26)
  • Loading branch information
Yamashita Yuu committed Jun 15, 2014
1 parent 4a6ef11 commit d9e3adc
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 4 deletions.
6 changes: 6 additions & 0 deletions bin/pyenv-sh-activate
Expand Up @@ -33,6 +33,12 @@ if [ -z "$versions" ]; then
IFS="$OLDIFS"
fi

if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26
no_shell=
fi

if [ "${#versions[@]}" -gt 1 ]; then
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
exit 1
Expand Down
10 changes: 8 additions & 2 deletions bin/pyenv-sh-deactivate
Expand Up @@ -11,6 +11,12 @@ set -e

shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in
fish ) echo "functions -q deactivate; and deactivate";;
* ) echo "declare -f deactivate 1>/dev/null 2>&1 && deactivate";;
fish ) echo "functions -q deactivate; and deactivate;";;
* ) echo "declare -f deactivate 1>/dev/null 2>&1 && deactivate;";;
esac

if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
# Backward compatibility issue
# https://github.com/yyuu/pyenv-virtualenv/issues/26
echo "pyenv shell --unset"
fi
3 changes: 3 additions & 0 deletions bin/pyenv-virtualenv-init
Expand Up @@ -65,6 +65,7 @@ fi
case "$shell" in
bash )
cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true;
Expand All @@ -80,6 +81,7 @@ EOS
;;
fish )
cat <<EOS
setenv PYENV_VIRTUALENV_INIT=1;
function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ (pyenv version-name) = "system" ]
eval (pyenv sh-deactivate); or true
Expand All @@ -92,6 +94,7 @@ EOS
;;
zsh )
cat <<EOS
export PYENV_VIRTUALENV_INIT=1
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true
Expand Down
64 changes: 64 additions & 0 deletions test/activate.bats
Expand Up @@ -8,6 +8,8 @@ setup() {
}

@test "activate virtualenv from current version" {
export PYENV_VIRTUALENV_INIT=1

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\""
Expand All @@ -24,7 +26,29 @@ source "${PYENV_ROOT}/versions/venv/bin/activate"
EOS
}

@test "activate virtualenv from current version (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=

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\""

PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate

unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix

assert_success
assert_output <<EOS
pyenv shell "venv";
source "${PYENV_ROOT}/versions/venv/bin/activate"
EOS
}

@test "activate virtualenv from current version (fish)" {
export PYENV_VIRTUALENV_INIT=1

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\""
Expand All @@ -41,7 +65,47 @@ EOS
EOS
}

@test "activate virtualenv from current version (fish) (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=

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\""

PYENV_SHELL="fish" PYENV_VERSION="venv" run pyenv-sh-activate

unstub pyenv-version-name
unstub pyenv-virtualenv-prefix
unstub pyenv-prefix

assert_success
assert_output <<EOS
pyenv shell "venv";
. "${PYENV_ROOT}/versions/venv/bin/activate.fish"
EOS
}

@test "activate virtualenv from command-line argument" {
export PYENV_VIRTUALENV_INIT=1

stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""

run pyenv-sh-activate "venv27"

unstub pyenv-virtualenv-prefix
unstub pyenv-prefix

assert_success
assert_output <<EOS
pyenv shell "venv27";
source "${PYENV_ROOT}/versions/venv27/bin/activate"
EOS
}

@test "activate virtualenv from command-line argument (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=

stub pyenv-virtualenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""

Expand Down
32 changes: 30 additions & 2 deletions test/deactivate.bats
Expand Up @@ -7,20 +7,48 @@ setup() {
}

@test "deactivate virtualenv" {
export PYENV_VIRTUALENV_INIT=1

PYENV_SHELL="bash" run pyenv-sh-deactivate

assert_success
assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate;
EOS
}

@test "deactivate virtualenv (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=

PYENV_SHELL="bash" run pyenv-sh-deactivate

assert_success
assert_output <<EOS
declare -f deactivate 1>/dev/null 2>&1 && deactivate
declare -f deactivate 1>/dev/null 2>&1 && deactivate;
pyenv shell --unset
EOS
}

@test "deactivate virtualenv (fish)" {
export PYENV_VIRTUALENV_INIT=1

PYENV_SHELL="fish" run pyenv-sh-deactivate

assert_success
assert_output <<EOS
functions -q deactivate; and deactivate;
EOS
}

@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init)" {
export PYENV_VIRTUALENV_INIT=

PYENV_SHELL="fish" run pyenv-sh-deactivate

assert_success
assert_output <<EOS
functions -q deactivate; and deactivate
functions -q deactivate; and deactivate;
pyenv shell --unset
EOS
}

Expand Down

0 comments on commit d9e3adc

Please sign in to comment.