├── test ├── tmp │ └── .gitignore ├── virtualenvwrapper │ ├── virtualenvwrapper_lazy.sh │ └── virtualenvwrapper.sh ├── unset.bats ├── command.bats ├── version.bats ├── installer.bats ├── test_helper.bash ├── stubs │ └── stub ├── setup.bats ├── virtualenvwrapper.bats ├── pyvenv.bats └── virtualenvwrapper_lazy.bats ├── .gitignore ├── .travis.yml ├── bin ├── pyenv-virtualenvwrapper_lazy ├── pyenv-sh-virtualenvwrapper ├── pyenv-sh-virtualenvwrapper_lazy └── pyenv-virtualenvwrapper ├── install.sh ├── shims └── deactivate ├── LICENSE └── README.md /test/tmp/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swo 2 | *.swp 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | install: git clone https://github.com/sstephenson/bats.git 2 | script: bats/bin/bats --tap test 3 | language: c 4 | notifications: 5 | email: 6 | on_success: never 7 | -------------------------------------------------------------------------------- /bin/pyenv-virtualenvwrapper_lazy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Setup virtualenvwrapper_lazy into current shell. 3 | # 4 | # Usage: pyenv virtualenvwrapper_lazy 5 | 6 | set -e 7 | [ -n "$PYENV_DEBUG" ] && set -x 8 | 9 | pyenv-virtualenvwrapper "$@" 10 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Usage: PREFIX=/usr/local ./install.sh 3 | # 4 | # Installs pyenv-virtualenvwrapper under $PREFIX. 5 | 6 | set -e 7 | 8 | cd "$(dirname "$0")" 9 | 10 | if [ -z "${PREFIX}" ]; then 11 | PREFIX="/usr/local" 12 | fi 13 | 14 | BIN_PATH="${PREFIX}/bin" 15 | 16 | mkdir -p "${BIN_PATH}" 17 | 18 | install -p bin/* "$BIN_PATH" 19 | -------------------------------------------------------------------------------- /test/virtualenvwrapper/virtualenvwrapper_lazy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo "virtualenvwrapper_lazy.sh is loaded" 4 | 5 | VIRTUALENVWRAPPER_SCRIPT="${VIRTUALENVWRAPPER_SCRIPT:-undefined}" 6 | 7 | virtualenvwrapper_load() { 8 | echo "virtualenvwrapper_load is invoked" 9 | } 10 | 11 | mkvirtualenv() { 12 | virtualenvwrapper_load 13 | mkvirtualenv "$@" 14 | } 15 | 16 | lsvirtualenv() { 17 | virtualenvwrapper_load 18 | lsvirtualenv "$@" 19 | } 20 | -------------------------------------------------------------------------------- /test/unset.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | setup() { 6 | export PYENV_ROOT="${TMP}/pyenv" 7 | } 8 | 9 | @test "reset virtualenvwrapper" { 10 | run pyenv-sh-virtualenvwrapper --unset 11 | assert_success 12 | assert_output <&2 25 | exit 1 26 | } 27 | declare -f virtualenvwrapper_load 28 | echo "virtualenvwrapper_load" 29 | -------------------------------------------------------------------------------- /test/command.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | setup() { 6 | export PYENV_ROOT="${TMP}/pyenv" 7 | } 8 | 9 | @test "library functions for virtualenvwrapper" { 10 | run pyenv-virtualenvwrapper --lib 11 | assert_success 12 | assert_output_contains "lib ()" 13 | } 14 | 15 | @test "library functions for virtualenvwrapper_lazy" { 16 | run pyenv-virtualenvwrapper_lazy --lib 17 | assert_success 18 | assert_output_contains "lib ()" 19 | } 20 | 21 | @test "virtualenvwrapper cannot be invoked as command" { 22 | run pyenv-virtualenvwrapper 23 | assert_failure 24 | } 25 | 26 | @test "virtualenvwrapper_lazy cannot be invoked as command" { 27 | run pyenv-virtualenvwrapper_lazy 28 | assert_failure 29 | } 30 | -------------------------------------------------------------------------------- /bin/pyenv-sh-virtualenvwrapper_lazy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Summary: Setup virtualenvwrapper_lazy into current shell. 3 | # 4 | # Usage: pyenv virtualenvwrapper_lazy 5 | 6 | set -e 7 | [ -n "$PYENV_DEBUG" ] && set -x 8 | 9 | # Load shared library function 10 | eval "$(pyenv-virtualenvwrapper_lazy --lib)" 11 | 12 | case "$1" in 13 | "--unset" ) 14 | echo "unset PYENV_VIRTUALENVWRAPPER_PYENV_VERSION" 15 | exit 16 | ;; 17 | "--version" ) 18 | echo "echo \"$(pyenv-virtualenvwrapper_lazy --version)\"" 19 | exit 20 | ;; 21 | esac 22 | 23 | pyenv_virtualenvwrapper_init || { 24 | echo "pyenv-virtualenvwrapper: failed to initialize virtualenvwrapper_lazy." 1>&2 25 | exit 1 26 | } 27 | echo "source \"${VIRTUALENVWRAPPER_LAZY_SCRIPT}\"" 28 | declare -f virtualenvwrapper_load 29 | -------------------------------------------------------------------------------- /shims/deactivate: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | if [[ "$0" != "${BASH_SOURCE}" ]]; then 3 | unset VIRTUAL_ENV 4 | if [ -n "${CONDA_DEFAULT_ENV}" ]; then 5 | unset CONDA_DEFAULT_ENV 6 | fi 7 | if [ -n "${_OLD_VIRTUAL_PATH}" ]; then 8 | export PATH="${_OLD_VIRTUAL_PATH}" 9 | unset _OLD_VIRTUAL_PATH 10 | fi 11 | if [ -n "${_OLD_VIRTUAL_PYTHONHOME}" ]; then 12 | export PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME}" 13 | unset _OLD_VIRTUAL_PYTHONHOME 14 | fi 15 | if [ -n "${_OLD_VIRTUAL_PS1}" ]; then 16 | export PS1="${_OLD_VIRTUAL_PS1}" 17 | unset _OLD_VIRTUAL_PS1 18 | fi 19 | if declare -f deactivate 1>/dev/null 2>&1; then 20 | unset -f deactivate 21 | fi 22 | else 23 | # not invoked as `source deactivate` 24 | # what should i do here...? 25 | : 26 | fi 27 | -------------------------------------------------------------------------------- /test/version.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | setup() { 6 | export PYENV_ROOT="${TMP}/pyenv" 7 | export PYENV_VIRTUALENVWRAPPER_VERSION="20140609" 8 | } 9 | 10 | @test "display virtualenvwrapper version" { 11 | stub pyenv "pip show virtualenvwrapper: echo \"---\";echo \"Name: virtualenvwrapper\";echo \"Version: 4.2\"" 12 | run eval "$(pyenv-sh-virtualenvwrapper --version)" 13 | unstub pyenv 14 | assert_success 15 | assert_output_contains "pyenv-virtualenvwrapper ${PYENV_VIRTUALENVWRAPPER_VERSION} (virtualenvwrapper 4.2)" 16 | } 17 | 18 | @test "display virtualenvwrapper_lazy version" { 19 | stub pyenv "pip show virtualenvwrapper: echo \"---\";echo \"Name: virtualenvwrapper\";echo \"Version: 4.2\"" 20 | run eval "$(pyenv-sh-virtualenvwrapper_lazy --version)" 21 | unstub pyenv 22 | assert_success 23 | assert_output_contains "pyenv-virtualenvwrapper ${PYENV_VIRTUALENVWRAPPER_VERSION} (virtualenvwrapper 4.2)" 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Yamashita, Yuu 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/virtualenvwrapper/virtualenvwrapper.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echo "virtualenvwrapper.sh is loaded" 4 | 5 | VIRTUALENVWRAPPER_PYTHON="${VIRTUALENVWRAPPER_PYTHON:-undefined}" 6 | VIRTUALENVWRAPPER_VIRTUALENV="${VIRTUALENVWRAPPER_VIRTUALENV:-undefined}" 7 | VIRTUALENVWRAPPER_VIRTUALENV_CLONE="${VIRTUALENVWRAPPER_VIRTUALENV_CLONE:-undefined}" 8 | 9 | virtualenvwrapper_verify_resource() { 10 | echo "virtualenvwrapper_verify_resource is invoked" 11 | } 12 | 13 | virtualenvwrapper_verify_workon_home() { 14 | echo "virtualenvwrapper_verify_workon_home is invoked" 15 | } 16 | 17 | virtualenvwrapper_verify_virtualenv() { 18 | virtualenvwrapper_verify_resource "${VIRTUALENVWRAPPER_VIRTUALENV}" 19 | } 20 | 21 | mkvirtualenv() { 22 | virtualenvwrapper_verify_workon_home || return 1 23 | virtualenvwrapper_verify_virtualenv || return 1 24 | echo "PYTHON=${VIRTUALENVWRAPPER_PYTHON} VIRTUALENV=${VIRTUALENVWRAPPER_VIRTUALENV} mkvirtualenv $@" 25 | } 26 | 27 | lsvirtualenv() { 28 | virtualenvwrapper_verify_workon_home || return 1 29 | echo "PYTHON=${VIRTUALENVWRAPPER_PYTHON} VIRTUALENV=${VIRTUALENVWRAPPER_VIRTUALENV} lsvirtualenv" 30 | } 31 | -------------------------------------------------------------------------------- /test/installer.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "installs pyenv-virtualenvwrapper into PREFIX" { 6 | cd "$TMP" 7 | PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh" 8 | assert_success "" 9 | 10 | cd usr 11 | 12 | assert [ -x bin/pyenv-sh-virtualenvwrapper ] 13 | assert [ -x bin/pyenv-sh-virtualenvwrapper_lazy ] 14 | assert [ -x bin/pyenv-virtualenvwrapper ] 15 | assert [ -x bin/pyenv-virtualenvwrapper_lazy ] 16 | } 17 | 18 | @test "overwrites old installation" { 19 | cd "$TMP" 20 | mkdir -p bin 21 | touch bin/pyenv-virtualenvwrapper 22 | 23 | PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh" 24 | assert_success "" 25 | 26 | assert [ -x bin/pyenv-virtualenvwrapper ] 27 | run grep "virtualenvwrapper" bin/pyenv-virtualenvwrapper 28 | assert_success 29 | } 30 | 31 | @test "unrelated files are untouched" { 32 | cd "$TMP" 33 | mkdir -p bin share/bananas 34 | chmod g-w bin 35 | touch bin/bananas 36 | 37 | PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh" 38 | assert_success "" 39 | 40 | assert [ -e bin/bananas ] 41 | 42 | run ls -ld bin 43 | assert_equal "r-x" "${output:4:3}" 44 | } 45 | -------------------------------------------------------------------------------- /test/test_helper.bash: -------------------------------------------------------------------------------- 1 | export TMP="$BATS_TEST_DIRNAME/tmp" 2 | 3 | PATH=/usr/bin:/usr/sbin:/bin/:/sbin 4 | PATH="$BATS_TEST_DIRNAME/../bin:$PATH" 5 | PATH="$TMP/bin:$PATH" 6 | export PATH 7 | 8 | teardown() { 9 | rm -fr "$TMP"/* 10 | } 11 | 12 | stub() { 13 | local program="$1" 14 | local prefix="$(echo "$program" | tr a-z- A-Z_)" 15 | shift 16 | 17 | export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan" 18 | export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run" 19 | export "${prefix}_STUB_END"= 20 | 21 | mkdir -p "${TMP}/bin" 22 | ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}" 23 | 24 | touch "${TMP}/${program}-stub-plan" 25 | for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done 26 | } 27 | 28 | unstub() { 29 | local program="$1" 30 | local prefix="$(echo "$program" | tr a-z- A-Z_)" 31 | local path="${TMP}/bin/${program}" 32 | 33 | export "${prefix}_STUB_END"=1 34 | 35 | local STATUS=0 36 | "$path" || STATUS="$?" 37 | 38 | rm -f "$path" 39 | rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run" 40 | return "$STATUS" 41 | } 42 | 43 | assert() { 44 | if ! "$@"; then 45 | flunk "failed: $@" 46 | fi 47 | } 48 | 49 | flunk() { 50 | { if [ "$#" -eq 0 ]; then cat - 51 | else echo "$@" 52 | fi 53 | } | sed "s:${TMP}:\${TMP}:g" >&2 54 | return 1 55 | } 56 | 57 | assert_success() { 58 | if [ "$status" -ne 0 ]; then 59 | { echo "command failed with exit status $status" 60 | echo "output: $output" 61 | } | flunk 62 | elif [ "$#" -gt 0 ]; then 63 | assert_output "$1" 64 | fi 65 | } 66 | 67 | assert_failure() { 68 | if [ "$status" -eq 0 ]; then 69 | flunk "expected failed exit status" 70 | elif [ "$#" -gt 0 ]; then 71 | assert_output "$1" 72 | fi 73 | } 74 | 75 | assert_equal() { 76 | if [ "$1" != "$2" ]; then 77 | { echo "expected: $1" 78 | echo "actual: $2" 79 | } | flunk 80 | fi 81 | } 82 | 83 | assert_output() { 84 | local expected 85 | if [ $# -eq 0 ]; then expected="$(cat -)" 86 | else expected="$1" 87 | fi 88 | assert_equal "$expected" "$output" 89 | } 90 | 91 | assert_output_contains() { 92 | local expected="$1" 93 | echo "$output" | grep -F "$expected" >/dev/null || { 94 | { echo "expected output to contain $expected" 95 | echo "actual: $output" 96 | } | flunk 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /test/stubs/stub: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | status=0 5 | program="${0##*/}" 6 | PROGRAM="$(echo "$program" | tr a-z- A-Z_)" 7 | [ -n "$TMPDIR" ] || TMPDIR="/tmp" 8 | 9 | _STUB_PLAN="${PROGRAM}_STUB_PLAN" 10 | _STUB_RUN="${PROGRAM}_STUB_RUN" 11 | _STUB_INDEX="${PROGRAM}_STUB_INDEX" 12 | _STUB_RESULT="${PROGRAM}_STUB_RESULT" 13 | _STUB_END="${PROGRAM}_STUB_END" 14 | _STUB_DEBUG="${PROGRAM}_STUB_DEBUG" 15 | 16 | if [ -n "${!_STUB_DEBUG}" ]; then 17 | echo "$program" "$@" >&${!_STUB_DEBUG} 18 | fi 19 | 20 | [ -e "${!_STUB_PLAN}" ] || exit 1 21 | [ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run" 22 | 23 | 24 | # Initialize or load the stub run information. 25 | eval "${_STUB_INDEX}"=1 26 | eval "${_STUB_RESULT}"=0 27 | [ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}" 28 | 29 | 30 | # Loop over each line in the plan. 31 | index=0 32 | while IFS= read -r line; do 33 | index=$(($index + 1)) 34 | 35 | if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then 36 | # We found the plan line we're interested in. 37 | # Start off by assuming success. 38 | result=0 39 | 40 | # Split the line into an array of arguments to 41 | # match and a command to run to produce output. 42 | command=" $line" 43 | if [ "$command" != "${command/ : }" ]; then 44 | patterns="${command%% : *}" 45 | command="${command#* : }" 46 | fi 47 | 48 | # Naively split patterns by whitespace for now. 49 | # In the future, use a sed script to split while 50 | # respecting quoting. 51 | set -f 52 | patterns=($patterns) 53 | set +f 54 | arguments=("$@") 55 | 56 | # Match the expected argument patterns to actual 57 | # arguments. 58 | for (( i=0; i<${#patterns[@]}; i++ )); do 59 | pattern="${patterns[$i]}" 60 | argument="${arguments[$i]}" 61 | 62 | case "$argument" in 63 | $pattern ) ;; 64 | * ) result=1 ;; 65 | esac 66 | done 67 | 68 | # If the arguments matched, evaluate the command 69 | # in a subshell. Otherwise, log the failure. 70 | if [ $result -eq 0 ] ; then 71 | set +e 72 | ( eval "$command" ) 73 | status="$?" 74 | set -e 75 | else 76 | eval "${_STUB_RESULT}"=1 77 | fi 78 | fi 79 | done < "${!_STUB_PLAN}" 80 | 81 | 82 | if [ -n "${!_STUB_END}" ]; then 83 | # Clean up the run file. 84 | rm -f "${!_STUB_RUN}" 85 | 86 | # If the number of lines in the plan is larger than 87 | # the requested index, we failed. 88 | if [ $index -ge "${!_STUB_INDEX}" ]; then 89 | eval "${_STUB_RESULT}"=1 90 | fi 91 | 92 | # Return the result. 93 | exit "${!_STUB_RESULT}" 94 | 95 | else 96 | # If the requested index is larger than the number 97 | # of lines in the plan file, we failed. 98 | if [ "${!_STUB_INDEX}" -gt $index ]; then 99 | eval "${_STUB_RESULT}"=1 100 | fi 101 | 102 | # Write out the run information. 103 | { echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))" 104 | echo "${_STUB_RESULT}=${!_STUB_RESULT}" 105 | } > "${!_STUB_RUN}" 106 | 107 | exit "$status" 108 | 109 | fi 110 | -------------------------------------------------------------------------------- /test/setup.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | setup() { 6 | export PYENV_ROOT="${TMP}/pyenv" 7 | export PYENV_VERSION="3.3.3" 8 | create_executable "python" <<-EOS 9 | #!/usr/bin/env bash 10 | echo python is invoked 11 | EOS 12 | create_executable "virtualenv" <<-EOS 13 | #!/usr/bin/env bash 14 | echo virtualenv is invoked 15 | EOS 16 | create_executable "virtualenv-clone" <<-EOS 17 | #!/usr/bin/env bash 18 | echo virtualenv-clone is invoked 19 | EOS 20 | } 21 | 22 | create_executable() { 23 | name="${1?}" 24 | shift 1 25 | bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin" 26 | mkdir -p "$bin" 27 | { if [ $# -eq 0 ]; then cat - 28 | else echo "$@" 29 | fi 30 | } | sed -Ee '1s/^ +//' > "${bin}/$name" 31 | chmod +x "${bin}/$name" 32 | } 33 | 34 | @test "install virtualenvwrapper if not available" { 35 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 36 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 37 | stub pyenv "which virtualenvwrapper.sh : false" 38 | stub pyenv "which virtualenvwrapper_lazy.sh : false" 39 | stub pyenv "exec pip install virtualenvwrapper : d=\"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin\";mkdir -p \"\$d\";touch \"\$d/virtualenvwrapper.sh\";touch \"\$d/virtualenvwrapper_lazy.sh\";echo \"\$@\"" 40 | stub pyenv "which virtualenvwrapper.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper.sh\"" 41 | stub pyenv "which virtualenvwrapper_lazy.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper_lazy.sh\"" 42 | 43 | VIRTUALENVWRAPPER_VERSION="" run pyenv-sh-virtualenvwrapper 44 | 45 | unstub pyenv 46 | assert_success 47 | assert_output_contains "exec pip install virtualenvwrapper" 48 | } 49 | 50 | @test "install virtualenvwrapper version if not available" { 51 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 52 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 53 | stub pyenv "which virtualenvwrapper.sh : false" 54 | stub pyenv "which virtualenvwrapper_lazy.sh : false" 55 | stub pyenv "exec pip install virtualenvwrapper==4.2 : d=\"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin\";mkdir -p \"\$d\";touch \"\$d/virtualenvwrapper.sh\";touch \"\$d/virtualenvwrapper_lazy.sh\";echo \"\$@\"" 56 | stub pyenv "which virtualenvwrapper.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper.sh\"" 57 | stub pyenv "which virtualenvwrapper_lazy.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper_lazy.sh\"" 58 | 59 | VIRTUALENVWRAPPER_VERSION="4.2" run pyenv-sh-virtualenvwrapper 60 | 61 | unstub pyenv 62 | assert_success 63 | assert_output_contains "exec pip install virtualenvwrapper==4.2" 64 | } 65 | 66 | @test "install virtualenvwrapper with unsetting troublesome pip options" { 67 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 68 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 69 | stub pyenv "which virtualenvwrapper.sh : false" 70 | stub pyenv "which virtualenvwrapper_lazy.sh : false" 71 | stub pyenv "exec pip install virtualenvwrapper : d=\"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin\";mkdir -p \"\$d\";touch \"\$d/virtualenvwrapper.sh\";touch \"\$d/virtualenvwrapper_lazy.sh\";echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} \"\$@\"" 72 | stub pyenv "which virtualenvwrapper.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper.sh\"" 73 | stub pyenv "which virtualenvwrapper_lazy.sh : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/virtualenvwrapper_lazy.sh\"" 74 | 75 | PIP_REQUIRE_VENV="true" VIRTUALENVWRAPPER_VERSION="" run pyenv-sh-virtualenvwrapper 76 | 77 | unstub pyenv 78 | assert_success 79 | assert_output_contains "PIP_REQUIRE_VENV= exec pip install virtualenvwrapper" 80 | } 81 | -------------------------------------------------------------------------------- /test/virtualenvwrapper.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | setup() { 6 | export PYENV_ROOT="${TMP}/pyenv" 7 | for version in "2.7.6" "3.3.3"; do 8 | PYENV_VERSION="${version}" create_executable "python" < "${bin}/$name" 34 | chmod +x "${bin}/$name" 35 | } 36 | 37 | gen_script() { 38 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 39 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 40 | 41 | run pyenv-sh-virtualenvwrapper 42 | 43 | unstub pyenv 44 | assert_success 45 | 46 | echo "${output%;}" 47 | } 48 | 49 | @test "initialize virtualenvwrapper" { 50 | export PYENV_VERSION="3.3.3" 51 | 52 | script="$(gen_script)" 53 | 54 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 55 | 56 | run eval "${script}" 57 | 58 | unstub pyenv 59 | assert_success 60 | assert_output < "${bin}/$name" 32 | chmod +x "${bin}/$name" 33 | } 34 | 35 | gen_script() { 36 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 37 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 38 | 39 | run pyenv-sh-virtualenvwrapper 40 | 41 | unstub pyenv 42 | assert_success 43 | 44 | echo "${output%;}" 45 | } 46 | 47 | @test "prefer virtualenv by default" { 48 | export PYENV_VERSION="3.3.3" 49 | unset PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV 50 | rm -f "${PYENV_ROOT}/versions/${PYENV_VERSION}/bin/pyvenv" 51 | 52 | script="$(gen_script)" 53 | 54 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 55 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 56 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 57 | 58 | run eval "${script}; mkvirtualenv venv33" 59 | 60 | unstub pyenv 61 | assert_success 62 | assert_output < "${bin}/$name" 34 | chmod +x "${bin}/$name" 35 | } 36 | 37 | gen_script() { 38 | stub pyenv "version-name : echo \"${PYENV_VERSION}\"" 39 | stub pyenv "prefix ${PYENV_VERSION} : echo \"${PYENV_ROOT}/versions/${PYENV_VERSION}\"" 40 | 41 | run pyenv-sh-virtualenvwrapper_lazy 42 | 43 | unstub pyenv 44 | assert_success 45 | 46 | echo "${output%;}" 47 | } 48 | 49 | @test "initialize virtualenvwrapper_lazy" { 50 | export PYENV_VERSION="2.7.6" 51 | 52 | script="$(gen_script)" 53 | 54 | run eval "${script}" 55 | 56 | assert_success 57 | assert_output <&2 16 | return 1 17 | fi 18 | if [[ "${PYENV_VIRTUALENVWRAPPER_PYENV_VERSION}" != "${version}" ]]; then 19 | unset PYENV_VIRTUALENVWRAPPER_PYENV_VERSION 20 | 21 | local prefix="$(pyenv prefix "${version%%:*}" 2>/dev/null || true)" 22 | if [ ! -d "${prefix}" ]; then 23 | echo "pyenv-virtualenvwrapper: no such directory: ${prefix}" 1>&2 24 | return 1 25 | fi 26 | 27 | export VIRTUALENVWRAPPER_SCRIPT="${prefix}/bin/virtualenvwrapper.sh" 28 | export VIRTUALENVWRAPPER_LAZY_SCRIPT="${prefix}/bin/virtualenvwrapper_lazy.sh" 29 | [ -f "${VIRTUALENVWRAPPER_SCRIPT}" -a -f "${VIRTUALENVWRAPPER_LAZY_SCRIPT}" ] || { 30 | export VIRTUALENVWRAPPER_SCRIPT="$(pyenv which virtualenvwrapper.sh 2>/dev/null || true)" 31 | export VIRTUALENVWRAPPER_LAZY_SCRIPT="$(pyenv which virtualenvwrapper_lazy.sh 2>/dev/null || true)" 32 | [ -f "${VIRTUALENVWRAPPER_SCRIPT}" -a -f "${VIRTUALENVWRAPPER_LAZY_SCRIPT}" ] || { 33 | ( local VIRTUALENVWRAPPER_VERSION="==${VIRTUALENVWRAPPER_VERSION}" 34 | unset PIP_REQUIRE_VENV 35 | unset PIP_REQUIRE_VIRTUALENV 36 | pyenv exec pip install $QUIET $VERBOSE "virtualenvwrapper${VIRTUALENVWRAPPER_VERSION%==}" 37 | ) 1>&2 38 | export VIRTUALENVWRAPPER_SCRIPT="$(pyenv which virtualenvwrapper.sh 2>/dev/null || true)" 39 | export VIRTUALENVWRAPPER_LAZY_SCRIPT="$(pyenv which virtualenvwrapper_lazy.sh 2>/dev/null || true)" 40 | [ -f "${VIRTUALENVWRAPPER_SCRIPT}" -a -f "${VIRTUALENVWRAPPER_LAZY_SCRIPT}" ] || { 41 | echo "pyenv-virtualenvwrapper: ${version}: either virtualenvwrapper.sh or virtualenvwrapper_lazy.sh is not available." 1>&2 42 | return 1 43 | } 44 | } 45 | } 46 | 47 | export VIRTUALENVWRAPPER_VIRTUALENV_CLONE="${prefix}/bin/virtualenv-clone" 48 | [ -x "${VIRTUALENVWRAPPER_VIRTUALENV_CLONE}" ] || { 49 | export VIRTUALENVWRAPPER_VIRTUALENV_CLONE="$(pyenv which virtualenv-clone 2>/dev/null || true)" 50 | [ -x "${VIRTUALENVWRAPPER_VIRTUALENV_CLONE}" ] || { 51 | echo "pyenv-virtualenvwrapper: ${version}: virtualenv-clone is not available." 1>&2 52 | return 1 53 | } 54 | } 55 | 56 | if [ -n "${PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV}" -a -x "${prefix}/bin/pyvenv" ]; then 57 | export VIRTUALENVWRAPPER_VIRTUALENV="${prefix}/bin/pyvenv" 58 | else 59 | export VIRTUALENVWRAPPER_VIRTUALENV="${prefix}/bin/virtualenv" 60 | fi 61 | [ -x "${VIRTUALENVWRAPPER_VIRTUALENV}" ] || { 62 | export VIRTUALENVWRAPPER_VIRTUALENV="$(pyenv which virtualenv 2>/dev/null || true)" 63 | [ -x "${VIRTUALENVWRAPPER_VIRTUALENV}" ] || { 64 | echo "pyenv-virtualenvwrapper: ${version}: virtualenv is not available." 1>&2 65 | return 1 66 | } 67 | } 68 | 69 | # if the python version used by virtualenvwrapper is already set, respect 70 | # it 71 | [ -x "${VIRTUALENVWRAPPER_PYTHON}" ] || export VIRTUALENVWRAPPER_PYTHON="${prefix}/bin/python" 72 | [ -x "${VIRTUALENVWRAPPER_PYTHON}" ] || { 73 | export VIRTUALENVWRAPPER_PYTHON="$(pyenv which python 2>/dev/null || true)" 74 | [ -x "${VIRTUALENVWRAPPER_PYTHON}" ] || { 75 | echo "pyenv-virtualenvwrapper: ${version}: python is not available." 1>&2 76 | return 1 77 | } 78 | } 79 | fi 80 | export PYENV_VIRTUALENVWRAPPER_PYENV_VERSION="${version}" 81 | } 82 | 83 | pyenv_virtualenvwrapper_verify_resource() { 84 | if [ "$1" = "${1##*/}" ]; then 85 | if pyenv which "$1" >/dev/null 2>&1; then 86 | return 0 87 | fi 88 | else 89 | if [ -e "$1" ]; then 90 | return 0 91 | fi 92 | fi 93 | echo "pyenv-virtualenvwrapper: could not find $1 in your path" 1>&2 94 | return 1 95 | } 96 | 97 | virtualenvwrapper_load() { 98 | pyenv_virtualenvwrapper_load || return 1 99 | source "$VIRTUALENVWRAPPER_SCRIPT" 100 | 101 | # FIXME: is there any better way to override existing shell functions in both bash and zsh? 102 | # preserve original virtualenvwrapper_verify_workon_home 103 | local vvwh="$(declare -f "virtualenvwrapper_verify_workon_home" 2>/dev/null || printf "f() {\ntrue;}\n")" 104 | # removing lines starting with '{' as workaround for bash 105 | eval "$(echo "pyenv_virtualenvwrapper_verify_workon_home() {"; echo "$vvwh" | tail -n +2 | sed '/^{ *$/d')" 106 | virtualenvwrapper_verify_workon_home() { 107 | pyenv_virtualenvwrapper_load || return 1 108 | pyenv_virtualenvwrapper_verify_workon_home "$@" 109 | } 110 | 111 | # override virtualenvwrapper_verify_resource 112 | virtualenvwrapper_verify_resource() { 113 | pyenv_virtualenvwrapper_load || return 1 114 | pyenv_virtualenvwrapper_verify_resource "$@" 115 | } 116 | } 117 | 118 | pyenv_virtualenvwrapper_init() { 119 | pyenv_virtualenvwrapper_load || { 120 | echo "unset PYENV_VIRTUALENVWRAPPER_PYENV_VERSION" 121 | return 1 122 | } 123 | PYENV_VIRTUALENVWRAPPER_INSTALL_PREFIX="${BASH_SOURCE%/bin/*}" 124 | echo "export PATH=\"${PYENV_VIRTUALENVWRAPPER_INSTALL_PREFIX}/shims:${PATH}\"" 125 | echo "export PYENV_VIRTUALENVWRAPPER_PYENV_VERSION=\"${PYENV_VIRTUALENVWRAPPER_PYENV_VERSION}\"" 126 | 127 | echo "export VIRTUALENVWRAPPER_PYTHON=\"${VIRTUALENVWRAPPER_PYTHON}\"" 128 | echo "export VIRTUALENVWRAPPER_VIRTUALENV=\"${VIRTUALENVWRAPPER_VIRTUALENV}\"" 129 | echo "export VIRTUALENVWRAPPER_VIRTUALENV_CLONE=\"${VIRTUALENVWRAPPER_VIRTUALENV_CLONE}\"" 130 | echo "export VIRTUALENVWRAPPER_SCRIPT=\"${VIRTUALENVWRAPPER_SCRIPT}\"" 131 | echo "export VIRTUALENVWRAPPER_LAZY_SCRIPT=\"${VIRTUALENVWRAPPER_LAZY_SCRIPT}\"" 132 | 133 | declare -f pyenv_virtualenvwrapper_load 134 | declare -f pyenv_virtualenvwrapper_verify_resource 135 | } 136 | 137 | if [ "$1" == "--$FUNCNAME" ]; then 138 | declare -f "$FUNCNAME" 139 | echo "$FUNCNAME \"\$1\";" 140 | exit 141 | fi 142 | } 143 | lib "$1" 144 | 145 | virtualenvwrapper_version() { 146 | local info="$(pyenv exec pip show virtualenvwrapper 2>/dev/null || true)" 147 | local version="$(echo "${info}" | grep 'Version:' | head -n 1 || true)" 148 | version="${version##Version: }" 149 | echo "${version:-unknown}" 150 | } 151 | 152 | if [ "$1" == "--version" ]; then 153 | echo "pyenv-virtualenvwrapper ${PYENV_VIRTUALENVWRAPPER_VERSION} (virtualenvwrapper $(virtualenvwrapper_version))" 154 | exit 155 | fi 156 | 157 | { printf "\x1B[31;1m" 158 | echo 159 | echo "Failed to initialize virtualenvwrapper." 160 | echo 161 | echo "Perhaps pyenv-virtualenvwrapper has not been loaded into your shell properly." 162 | echo "Please restart current shell and try again." 163 | echo 164 | printf "\x1B[0m" 165 | } 1>&2 166 | exit 1 167 | --------------------------------------------------------------------------------