65 Commits

Author SHA1 Message Date
Yamashita Yuu
4a6ef11349 v20140614 2014-06-14 14:45:57 +09:00
Yamashita Yuu
21e999cc7c Fix deactivation issue with system version
$ pyenv global system
    $ pyenv shell venv
    (venv) $ pyenv shell --unset
    (venv) $ pyenv version
    system (set by /home/yamashita/.pyenv/version)
    (venv) $ pyenv which python
    /home/yamashita/.pyenv/versions/venv/bin/python # <-- should be `/usr/bin/python` (system)
2014-06-13 19:30:37 +09:00
Yamashita Yuu
e853f74b26 Fix test cases around create_symlinks 2014-06-12 20:35:46 +09:00
Yamashita Yuu
fe5e69846e Create symlinks for executables with version suffix 2014-06-12 20:35:44 +09:00
Yamashita Yuu
c17e4ec9a4 style 2014-06-05 19:31:10 +09:00
Yamashita Yuu
7828a15066 Use pyenv-virtualenv-init to show caveats 2014-06-05 00:17:06 +09:00
Yamashita Yuu
55a2a9e482 Read shell profile from $TMP during tests 2014-06-05 00:16:41 +09:00
Yamashita Yuu
b258bfa907 Display notice about virtualenv-init in activate 2014-06-05 00:08:21 +09:00
Yamashita, Yuu
8838bae3bc Merge pull request #24 from yyuu/automatic-activation
Automatic activation
2014-06-04 20:51:05 +09:00
Yamashita Yuu
b7535395e8 Update README 2014-06-04 20:48:22 +09:00
Yamashita Yuu
6761ed09cd Add test for pyenv-virtualenv-init 2014-06-04 20:48:22 +09:00
Yamashita Yuu
8a837758dc activate: invoke pyenv shell only if the arguments passed 2014-06-04 15:24:36 +09:00
Yamashita Yuu
ea120da0c5 fish: Suppress annoying stderr outputs from pyenv-sh-activate 2014-06-04 15:24:34 +09:00
Yamashita Yuu
af6c157a79 Add eval "$(pyenv virtualenv-init -)" to setup _precmd_ for user's shell 2014-06-04 15:24:12 +09:00
Yamashita Yuu
c9043920db v20140602 2014-06-02 21:42:27 +09:00
Yamashita Yuu
26f2004abb Add semicolon for the deactivate scripts for non-fish 2014-06-02 16:02:39 +09:00
Yamashita, Yuu
b9e2fc6acb Merge pull request #23 from fgimian/fish-activation-fix
Repaired virtualenv activation and deactivation for the fish shell
2014-05-31 14:19:02 +09:00
Fotis Gimian
21a8745bcf Updated tests to correctly pass for fish shell bug fix 2014-05-31 15:15:45 +10:00
Fotis Gimian
f7da0c5808 Repaired virtualenv activation and deactivation for the fish shell 2014-05-31 12:29:19 +10:00
Yamashita Yuu
3f5171e2fc Use new style GH raw url to avoid redirects (raw.github.com -> raw.githubusercontent.com) 2014-04-30 11:52:15 +09:00
Yamashita Yuu
579f4784c8 v20140421 (fixes #19) 2014-04-21 20:24:25 +09:00
Yamashita Yuu
a8940e2f85 Merge branch 'pyvenv-with-p' 2014-04-20 15:27:41 +09:00
Yamashita Yuu
2cab533652 Use virtualenv instead of pyvenv if -p has given (yyuu/pyenv#158) 2014-04-20 15:24:40 +09:00
Yamashita Yuu
5033c3ba0d Update README about pyenv activate --unset (#15) 2014-02-03 16:13:41 +09:00
Yamashita Yuu
a958640776 Fix completion of pyenv activate (fixes #15) 2014-02-03 14:49:47 +09:00
Yamashita Yuu
503a51564e Display error if pyenv activate was invoked as a command 2014-01-25 19:14:58 +09:00
Yamashita Yuu
1db565b67c Update README.
Restart shell after installation of the plugin to enable `pyenv-sh-*`
commands.
2014-01-25 18:39:29 +09:00
Yamashita Yuu
1d291c7c4c v20140123 2014-01-23 19:09:40 +09:00
Yamashita Yuu
f8e1d9c57b Add tests for pyenv-virtualenvs 2014-01-23 15:14:01 +09:00
Yamashita Yuu
4feb64e8d3 Display the version of pyvenv if USE_PYVENV is set 2014-01-23 14:51:56 +09:00
Yamashita Yuu
39ce4e76fd Update change log 2014-01-22 22:01:27 +09:00
Yamashita Yuu
60ddcd8628 Prefer https than git when pulling from GH 2014-01-22 20:16:51 +09:00
Yamashita Yuu
b5b0cd6c72 Merge branch 'unset-pip-require-venv' 2014-01-22 20:10:01 +09:00
Yamashita Yuu
826a7e1a37 Unset PIP_REQUIRE_VENV before executing pip install. (#10) 2014-01-22 20:08:11 +09:00
Yamashita Yuu
a40cbc8acd Add tests for pyenv-virtualenv-prefix 2014-01-21 01:27:03 +09:00
Yamashita Yuu
d0685addc1 Add tests for activate and deactivate 2014-01-21 00:37:33 +09:00
Yamashita Yuu
802cd6b2fa Add .gitignore 2014-01-21 00:04:48 +09:00
Yamashita Yuu
e0012d8aa5 Add build status badge 2014-01-18 18:22:51 +09:00
Yamashita Yuu
87b2f7c6a2 Merge branch 'unittest' 2014-01-18 18:21:11 +09:00
Yamashita Yuu
5e36ec7b51 Add .travis.yml 2014-01-18 18:21:01 +09:00
Yamashita Yuu
dcd3b12a97 Add unittest 2014-01-18 18:19:53 +09:00
Yamashita Yuu
2aa207ede0 Add prepare_upgrade and upgrade 2014-01-18 18:04:59 +09:00
Yamashita Yuu
1cbcee3fc8 Add HAS_VIRTUALENV, HAS_PYVENV and USE_PYVENV 2014-01-18 18:04:45 +09:00
Yamashita Yuu
e7cc57acb8 Preserve original IFS 2014-01-18 18:01:34 +09:00
Yamashita Yuu
dc7b6abfde Display messages only on verbose mode 2014-01-18 18:00:59 +09:00
Yamashita Yuu
88c9633415 Merge branch 'activate' (fixes #9) 2014-01-17 23:24:03 +09:00
Yamashita Yuu
64b2545d5e Hide useless output from pyenv-virtualenv-prefix 2014-01-17 22:43:03 +09:00
Yamashita Yuu
f9d3984909 Hide output from declare -f deactivate 2014-01-17 22:39:37 +09:00
Yamashita Yuu
08951ea22d update README about pyenv activate 2014-01-17 20:26:49 +09:00
Yamashita Yuu
85959c735d Add sh commands to activate/deactivate virtualenv into current shell 2014-01-17 20:17:15 +09:00
Yamashita Yuu
4e61656e9d Modify error messages 2014-01-17 20:17:05 +09:00
Yamashita Yuu
c7da5d53bb Should not exit with error if virtualenv_is_pyvenv returns with non-zero (fixes #8) 2014-01-17 11:39:26 +09:00
Yamashita Yuu
0c2cf9f439 Merge branch 'ensurepip' 2014-01-16 18:33:39 +09:00
Yamashita Yuu
fc01a3d475 Use ensurepip if available 2014-01-16 18:33:02 +09:00
Yamashita Yuu
6e0798ea10 Merge branch 'release-20140110.1' 2014-01-10 15:36:57 +09:00
Yamashita Yuu
6b712a95cb v20140110.1 2014-01-10 15:36:42 +09:00
Yamashita Yuu
95cc31ba6b Import changes from install script of ruby-build v20131225.1 2014-01-10 15:35:10 +09:00
Yamashita Yuu
cebd326821 Merge branch 'release-20140110' 2014-01-10 11:36:08 +09:00
Yamashita Yuu
fa9950aa5b v20140110 2014-01-10 11:35:19 +09:00
Yamashita Yuu
124645e84c Use ez_setup.py and get_pip.py on filesystem if exist 2014-01-09 18:46:44 +09:00
Yamashita Yuu
ec60d5dfcb Support a short option of virtualenv: -p 2014-01-09 11:19:39 +09:00
Yamashita Yuu
d96a00ece3 v20131216 2013-12-16 13:58:10 +09:00
Yamashita, Yuu
20e31ed587 Merge pull request #3 from eikonomega/patch-1
Update README.md
2013-09-03 19:22:37 -07:00
Michael Dunn
32bfce2e41 Update 'Using pyenv virtualenv with pyenv' section.
Explicitly define the parameters that you pass into 'pyenv virtualenv' and what they stand for.
2013-09-03 17:15:53 -04:00
Michael Dunn
99de2882e3 Update README.md
Make it explicit that the 'installing as a plugin' section is assuming that you've installed pyenv in the standard location and what to do if you've done something else.  Clarify the language a little bit about checking out a specific release vs. pulling the current development version.
2013-09-03 17:10:23 -04:00
23 changed files with 1542 additions and 101 deletions

6
.travis.yml Normal file
View File

@@ -0,0 +1,6 @@
install: git clone https://github.com/sstephenson/bats.git
script: bats/bin/bats --tap test
language: c
notifications:
email:
on_success: never

119
README.md
View File

@@ -1,4 +1,6 @@
# pyenv-virtualenv (a.k.a. [python-virtualenv](https://github.com/yyuu/python-virtualenv))
# pyenv-virtualenv
[![Build Status](https://travis-ci.org/yyuu/pyenv-virtualenv.png)](https://travis-ci.org/yyuu/pyenv-virtualenv)
pyenv-virtualenv is a [pyenv](https://github.com/yyuu/pyenv) plugin
that provides a `pyenv virtualenv` command to create virtualenv for Python
@@ -12,15 +14,30 @@ to manage your virtualenvs.)
### Installing as a pyenv plugin
Installing pyenv-virtualenv as a pyenv plugin will give you access to the
`pyenv virtualenv` command.
$ git clone git://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
This will install the latest development version of pyenv-virtualenv into
the `~/.pyenv/plugins/pyenv-virtualenv` directory. From that directory, you
can check out a specific release tag. To update pyenv-virtualenv, run `git
pull` to download the latest changes.
the `~/.pyenv/plugins/pyenv-virtualenv` directory.
**Important note:** If you installed pyenv into a non-standard directory, make sure that you clone this
repo into the 'plugins' directory of wherever you installed into.
From inside that directory you can:
- Check out a specific release tag.
- Get the latest development release by running `git pull` to download the latest changes.
1. **Check out pyenv-virtualenv into plugin directory**
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
2. **Add `pyenv virtualenv-init` to your shell** to enable activation of virtualenv
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
**Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
3. **Restart your shell to the enable pyenv-virtualenv**
$ exec "$SHELL"
### Installing with Homebrew (for OS X users)
@@ -32,11 +49,18 @@ installed, you will also be able to use the `pyenv virtualenv` command.
*This is recommended method of installation if you installed pyenv
with Homebrew.*
brew install pyenv-virtualenv
```
$ brew install pyenv-virtualenv
```
Or, if you would like to install the latest development release:
brew install --HEAD pyenv-virtualenv
```
$ brew install --HEAD pyenv-virtualenv
```
After installation, you'll still need to add `eval "$(pyenv virtualenv-init -)"` to your
profile (as stated in the caveats). You'll only ever have to do this once.
## Usage
@@ -44,13 +68,15 @@ Or, if you would like to install the latest development release:
### Using `pyenv virtualenv` with pyenv
To create a virtualenv for the Python version use with pyenv, run
`pyenv virtualenv` with tha exact name of the version you want to create
virtualenv. For example,
`pyenv virtualenv`, specifying the Python version you want and the name
of the virtualenv directory. For example,
$ pyenv virtualenv 2.7.5 venv27
```
$ pyenv virtualenv 2.7.7 my-virtual-env-2.7.7
```
virtualenvs will be created into a directory of the same name
under `~/.pyenv/versions`.
will create a virtualenv based on Python 2.7.7
under `~/.pyenv/versions` in a folder called `my-virtual-env-2.7.7`.
### Create virtualenv from current version
@@ -59,21 +85,30 @@ If there is only one argument is given to `pyenv virtualenv`,
virtualenv will be created with given name based on current
version.
$ pyenv version
3.3.2 (set by /home/yyuu/.pyenv/version)
$ pyenv virtualenv venv33
```
$ pyenv version
3.4.1 (set by /home/yyuu/.pyenv/version)
$ pyenv virtualenv venv34
```
### List existing virtualenvs
`pyenv virtualenvs` shows you the list of existing virtualenvs.
$ pyenv shell venv27
$ pyenv virtualenvs
* venv27 (created from /home/yyuu/.pyenv/versions/2.7.5)
venv33 (created from /home/yyuu/.pyenv/versions/3.3.2)
```
$ pyenv shell venv27
$ pyenv virtualenvs
* venv27 (created from /home/yyuu/.pyenv/versions/2.7.7)
venv34 (created from /home/yyuu/.pyenv/versions/3.4.1)
```
### Activate virtualenv
Some external tools (e.g. [jedi](https://github.com/davidhalter/jedi)) might require you to `activate` the virtualenv.
The `pyenv-virtualenv` will automatically activate/deactivate the virtualenv if the `eval "$(pyenv virtualenv-init -)"` is properly configured in your shell.
### Special environment variables
You can set certain environment variables to control the pyenv-virtualenv.
@@ -83,6 +118,8 @@ You can set certain environment variables to control the pyenv-virtualenv.
* `VIRTUALENV_VERSION`, if set, forces pyenv-virtualenv to install desired
version of virtualenv. If the virtualenv has not been installed,
pyenv-virtualenv will try to install the given version of virtualenv.
* `EZ_SETUP` and `GET_PIP`, if set and pyvenv is preferred than virtualenv,
use `ez_setup.py` and `get_pip.py` at specified location.
* `EZ_SETUP_URL` and `GET_PIP_URL`, if set and pyvenv is preferred
than virtualenv, download `ez_setup.py` and `get_pip.py` from specified URL.
* `SETUPTOOLS_VERSION` and `PIP_VERSION`, if set and pyvenv is preferred
@@ -91,6 +128,42 @@ You can set certain environment variables to control the pyenv-virtualenv.
## Version History
#### 20140614
* Add `pyenv virtualenv-init` to enable auto-activation feature (#24)
* Create symlinks for executables with version suffix (yyuu/pyenv#182)
#### 20140602
* Use new style GH raw url to avoid redirects (raw.github.com -> raw.githubusercontent.com)
* Repaired virtualenv activation and deactivation for the fish shell (#23)
#### 20140421
* Display error if `pyenv activate` was invoked as a command
* Fix completion of `pyenv activate` (#15)
* Use `virtualenv` instead of `pyvenv` if `-p` has given (yyuu/pyenv#158)
#### 20140123
* Add `activate` and `deactivate` to make `pyenv-virtualenv` work with [jedi](https://github.com/davidhalter/jedi) (#9)
* Use `ensurepip` to install `pip` if it is available
* Unset `PIP_REQUIRE_VENV` to avoid problem on the installation of `virtualenv` (#10)
* Add tests
#### 20140110.1
* Fix install script
#### 20140110
* Support environment variables of `EZ_SETUP` and `GET_PIP`.
* Support a short option `-p` of `virtualenv`.
#### 20131216
* Use latest release of setuptools and pip if the version not given via environment variables.
#### 20130622
* Removed bundled `virtualenv.py` script. Now pyenv-virtualenv installs `virtualenv` package into source version and then use it.

32
bin/pyenv-activate Executable file
View File

@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
# pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --unset
exec pyenv-virtualenvs --bare
fi
{ printf "\x1B[31;1m"
echo
echo "Failed to activate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo
printf "\x1B[0m"
} 1>&2
exit 1

21
bin/pyenv-deactivate Executable file
View File

@@ -0,0 +1,21 @@
#!/usr/bin/env bash
#
# Summary: Deactivate virtual environment
#
# Usage: pyenv deactivate
#
# Deactivate a Python virtual environment.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
{ printf "\x1B[31;1m"
echo
echo "Failed to deactivate virtualenv."
echo
echo "Perhaps pyenv-virtualenv has not been loaded into your shell properly."
echo "Please restart current shell and try again."
echo
printf "\x1B[0m"
} 1>&2
exit 1

74
bin/pyenv-sh-activate Executable file
View File

@@ -0,0 +1,74 @@
#!/usr/bin/env bash
#
# Summary: Activate virtual environment
#
# Usage: pyenv activate <virtualenv>
# pyenv activate --unset
#
# Activate a Python virtualenv environment in current shell.
# This acts almost as same as `pyenv shell`, but this invokes the `activate`
# script in your shell.
#
# <virtualenv> should be a string matching a Python version known to pyenv.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
# Provide pyenv completions
if [ "$1" = "--complete" ]; then
echo --unset
exec pyenv-virtualenvs --bare
fi
if [ "$1" = "--unset" ]; then
echo "pyenv deactivate"
exit
fi
versions=("$@")
if [ -z "$versions" ]; then
no_shell=1
OLDIFS="$IFS"
IFS=: versions=($(pyenv-version-name))
IFS="$OLDIFS"
fi
if [ "${#versions[@]}" -gt 1 ]; then
echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
exit 1
fi
pyenv-virtualenv-prefix "${versions}" 1>/dev/null
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
case "$shell" in
bash )
profile="$HOME/.bash_profile"
;;
zsh )
profile="$HOME/.zshrc"
;;
ksh )
profile="$HOME/.profile"
;;
fish )
profile="$HOME/.config/fish/config.fish"
;;
* )
profile="$HOME/.profile"
;;
esac
# Display setup instruction if 'pyenv virtualenv-init -' is not found in "$profile"
if [ -f "$profile" ] && grep -q 'pyenv init -' "$profile" && ! grep -q 'pyenv virtualenv-init -' "$profile"; then
pyenv-virtualenv-init >&2 || true
fi
if [ -z "$no_shell" ]; then
echo "pyenv shell \"${versions}\";"
fi
case "$shell" in
fish ) echo ". \"$(pyenv-prefix "${versions}")/bin/activate.fish\"" ;;
* ) echo "source \"$(pyenv-prefix "${versions}")/bin/activate\"" ;;
esac

16
bin/pyenv-sh-deactivate Executable file
View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
#
# Summary: Deactivate virtual environment
#
# Usage: pyenv deactivate
#
# Deactivate a Python virtual environment.
set -e
[ -n "$PYENV_DEBUG" ] && set -x
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";;
esac

View File

@@ -11,7 +11,7 @@
# -f/--force Install even if the version appears to be installed already
#
PYENV_VIRTUALENV_VERSION="20130622"
PYENV_VIRTUALENV_VERSION="20140614"
set -e
[ -n "$PYENV_DEBUG" ] && set -x
@@ -25,6 +25,9 @@ if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi
unset PIP_REQUIRE_VENV
unset PIP_REQUIRE_VIRTUALENV
# Define library functions
parse_options() {
OPTIONS=()
@@ -100,8 +103,17 @@ http_get_wget() {
}
version() {
local version="$(venv --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
detect_venv
local version
if [ -n "$USE_PYVENV" ]; then
version="$(pyenv-which pyvenv 2>/dev/null || true)"
version="${version#$(pyenv-root)/versions/}"
version="${version%/bin/pyvenv}"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv ${version:-unknown})"
else
version="$(venv --version 2>/dev/null || true)"
echo "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv ${version:-unknown})"
fi
}
usage() {
@@ -111,23 +123,142 @@ usage() {
[ -z "$1" ] || exit "$1"
}
virtualenv_is_pyvenv() {
# Use pyvenv only if virtualenv is not installed and there is pyvenv
detect_venv() {
if pyenv-which "virtualenv" 1>/dev/null 2>&1; then
return 1
else
pyenv-which "pyvenv" 1>/dev/null 2>&1
HAS_VIRTUALENV=1
fi
if pyenv-which "pyvenv" 1>/dev/null 2>&1; then
HAS_PYVENV=1
fi
# 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
}
venv() {
if virtualenv_is_pyvenv; then
pyenv-exec pyvenv "$@"
local args=("$@")
if [ -n "${USE_PYVENV}" ]; then
pyenv-exec pyvenv "${args[@]}"
local last="${args[${#args[@]}-1]}"
ensurepip "${last##*/}"
else
pyenv-exec virtualenv "$@"
pyenv-exec virtualenv "${args[@]}"
fi
}
install_setuptools() {
local version="$1"
{ if [ "${EZ_SETUP+defined}" ] && [ -f "${EZ_SETUP}" ]; then
if [ -n "$VERBOSE" ]; then
echo "Installing setuptools from ${EZ_SETUP}..." 1>&2
fi
cat "${EZ_SETUP}"
else
[ -n "${EZ_SETUP_URL}" ] || {
if [ -n "${SETUPTOOLS_VERSION}" ]; then
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
else
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py"
fi
}
if [ -n "$VERBOSE" ]; then
echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
fi
http get "${EZ_SETUP_URL}"
fi
} | PYENV_VERSION="${version}" pyenv-exec python
}
install_pip() {
local version="$1"
{ if [ "${GET_PIP+defined}" ] && [ -f "${GET_PIP}" ]; then
if [ -n "$VERBOSE" ]; then
echo "Installing pip from ${GET_PIP}..." 1>&2
fi
cat "${GET_PIP}"
else
[ -n "${GET_PIP_URL}" ] || {
if [ -n "${PIP_VERSION}" ]; then
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
else
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py"
fi
}
if [ -n "$VERBOSE" ]; then
echo "Installing pip from ${GET_PIP_URL}..." 1>&2
fi
http get "${GET_PIP_URL}"
fi
} | PYENV_VERSION="${version}" pyenv-exec python
}
install_virtualenv() {
local version="$1"
VIRTUALENV_VERSION="==${VIRTUALENV_VERSION}"
PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE "virtualenv${VIRTUALENV_VERSION%==}"
}
ensurepip() {
local version="$1"
PYENV_VERSION="${version}" pyenv-exec python -m ensurepip 2>/dev/null || {
PYENV_VERSION="${version}" pyenv-exec python -c "import setuptools" 1>/dev/null 2>&1 || {
install_setuptools "${version}"
} 1>&2
PYENV_VERSION="${version}" pyenv-which pip 1>/dev/null 2>&1 || {
install_pip "${version}"
} 1>&2
}
}
create_symlinks() {
local version="$1"
local suffix="$(PYENV_VERSION="${version}" pyenv-exec python -c 'import sys;v=sys.version_info;sys.stdout.write("%d.%d"%(v[0],v[1]))')"
local file link
shopt -s nullglob
for file in "${VIRTUALENV_PATH}/bin"/*; do
if [[ "${file##*/}" == *"${suffix}" ]]; then
if [[ "${file}" == *"-${suffix}" ]]; then
link="${file%%-${suffix}}"
else
link="${file%%${suffix}}"
fi
if [ ! -e "${link}" ]; then
( cd "${file%/*}" && ln -fs "${file##*/}" "${link##*/}" )
fi
fi
done
shopt -u nullglob
}
prepare_upgrade() {
local version="$1"
local prefix="$2"
PYENV_VERSION="${version}" pyenv-exec pip freeze >"${UPGRADE_LIST}"
mv -f "${prefix}" "${UPGRADE_PATH}"
}
upgrade() {
local version="$1"
local prefix="$2"
## Migrate previously installed packages from requirements.txt
if ! PYENV_VERSION="${version}" pyenv-exec pip install $QUIET $VERBOSE --requirement "${UPGRADE_LIST}"; then
{ echo
echo "UPGRADE FAILED"
echo
echo "Inspect or clean up the original tree at ${UPGRADE_PATH}"
echo
echo "Package list:"
cat "${UPGRADE_LIST}"
} 1>&2
return 1
fi
rm -f "${UPGRADE_LIST}"
rm -fr "${UPGRADE_PATH}"
}
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
if [ -z "${PYENV_VIRTUALENV_CACHE_PATH}" ]; then
PYENV_VIRTUALENV_CACHE_PATH="${PYTHON_BUILD_CACHE_PATH:-${PYENV_ROOT}/cache}"
@@ -135,6 +266,7 @@ fi
VIRTUALENV_OPTIONS=()
unset FORCE
unset VIRTUALENV_PYTHON
unset QUIET
unset UPGRADE
unset VERBOSE
@@ -148,6 +280,10 @@ for option in "${OPTIONS[@]}"; do
"h" | "help" )
usage 0
;;
"p" | "python" )
VIRTUALENV_PYTHON="${ARGUMENTS[0]}"
ARGUMENTS=("${ARGUMENTS[@]:1}") # shift 1
;;
"q" | "quiet" )
QUIET="--quiet"
;;
@@ -162,13 +298,17 @@ 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
if [[ "${#ARGUMENTS[@]}" == 0 ]]; then
echo "pyenv: no virtualenv name given." 1>&2
echo "pyenv-virtualenv: no virtualenv name given." 1>&2
exit 1
elif [[ "${#ARGUMENTS[@]}" == 1 ]]; then
# If only one argument given, use current version as source version
@@ -202,7 +342,12 @@ UPGRADE_LIST="${TMP}/pyenv-virtualenv.${SEED}.txt"
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
if virtualenv_is_pyvenv; then
unset HAS_VIRTUALENV
unset HAS_PYVENV
unset USE_PYVENV
detect_venv
if [ -n "${USE_PYVENV}" ]; then
# Unset some arguments not supported by pyvenv
unset QUIET
unset VERBOSE
@@ -211,11 +356,13 @@ if virtualenv_is_pyvenv; then
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade"
fi
else
pyenv-which "virtualenv" 1>/dev/null 2>&1 || {
VIRTUALENV_VERSION="==${VIRTUALENV_VERSION}"
pyenv-exec pip install $QUIET $VERBOSE "virtualenv${VIRTUALENV_VERSION%==}"
pyenv-rehash
}
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
fi
fi
# Unset environment variables which starts with `VIRTUALENV_`.
@@ -239,9 +386,10 @@ after_virtualenv() {
}
# Load plugin hooks.
for script in $(pyenv-hooks virtualenv); do
source "$script"
done
OLDIFS="$IFS"
IFS=$'\n' scripts=(`pyenv-hooks virtualenv`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done
[ -d "${VIRTUALENV_PATH}" ] && PREFIX_EXISTS=1
@@ -250,7 +398,7 @@ done
# the --force option was specified.
if [ -d "${VIRTUALENV_PATH}/bin" ]; then
if [ -z "$FORCE" ]; then
echo "pyenv: ${VIRTUALENV_PATH} already exists" 1>&2
echo "pyenv-virtualenv: ${VIRTUALENV_PATH} already exists" 1>&2
read -p "continue with installation? (y/N) "
case "$REPLY" in
@@ -260,8 +408,7 @@ if [ -d "${VIRTUALENV_PATH}/bin" ]; then
fi
if [ -n "$UPGRADE" ]; then
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec pip freeze >"${UPGRADE_LIST}"
mv -f "${VIRTUALENV_PATH}" "${UPGRADE_PATH}"
prepare_upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}"
fi
fi
@@ -281,54 +428,15 @@ STATUS=0
# change to cache directory to reuse them between invocation.
mkdir -p "${PYENV_VIRTUALENV_CACHE_PATH}"
cd "${PYENV_VIRTUALENV_CACHE_PATH}"
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" && {
if virtualenv_is_pyvenv; then
[ -n "${EZ_SETUP_URL}" ] || {
if [ -n "${SETUPTOOLS_VERSION}" ]; then
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
else
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py"
fi
}
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec python -c 'import setuptools' 1>/dev/null 2>&1 || {
echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
http get "${EZ_SETUP_URL}" | PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec python
}
[ -n "${GET_PIP_URL}" ] || {
if [ -n "${PIP_VERSION}" ]; then
GET_PIP_URL="https://raw.github.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
else
GET_PIP_URL="https://raw.github.com/pypa/pip/master/contrib/get-pip.py"
fi
}
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-which pip 1>/dev/null 2>&1 || {
echo "Installing pip from ${GET_PIP_URL}..." 1>&2
http get "${GET_PIP_URL}" | PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec python
}
fi
} || STATUS="$?"
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
## Migrate previously installed packages from requirements.txt
if [ -n "$UPGRADE" ]; then
UPGRADE_STATUS=0
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec pip install $QUIET $VERBOSE --requirement "${UPGRADE_LIST}" || UPGRADE_STATUS=$?
if [ "$UPGRADE_STATUS" == "0" ]; then
rm -f "${UPGRADE_LIST}"
rm -fr "${UPGRADE_PATH}"
else
{ echo
echo "UPGRADE FAILED"
echo
echo "Inspect or clean up the original tree at ${UPGRADE_PATH}"
echo
echo "Package list:"
cat "${UPGRADE_LIST}"
} 1>&2
STATUS="$UPGRADE_STATUS"
fi
upgrade "${VIRTUALENV_NAME}" "${VIRTUALENV_PATH}" || STATUS="$?"
fi
create_symlinks "${VIRTUALENV_NAME}"
# Execute `after_virtualenv` hooks
for hook in "${after_hooks[@]}"; do eval "$hook"; done

112
bin/pyenv-virtualenv-init Executable file
View File

@@ -0,0 +1,112 @@
#!/usr/bin/env bash
# Summary: Configure the shell environment for pyenv-virtualenv
# Usage: eval "$(pyenv virtualenv-init - [<shell>])"
#
# Automatically activates a Python virtualenv environment based on current
# pyenv version.
#
set -e
[ -n "$PYENV_DEBUG" ] && set -x
print=""
for args in "$@"
do
if [ "$args" = "-" ]; then
print=1
shift
fi
done
shell="$1"
if [ -z "$shell" ]; then
shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)"
shell="${shell##-}"
shell="${shell%% *}"
shell="$(basename "${shell:-$SHELL}")"
fi
if [ -z "$print" ]; then
case "$shell" in
bash )
profile='~/.bash_profile'
;;
zsh )
profile='~/.zshrc'
;;
ksh )
profile='~/.profile'
;;
fish )
profile='~/.config/fish/config.fish'
;;
* )
profile='your profile'
;;
esac
{ echo "# Load pyenv-virtualenv automatically by adding"
echo "# the following to ${profile}:"
echo
case "$shell" in
fish )
echo 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
;;
* )
echo 'eval "$(pyenv virtualenv-init -)"'
;;
esac
echo
} >&2
exit 1
fi
case "$shell" in
bash )
cat <<EOS
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true;
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then
pyenv deactivate || true;
pyenv activate 2>/dev/null || true
fi
};
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
fi
EOS
;;
fish )
cat <<EOS
function _pyenv_virtualenv_hook --on-event fish_prompt;
if [ (pyenv version-name) = "system" ]
eval (pyenv sh-deactivate); or true
else if [ "\$VIRTUAL_ENV" != (pyenv prefix) ]
eval (pyenv sh-deactivate); or true
eval (pyenv sh-activate 2>/dev/null); or true
end
end
EOS
;;
zsh )
cat <<EOS
_pyenv_virtualenv_hook() {
if [[ "\$(pyenv version-name)" == "system" ]]; then
pyenv deactivate || true
elif [[ "\$VIRTUAL_ENV" != "\$(pyenv prefix)" ]]; then
pyenv deactivate || true
pyenv activate 2>/dev/null || true
fi
}
typeset -a precmd_functions
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
precmd_functions+=_pyenv_virtualenv_hook;
fi
EOS
;;
* )
# FIXME: what should i do here??
;;
esac

