From 2cab5336522f7a505e81c7120a6ed40257c5f4aa Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Sun, 20 Apr 2014 15:24:40 +0900 Subject: [PATCH] Use virtualenv instead of pyvenv if `-p` has given (yyuu/pyenv#158) --- bin/pyenv-virtualenv | 18 +++++++++++++----- test/pyvenv.bats | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/bin/pyenv-virtualenv b/bin/pyenv-virtualenv index 9f681c6..dac0ac8 100755 --- a/bin/pyenv-virtualenv +++ b/bin/pyenv-virtualenv @@ -130,8 +130,8 @@ detect_venv() { if pyenv-which "pyvenv" 1>/dev/null 2>&1; then HAS_PYVENV=1 fi - # Use pyvenv only if virtualenv is not installed and there is pyvenv - if [ -n "${HAS_PYVENV}" ] && [ -z "${HAS_VIRTUALENV}" ]; then + # Use pyvenv only if there is pyvenv, virtualenv is not installed, and `-p` not given + if [ -n "${HAS_PYVENV}" ] && [ -z "${HAS_VIRTUALENV}" ] && [ -z "${VIRTUALENV_PYTHON}" ]; then USE_PYVENV=1 fi } @@ -253,6 +253,7 @@ fi VIRTUALENV_OPTIONS=() unset FORCE +unset VIRTUALENV_PYTHON unset QUIET unset UPGRADE unset VERBOSE @@ -266,8 +267,8 @@ for option in "${OPTIONS[@]}"; do "h" | "help" ) usage 0 ;; - "p" ) - VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${ARGUMENTS[0]}" + "p" | "python" ) + VIRTUALENV_PYTHON="${ARGUMENTS[0]}" ARGUMENTS=("${ARGUMENTS[@]:1}") # shift 1 ;; "q" | "quiet" ) @@ -284,7 +285,11 @@ for option in "${OPTIONS[@]}"; do exit 0 ;; * ) # virtualenv long options - VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option" + if [[ "$option" == "python="* ]]; then + VIRTUALENV_PYTHON="${option#python=}" + else + VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--$option" + fi ;; esac done @@ -338,6 +343,9 @@ if [ -n "${USE_PYVENV}" ]; then VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade" fi else + if [ -n "${VIRTUALENV_PYTHON}" ]; then + VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${VIRTUALENV_PYTHON}" + fi if [ -z "${HAS_VIRTUALENV}" ]; then install_virtualenv "${PYENV_VERSION}" HAS_VIRTUALENV=1 diff --git a/test/pyvenv.bats b/test/pyvenv.bats index ea04ba6..c5ee74d 100644 --- a/test/pyvenv.bats +++ b/test/pyvenv.bats @@ -97,6 +97,48 @@ rehashed OUT } +@test "install virtualenv if -p has given" { + stub_pyenv "3.4.0" + stub pyenv-which "virtualenv : false" + stub pyenv-which "pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv'" + stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" + stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\"" + + run pyenv-virtualenv -p python3 venv + + unstub_pyenv + unstub pyenv-which + unstub pyenv-exec + + assert_success + assert_output <