View File

@@ -35,11 +35,9 @@ for version in "${versions[@]}"; do
PREFIX="$(pyenv-prefix "${version}")"
if [ -f "${PREFIX}/bin/activate" ]; then
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
if [ -d "${VIRTUALENV_PREFIX_PATH}" ]; then
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH")
fi
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "$VIRTUALENV_PREFIX_PATH")
else
echo "pyenv: version \`${version}' is not a virtualenv" 1>&2
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
exit 1
fi
done

View File

@@ -1,17 +1,18 @@
#!/bin/sh
# Usage: PREFIX=/usr/local ./install.sh
#
# Installs pyenv-virtualenv under $PREFIX.
set -e
cd "$(dirname "$0")"
if [ -z "${PREFIX}" ]; then
PREFIX="/usr/local"
fi
BIN_PATH="${PREFIX}/bin"
mkdir -p "${BIN_PATH}"
mkdir -p "$BIN_PATH"
for file in bin/*; do
cp "${file}" "${BIN_PATH}"
done
echo "Installed pyenv-virtualenv at ${PREFIX}"
install -p bin/* "$BIN_PATH"

92
test/activate.bats Normal file
View File

@@ -0,0 +1,92 @@
#!/usr/bin/env bats
load test_helper
setup() {
export HOME="${TMP}"
export PYENV_ROOT="${TMP}/pyenv"
}
@test "activate virtualenv from current version" {
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
source "${PYENV_ROOT}/versions/venv/bin/activate"
EOS
}
@test "activate virtualenv from current version (fish)" {
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_ROOT}/versions/venv/bin/activate.fish"
EOS
}
@test "activate virtualenv from command-line argument" {
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 "unset invokes deactivate" {
run pyenv-sh-activate --unset
assert_success
assert_output <<EOS
pyenv deactivate
EOS
}
@test "should fail if the version is not a virtualenv" {
stub pyenv-virtualenv-prefix "3.3.3 : false"
run pyenv-sh-activate "3.3.3"
unstub pyenv-virtualenv-prefix
assert_failure
}
@test "should fail if there are multiple versions" {
run pyenv-sh-activate "venv" "venv27"
assert_failure
assert_output <<EOS
pyenv-virtualenv: cannot activate multiple versions at once: venv venv27
EOS
}
@test "should fail if activate is invoked as a command" {
run pyenv-activate
assert_failure
}

31
test/deactivate.bats Normal file
View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
@test "deactivate virtualenv" {
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 (fish)" {
PYENV_SHELL="fish" run pyenv-sh-deactivate
assert_success
assert_output <<EOS
functions -q deactivate; and deactivate
EOS
}
@test "should fail if deactivate is invoked as a command" {
run pyenv-deactivate
assert_failure
}

33
test/hooks.bats Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
export HOOK_PATH="${TMP}/i has hooks"
mkdir -p "$HOOK_PATH"
}
@test "pyenv-virtualenv hooks" {
cat > "${HOOK_PATH}/virtualenv.bash" <<OUT
before_virtualenv 'echo before: \$VIRTUALENV_PATH'
after_virtualenv 'echo after: \$STATUS'
OUT
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/\${PYENV_VERSION}'"
stub pyenv-which "virtualenv : echo '${PYENV_ROOT}/versions/bin/virtualenv'" \
"pyvenv : false"
stub pyenv-hooks "virtualenv : echo '$HOOK_PATH'/virtualenv.bash"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-rehash "echo rehashed"
mkdir -p "${PYENV_ROOT}/versions/3.2.1"
run pyenv-virtualenv "3.2.1" venv
assert_success
assert_output <<-OUT
before: ${PYENV_ROOT}/versions/venv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
after: 0
rehashed
OUT
}

44
test/init.bats Normal file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env bats
load test_helper
@test "detect parent shell" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
SHELL=/bin/false run pyenv-virtualenv-init -
assert_success
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
}
@test "sh-compatible instructions" {
run pyenv-virtualenv-init bash
assert [ "$status" -eq 1 ]
assert_output_contains 'eval "$(pyenv virtualenv-init -)"'
run pyenv-virtualenv-init zsh
assert [ "$status" -eq 1 ]
assert_output_contains 'eval "$(pyenv virtualenv-init -)"'
}
@test "fish instructions" {
run pyenv-virtualenv-init fish
assert [ "$status" -eq 1 ]
assert_output_contains 'status --is-interactive; and . (pyenv virtualenv-init -|psub)'
}
@test "outputs bash-specific syntax" {
run pyenv-virtualenv-init - bash
assert_success
assert_output_contains ' PROMPT_COMMAND="_pyenv_virtualenv_hook;$PROMPT_COMMAND";'
}
@test "outputs fish-specific syntax" {
run pyenv-virtualenv-init - fish
assert_success
assert_output_contains 'function _pyenv_virtualenv_hook --on-event fish_prompt;'
}
@test "outputs zsh-specific syntax" {
run pyenv-virtualenv-init - zsh
assert_success
assert_output_contains ' precmd_functions+=_pyenv_virtualenv_hook;'
}

48
test/installer.bats Normal file
View File

@@ -0,0 +1,48 @@
#!/usr/bin/env bats
load test_helper
@test "installs pyenv-virtualenv into PREFIX" {
cd "$TMP"
PREFIX="${PWD}/usr" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
cd usr
assert [ -x bin/pyenv-activate ]
assert [ -x bin/pyenv-deactivate ]
assert [ -x bin/pyenv-sh-activate ]
assert [ -x bin/pyenv-sh-deactivate ]
assert [ -x bin/pyenv-virtualenv ]
assert [ -x bin/pyenv-virtualenv-init ]
assert [ -x bin/pyenv-virtualenv-prefix ]
assert [ -x bin/pyenv-virtualenvs ]
}
@test "overwrites old installation" {
cd "$TMP"
mkdir -p bin
touch bin/pyenv-virtualenv
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -x bin/pyenv-virtualenv ]
run grep "virtualenv" bin/pyenv-virtualenv
assert_success
}
@test "unrelated files are untouched" {
cd "$TMP"
mkdir -p bin share/bananas
chmod g-w bin
touch bin/bananas
PREFIX="$PWD" run "${BATS_TEST_DIRNAME}/../install.sh"
assert_success ""
assert [ -e bin/bananas ]
run ls -ld bin
assert_equal "r-x" "${output:4:3}"
}

196
test/pyvenv.bats Normal file
View File

@@ -0,0 +1,196 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
stub_pyenv() {
export PYENV_VERSION="$1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/\${PYENV_VERSION}'"
stub pyenv-hooks "virtualenv : echo"
stub pyenv-rehash " : echo rehashed"
}
unstub_pyenv() {
unset PYENV_VERSION
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-hooks
unstub pyenv-rehash
}
create_executable() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
touch "${PYENV_ROOT}/versions/$1/bin/$2"
chmod +x "${PYENV_ROOT}/versions/$1/bin/$2"
}
remove_executable() {
rm -f "${PYENV_ROOT}/versions/$1/bin/$2"
}
@test "use pyvenv if virtualenv is not available" {
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 "bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip3.4\";echo PYENV_VERSION=\${PYENV_VERSION} ensurepip"
stub pyenv-exec "echo 3.4"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv ensurepip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@test "not use pyvenv if virtualenv is available" {
stub_pyenv "3.4.0"
stub pyenv-which "virtualenv : echo '${PYENV_ROOT}/versions/bin/virtualenv'"
stub pyenv-which "pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.4"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@test "install virtualenv if pyvenv is not avaialble" {
stub_pyenv "3.2.1"
stub pyenv-which "virtualenv : false"
stub pyenv-which "pyvenv : false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 pip install virtualenv
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@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} \"\$@\""
stub pyenv-exec "echo 3.4"
run pyenv-virtualenv -p python3 venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 pip install virtualenv
PYENV_VERSION=3.4.0 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@test "install virtualenv if --python 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} \"\$@\""
stub pyenv-exec "echo 3.4"
run pyenv-virtualenv --python=python3 venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.4.0 pip install virtualenv
PYENV_VERSION=3.4.0 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@test "install virtualenv with unsetting troublesome pip options" {
stub_pyenv "3.2.1"
stub pyenv-which "virtualenv : false"
stub pyenv-which "pyvenv : false"
stub pyenv-exec "echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
PIP_REQUIRE_VENV="true" run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PIP_REQUIRE_VENV= PYENV_VERSION=3.2.1 pip install virtualenv
PIP_REQUIRE_VENV= PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}
@test "install pip without using ensurepip" {
stub_pyenv "3.3.0"
stub pyenv-which "virtualenv : false"
stub pyenv-which "pyvenv : echo '${PYENV_ROOT}/versions/bin/pyvenv'"
stub pyenv-which "pip : echo no pip; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} no ensurepip; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} no setuptools; false"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} setuptools"
stub pyenv-exec "bin=\"${PYENV_ROOT}/versions/venv/bin\";mkdir -p \"\$bin\";touch \"\$bin/pip\";echo PYENV_VERSION=\${PYENV_VERSION} pip"
stub pyenv-exec "echo 3.3"
stub curl "echo ez_setup.py"
stub curl "echo get_pip.py"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.3.0 pyvenv ${PYENV_ROOT}/versions/venv
PYENV_VERSION=venv no ensurepip
PYENV_VERSION=venv setuptools
PYENV_VERSION=venv pip
rehashed
OUT
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
unstub_pyenv
unstub pyenv-which
unstub pyenv-exec
}

109
test/stubs/stub Executable file
View File

@@ -0,0 +1,109 @@
#!/usr/bin/env bash
set -e
status=0
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
if [ -n "${!_STUB_DEBUG}" ]; then
echo "$program" "$@" >&${!_STUB_DEBUG}
fi
[ -e "${!_STUB_PLAN}" ] || exit 1
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
# Loop over each line in the plan.
index=0
while IFS= read -r line; do
index=$(($index + 1))
if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
# Split the line into an array of arguments to
# match and a command to run to produce output.
command=" $line"
if [ "$command" != "${command/ : }" ]; then
patterns="${command%% : *}"
command="${command#* : }"
fi
# Naively split patterns by whitespace for now.
# In the future, use a sed script to split while
# respecting quoting.
set -f
patterns=($patterns)
set +f
arguments=("$@")
# Match the expected argument patterns to actual
# arguments.
for (( i=0; i<${#patterns[@]}; i++ )); do
pattern="${patterns[$i]}"
argument="${arguments[$i]}"
case "$argument" in
$pattern ) ;;
* ) result=1 ;;
esac
done
# If the arguments matched, evaluate the command
# in a subshell. Otherwise, log the failure.
if [ $result -eq 0 ] ; then
set +e
( eval "$command" )
status="$?"
set -e
else
eval "${_STUB_RESULT}"=1
fi
fi
done < "${!_STUB_PLAN}"
if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"
# If the number of lines in the plan is larger than
# the requested index, we failed.
if [ $index -ge "${!_STUB_INDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
# Return the result.
exit "${!_STUB_RESULT}"
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
eval "${_STUB_RESULT}"=1
fi
# Write out the run information.
{ echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
} > "${!_STUB_RUN}"
exit "$status"
fi

98
test/test_helper.bash Normal file
View File

@@ -0,0 +1,98 @@
export TMP="$BATS_TEST_DIRNAME/tmp"
PATH=/usr/bin:/usr/sbin:/bin/:/sbin
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
PATH="$TMP/bin:$PATH"
export PATH
teardown() {
rm -fr "$TMP"/*
}
stub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
shift
export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
export "${prefix}_STUB_END"=
mkdir -p "${TMP}/bin"
ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}"
touch "${TMP}/${program}-stub-plan"
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
}
unstub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
local path="${TMP}/bin/${program}"
export "${prefix}_STUB_END"=1
local STATUS=0
"$path" || STATUS="$?"
rm -f "$path"
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
return "$STATUS"
}
assert() {
if ! "$@"; then
flunk "failed: $@"
fi
}
flunk() {
{ if [ "$#" -eq 0 ]; then cat -
else echo "$@"
fi
} | sed "s:${TMP}:\${TMP}:g" >&2
return 1
}
assert_success() {
if [ "$status" -ne 0 ]; then
{ echo "command failed with exit status $status"
echo "output: $output"
} | flunk
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_failure() {
if [ "$status" -eq 0 ]; then
flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_equal() {
if [ "$1" != "$2" ]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_output_contains() {
local expected="$1"
echo "$output" | grep -F "$expected" >/dev/null || {
{ echo "expected output to contain $expected"
echo "actual: $output"
} | flunk
}
}

1
test/tmp/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*

37
test/version.bats Normal file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
export PYENV_VIRTUALENV_VERSION="20140614"
}
@test "display virtualenv version" {
stub pyenv-which "virtualenv : true"
stub pyenv-which "pyvenv : true"
stub pyenv-exec "virtualenv --version : echo \"1.11\""
run pyenv-virtualenv --version
unstub pyenv-which
unstub pyenv-exec
assert_success
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv 1.11)"
}
@test "display pyvenv version" {
stub pyenv-which "virtualenv : false"
stub pyenv-which "pyvenv : echo \"${PYENV_ROOT}/versions/3.3.3/bin/pyvenv\""
stub pyenv-which "pyvenv : echo \"${PYENV_ROOT}/versions/3.3.3/bin/pyvenv\""
stub pyenv-root "echo \"${PYENV_ROOT}\""
run pyenv-virtualenv --version
unstub pyenv-which
unstub pyenv-root
assert_success
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv 3.3.3)"
}

145
test/virtualenv-prefix.bats Normal file
View File

@@ -0,0 +1,145 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
create_virtualenv() {
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
touch "${PYENV_ROOT}/versions/$1/bin/activate"
}
remove_virtualenv() {
rm -fr "${PYENV_ROOT}/versions/$1"
}
@test "display prefix with using sys.real_prefix" {
stub pyenv-version-name "echo venv27"
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\""
stub pyenv-exec "echo \"${PYENV_ROOT}/versions/2.7.6\""
create_virtualenv "venv27"
PYENV_VERSION="venv27" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv27"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/2.7.6
OUT
}
@test "display prefixes with using sys.real_prefix" {
stub pyenv-version-name "echo venv27:venv32"
stub pyenv-prefix "venv27 : echo \"${PYENV_ROOT}/versions/venv27\"" \
"venv32 : echo \"${PYENV_ROOT}/versions/venv32\""
stub pyenv-exec "echo \"${PYENV_ROOT}/versions/2.7.6\"" \
"echo \"${PYENV_ROOT}/versions/3.2.1\""
create_virtualenv "venv27"
create_virtualenv "venv32"
PYENV_VERSION="venv27:venv32" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv27"
remove_virtualenv "venv32"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/2.7.6:${PYENV_ROOT}/versions/3.2.1
OUT
}
@test "display prefix with using sys.base_prefix" {
stub pyenv-version-name "echo venv33"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\""
create_virtualenv "venv33"
PYENV_VERSION="venv33" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/3.3.3
OUT
}
@test "display prefixes with using sys.base_prefix" {
stub pyenv-version-name "echo venv33:venv34"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\"" \
"venv34 : echo \"${PYENV_ROOT}/versions/venv34\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\"" \
"false" \
"echo \"${PYENV_ROOT}/versions/3.4.0\""
create_virtualenv "venv33"
create_virtualenv "venv34"
PYENV_VERSION="venv33:venv34" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
remove_virtualenv "venv34"
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/3.3.3:${PYENV_ROOT}/versions/3.4.0
OUT
}
@test "should fail if the version is not a virtualenv" {
stub pyenv-version-name "echo 3.4.0"
stub pyenv-prefix "3.4.0 : echo \"${PYENV_ROOT}/versions/3.4.0\""
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
PYENV_VERSION="3.4.0" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
rmdir "${PYENV_ROOT}/versions/3.4.0"
assert_failure
assert_output <<OUT
pyenv-virtualenv: version \`3.4.0' is not a virtualenv
OUT
}
@test "should fail if one of the versions is not a virtualenv" {
stub pyenv-version-name "echo venv33:3.4.0"
stub pyenv-prefix "venv33 : echo \"${PYENV_ROOT}/versions/venv33\"" \
"3.4.0 : echo \"${PYENV_ROOT}/versions/3.4.0\""
stub pyenv-exec "false" \
"echo \"${PYENV_ROOT}/versions/3.3.3\""
create_virtualenv "venv33"
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
PYENV_VERSION="venv33:3.4.0" run pyenv-virtualenv-prefix
unstub pyenv-version-name
unstub pyenv-prefix
unstub pyenv-exec
remove_virtualenv "venv33"
rmdir "${PYENV_ROOT}/versions/3.4.0"
assert_failure
assert_output <<OUT
pyenv-virtualenv: version \`3.4.0' is not a virtualenv
OUT
}

98
test/virtualenv.bats Normal file
View File

@@ -0,0 +1,98 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
}
stub_pyenv() {
export PYENV_VERSION="$1"
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/\${PYENV_VERSION}'"
stub pyenv-which "virtualenv : echo '${PYENV_ROOT}/versions/bin/virtualenv'"
stub pyenv-which "pyvenv : false"
stub pyenv-hooks "virtualenv : echo"
stub pyenv-rehash " : echo rehashed"
}
unstub_pyenv() {
unset PYENV_VERSION
unstub pyenv-prefix
unstub pyenv-which
unstub pyenv-hooks
unstub pyenv-rehash
}
@test "create virtualenv from given version" {
stub_pyenv "3.2.1"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv "3.2.1" "venv"
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-exec
}
@test "create virtualenv from current version" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}
@test "create virtualenv with short options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv -v -p python venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}
@test "create virtualenv with long options" {
stub_pyenv "3.2.1"
stub pyenv-version-name "echo \${PYENV_VERSION}"
stub pyenv-exec "echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
stub pyenv-exec "echo 3.2"
run pyenv-virtualenv --verbose --python=python venv
assert_success
assert_output <<OUT
PYENV_VERSION=3.2.1 virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv
rehashed
OUT
unstub_pyenv
unstub pyenv-version-name
unstub pyenv-exec
}

68
test/virtualenvs.bats Normal file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env bats
load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
mkdir -p "${PYENV_ROOT}/versions/2.7.6"
mkdir -p "${PYENV_ROOT}/versions/3.3.3"
mkdir -p "${PYENV_ROOT}/versions/venv27"
mkdir -p "${PYENV_ROOT}/versions/venv33"
}
@test "list virtual environments only" {
stub pyenv-version-name ": echo system"
stub pyenv-versions "--bare : echo \"system\";echo \"2.7.6\";echo \"3.3.3\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "2.7.6 : false"
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\""
run pyenv-virtualenvs
unstub pyenv-version-name
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
venv33 (created from ${PYENV_ROOT}/versions/3.3.3)
OUT
}
@test "list virtual environments with hit prefix" {
stub pyenv-version-name ": echo venv33"
stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\""
stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\""
run pyenv-virtualenvs
unstub pyenv-version-name
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27 (created from /usr)
* venv33 (created from /usr)
OUT
}
@test "list virtual environments with --bare" {
stub pyenv-versions "--bare : echo \"system\";echo \"venv27\";echo \"venv33\""
stub pyenv-virtualenv-prefix "venv27 : echo \"/usr\""
stub pyenv-virtualenv-prefix "venv33 : echo \"/usr\""
run pyenv-virtualenvs --bare
unstub pyenv-versions
unstub pyenv-virtualenv-prefix
assert_success
assert_output <<OUT
venv27
venv33
OUT
}