mirror of
https://github.com/pyenv/pyenv-virtualenv.git
synced 2025-11-08 11:33:55 -05:00
Compare commits
112 Commits
v20140110.
...
v20141106
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad644e83b5 | ||
|
|
0786c7c5b6 | ||
|
|
58e93f9e0d | ||
|
|
2060c43387 | ||
|
|
a02eb0a396 | ||
|
|
1d36ef2f68 | ||
|
|
5795eabaac | ||
|
|
e655abbd41 | ||
|
|
d6671249b7 | ||
|
|
79bfc9d362 | ||
|
|
5252cd0777 | ||
|
|
909e4de414 | ||
|
|
215bda7032 | ||
|
|
86046fd4e8 | ||
|
|
2cc21f112c | ||
|
|
81736da905 | ||
|
|
7304608e0f | ||
|
|
3ebb00414f | ||
|
|
c9bcef656c | ||
|
|
2662b5807c | ||
|
|
fa04d6f9df | ||
|
|
a3c86f013b | ||
|
|
7aa5d13633 | ||
|
|
aef2546319 | ||
|
|
6b4d6249a0 | ||
|
|
0de2cdbbc3 | ||
|
|
abfda4bac9 | ||
|
|
be18fd294e | ||
|
|
d4ce853e94 | ||
|
|
58ed045796 | ||
|
|
c2b5bbd19a | ||
|
|
f0c400b669 | ||
|
|
5b3b909089 | ||
|
|
0b5e0cf592 | ||
|
|
c09a6d3d6c | ||
|
|
e2e69b3b38 | ||
|
|
008a9820bb | ||
|
|
c68685a12f | ||
|
|
08b4c94f85 | ||
|
|
7079cbe979 | ||
|
|
763696b3fb | ||
|
|
0580d566d4 | ||
|
|
1a4fa48106 | ||
|
|
7a6325f8a4 | ||
|
|
1944479bdf | ||
|
|
bad4507541 | ||
|
|
7c8805c343 | ||
|
|
467e9da3a8 | ||
|
|
5168aacdc0 | ||
|
|
877aba8ae7 | ||
|
|
f5cfc95809 | ||
|
|
2fc7e62b78 | ||
|
|
ea18a1b283 | ||
|
|
dcb5371f07 | ||
|
|
56a3225d6c | ||
|
|
dc12a345d8 | ||
|
|
d9e3adc108 | ||
|
|
4a6ef11349 | ||
|
|
21e999cc7c | ||
|
|
e853f74b26 | ||
|
|
fe5e69846e | ||
|
|
c17e4ec9a4 | ||
|
|
7828a15066 | ||
|
|
55a2a9e482 | ||
|
|
b258bfa907 | ||
|
|
8838bae3bc | ||
|
|
b7535395e8 | ||
|
|
6761ed09cd | ||
|
|
8a837758dc | ||
|
|
ea120da0c5 | ||
|
|
af6c157a79 | ||
|
|
c9043920db | ||
|
|
26f2004abb | ||
|
|
b9e2fc6acb | ||
|
|
21a8745bcf | ||
|
|
f7da0c5808 | ||
|
|
3f5171e2fc | ||
|
|
579f4784c8 | ||
|
|
a8940e2f85 | ||
|
|
2cab533652 | ||
|
|
5033c3ba0d | ||
|
|
a958640776 | ||
|
|
503a51564e | ||
|
|
1db565b67c | ||
|
|
1d291c7c4c | ||
|
|
f8e1d9c57b | ||
|
|
4feb64e8d3 | ||
|
|
39ce4e76fd | ||
|
|
60ddcd8628 | ||
|
|
b5b0cd6c72 | ||
|
|
826a7e1a37 | ||
|
|
a40cbc8acd | ||
|
|
d0685addc1 | ||
|
|
802cd6b2fa | ||
|
|
e0012d8aa5 | ||
|
|
87b2f7c6a2 | ||
|
|
5e36ec7b51 | ||
|
|
dcd3b12a97 | ||
|
|
2aa207ede0 | ||
|
|
1cbcee3fc8 | ||
|
|
e7cc57acb8 | ||
|
|
dc7b6abfde | ||
|
|
88c9633415 | ||
|
|
64b2545d5e | ||
|
|
f9d3984909 | ||
|
|
08951ea22d | ||
|
|
85959c735d | ||
|
|
4e61656e9d | ||
|
|
c7da5d53bb | ||
|
|
0c2cf9f439 | ||
|
|
fc01a3d475 | ||
|
|
6e0798ea10 |
6
.travis.yml
Normal file
6
.travis.yml
Normal 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
|
||||
135
README.md
135
README.md
@@ -1,4 +1,6 @@
|
||||
# pyenv-virtualenv (a.k.a. [python-virtualenv](https://github.com/yyuu/python-virtualenv))
|
||||
# pyenv-virtualenv
|
||||
|
||||
[](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,13 +14,9 @@ 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.
|
||||
|
||||
**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.
|
||||
|
||||
@@ -26,6 +24,21 @@ 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. This is entirely optional but pretty useful.
|
||||
|
||||
$ 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)
|
||||
|
||||
Mac OS X users can install pyenv-virtualenv with the
|
||||
@@ -36,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
|
||||
@@ -51,10 +71,12 @@ To create a virtualenv for the Python version use with pyenv, run
|
||||
`pyenv virtualenv`, specifying the Python version you want and the name
|
||||
of the virtualenv directory. For example,
|
||||
|
||||
$ pyenv virtualenv 2.7.6 my-virtual-env-2.7.6
|
||||
```
|
||||
$ pyenv virtualenv 2.7.7 my-virtual-env-2.7.7
|
||||
```
|
||||
|
||||
will create a virtualenv based on Python 2.7.6
|
||||
under `~/.pyenv/versions` in a folder called `my-virtual-env-2.7.6`.
|
||||
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
|
||||
@@ -63,19 +85,49 @@ 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.3 (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.6)
|
||||
venv33 (created from /home/yyuu/.pyenv/versions/3.3.3)
|
||||
```
|
||||
$ 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.
|
||||
|
||||
You can also activate and deactivate a pyenv virtualenv this way:
|
||||
|
||||
pyenv activate <name>
|
||||
pyenv deactivate
|
||||
|
||||
|
||||
### Delete existing virtualenv
|
||||
|
||||
Removing the directory in `~/.pyenv/versions` will delete the virtualenv, or you can run:
|
||||
|
||||
pyenv uninstall my-virtual-env
|
||||
|
||||
|
||||
### virtualenv and pyvenv
|
||||
|
||||
There is [venv](http://docs.python.jp/3/library/venv.html) module available for CPython 3.3 and newer.
|
||||
It provides a command-line tool `pyvenv` which is the successor of `virtualenv` and distributed by default.
|
||||
|
||||
The `pyenv-virtualenv` uses `pyvenv` if it is available and the `virtualenv` is not available.
|
||||
|
||||
|
||||
### Special environment variables
|
||||
@@ -97,6 +149,53 @@ You can set certain environment variables to control the pyenv-virtualenv.
|
||||
|
||||
## Version History
|
||||
|
||||
#### 20141106
|
||||
|
||||
* Stop creating after `ensurepip` since it has done by `ensurepip` itself
|
||||
* Suppress some useless warnings from `pyenv virtualenv-init`
|
||||
|
||||
#### 20141012
|
||||
|
||||
* Fix warnings from `shellcheck` to improve support for POSIX sh (#40)
|
||||
* Do not allow whitespace in `VIRTUALENV_NAME` (#44)
|
||||
* Should not persist `PYENV_DEACTIVATE` after automatic deactivation (#47, #48)
|
||||
|
||||
#### 20140705
|
||||
|
||||
* Display information on auto-(de)?activation
|
||||
* Support manual (de)?activation with auto-activation enabled (#32, #34)
|
||||
* Exit as error when (de)?activation failed
|
||||
* Use https://bootstrap.pypa.io/ to install setuptools and pip
|
||||
* Create backup of original virtualenv within `$(pyenv root)/versions` when `--upgrade`
|
||||
|
||||
#### 20140615
|
||||
|
||||
* Fix incompatibility issue of `pyenv activate` and `pyenv deactivate` (#26)
|
||||
* Workaround for the issue with pyenv-which-ext (#26)
|
||||
|
||||
#### 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
|
||||
|
||||
32
bin/pyenv-activate
Executable file
32
bin/pyenv-activate
Executable 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
21
bin/pyenv-deactivate
Executable 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
|
||||
118
bin/pyenv-sh-activate
Executable file
118
bin/pyenv-sh-activate
Executable file
@@ -0,0 +1,118 @@
|
||||
#!/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
|
||||
|
||||
unset NOERROR
|
||||
unset VERBOSE
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
"--complete" )
|
||||
# Provide pyenv completions
|
||||
echo --unset
|
||||
exec pyenv-virtualenvs --bare
|
||||
;;
|
||||
"--no-error" )
|
||||
NOERROR=1
|
||||
;;
|
||||
"--unset" )
|
||||
echo "pyenv deactivate"
|
||||
exit
|
||||
;;
|
||||
"--verbose" )
|
||||
VERBOSE=1
|
||||
;;
|
||||
* )
|
||||
break
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
versions=("$@")
|
||||
if [ -z "$versions" ]; then
|
||||
no_shell=1
|
||||
OLDIFS="$IFS"
|
||||
IFS=: versions=($(pyenv-version-name))
|
||||
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
|
||||
[ -n "$NOERROR" ] || echo "pyenv-virtualenv: cannot activate multiple versions at once: ${versions[@]}" 1>&2
|
||||
echo "false"
|
||||
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
|
||||
fi
|
||||
|
||||
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 [ -n "$VERBOSE" ]; then
|
||||
echo "pyenv-virtualenv: activate ${versions}" 1>&2
|
||||
fi
|
||||
|
||||
if [ -z "$no_shell" ]; then
|
||||
echo "pyenv shell \"${versions}\";"
|
||||
fi
|
||||
|
||||
prefix="$(pyenv-prefix "${versions}")"
|
||||
case "$shell" in
|
||||
fish )
|
||||
cat <<EOS
|
||||
set -e PYENV_DEACTIVATE;
|
||||
setenv PYENV_ACTIVATE "${prefix}";
|
||||
. "${prefix}/bin/activate.fish";
|
||||
EOS
|
||||
;;
|
||||
* )
|
||||
cat <<EOS
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${prefix}";
|
||||
. "${prefix}/bin/activate";
|
||||
EOS
|
||||
;;
|
||||
esac
|
||||
84
bin/pyenv-sh-deactivate
Executable file
84
bin/pyenv-sh-deactivate
Executable file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Summary: Deactivate virtual environment
|
||||
#
|
||||
# Usage: pyenv deactivate
|
||||
#
|
||||
# Deactivate a Python virtual environment.
|
||||
|
||||
set -e
|
||||
[ -n "$PYENV_DEBUG" ] && set -x
|
||||
|
||||
unset NOERROR
|
||||
unset VERBOSE
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
"--no-error" )
|
||||
NOERROR=1
|
||||
;;
|
||||
"--verbose" )
|
||||
VERBOSE=1
|
||||
;;
|
||||
* )
|
||||
break
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
shell="$(basename "${PYENV_SHELL:-$SHELL}")"
|
||||
case "$shell" in
|
||||
fish )
|
||||
echo "if functions -q deactivate;"
|
||||
;;
|
||||
* )
|
||||
echo "if declare -f deactivate 1>/dev/null 2>&1; then"
|
||||
;;
|
||||
esac
|
||||
|
||||
prefix="${PYENV_ACTIVATE:-${VIRTUAL_ENV}}"
|
||||
|
||||
if [ -n "$VERBOSE" ]; then
|
||||
echo " echo \"pyenv-virtualenv: deactivate ${prefix##*/}\" 1>&2;"
|
||||
fi
|
||||
|
||||
if [ -z "${PYENV_VIRTUALENV_INIT}" ]; then
|
||||
# Backward compatibility issue
|
||||
# https://github.com/yyuu/pyenv-virtualenv/issues/26
|
||||
echo " pyenv shell --unset;"
|
||||
fi
|
||||
|
||||
case "$shell" in
|
||||
fish )
|
||||
cat <<EOS
|
||||
setenv PYENV_DEACTIVATE "$prefix";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
EOS
|
||||
;;
|
||||
* )
|
||||
cat <<EOS
|
||||
export PYENV_DEACTIVATE="$prefix";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
EOS
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "$NOERROR" ]; then
|
||||
echo " echo \"pyenv-virtualenv: no virtualenv has been activated.\" 1>&2;"
|
||||
fi
|
||||
|
||||
echo " false;"
|
||||
|
||||
case "$shell" in
|
||||
fish )
|
||||
echo "end;"
|
||||
;;
|
||||
* )
|
||||
echo "fi;"
|
||||
;;
|
||||
esac
|
||||
@@ -2,16 +2,14 @@
|
||||
#
|
||||
# Summary: Create a Python virtualenv using the pyenv-virtualenv plugin
|
||||
#
|
||||
# Usage: pyenv virtualenv [-f|--force] [-u|--upgrade] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
|
||||
# Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] <version> <virtualenv-name>
|
||||
# pyenv virtualenv --version
|
||||
# pyenv virtualenv --help
|
||||
#
|
||||
# -u/--upgrade Upgrade existing virtualenv to use new version of Python,
|
||||
# assuming Python has been upgraded in-place.
|
||||
# -f/--force Install even if the version appears to be installed already
|
||||
#
|
||||
|
||||
PYENV_VIRTUALENV_VERSION="20140110.1"
|
||||
PYENV_VIRTUALENV_VERSION="20141106"
|
||||
|
||||
set -e
|
||||
[ -n "$PYENV_DEBUG" ] && set -x
|
||||
@@ -25,6 +23,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 +101,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,59 +121,93 @@ usage() {
|
||||
[ -z "$1" ] || exit "$1"
|
||||
}
|
||||
|
||||
virtualenv_is_pyvenv() {
|
||||
# Use pyvenv only if virtualenv is not installed and there is pyvenv
|
||||
if pyenv-which "virtualenv" 1>/dev/null 2>&1; then
|
||||
return 1
|
||||
else
|
||||
pyenv-which "pyvenv" 1>/dev/null 2>&1
|
||||
detect_venv() {
|
||||
# Check the existence of executables as a workaround for the issue with pyenv-which-ext
|
||||
# https://github.com/yyuu/pyenv-virtualenv/issues/26
|
||||
if [ -x "$(pyenv-prefix)/bin/virtualenv" ]; then
|
||||
HAS_VIRTUALENV=1
|
||||
fi
|
||||
if [ -x "$(pyenv-prefix)/bin/pyvenv" ]; 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[@]}"
|
||||
else
|
||||
pyenv-exec virtualenv "$@"
|
||||
pyenv-exec virtualenv "${args[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
install_setuptools() {
|
||||
local version="$1"
|
||||
build_package_ez_setup() {
|
||||
local ez_setup="${PYENV_VIRTUALENV_CACHE_PATH}/ez_setup.py"
|
||||
rm -f "${ez_setup}"
|
||||
{ if [ "${EZ_SETUP+defined}" ] && [ -f "${EZ_SETUP}" ]; then
|
||||
echo "Installing setuptools from ${EZ_SETUP}..." 1>&2
|
||||
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
|
||||
}
|
||||
[ -n "${EZ_SETUP_URL}" ] || EZ_SETUP_URL="https://bootstrap.pypa.io/ez_setup.py"
|
||||
echo "Installing setuptools from ${EZ_SETUP_URL}..." 1>&2
|
||||
http get "${EZ_SETUP_URL}"
|
||||
fi
|
||||
} | PYENV_VERSION="${version}" pyenv-exec python
|
||||
} 1> "${ez_setup}"
|
||||
pyenv-exec python "${ez_setup}" ${EZ_SETUP_OPTS} 1>&2 || {
|
||||
echo "error: failed to install setuptools via ez_setup.py" >&2
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
install_pip() {
|
||||
local version="$1"
|
||||
build_package_get_pip() {
|
||||
local get_pip="${PYENV_VIRTUALENV_CACHE_PATH}/get-pip.py"
|
||||
rm -f "${get_pip}"
|
||||
{ if [ "${GET_PIP+defined}" ] && [ -f "${GET_PIP}" ]; then
|
||||
echo "Installing pip from ${GET_PIP}..." 1>&2
|
||||
cat "${GET_PIP}"
|
||||
else
|
||||
[ -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
|
||||
}
|
||||
[ -n "${GET_PIP_URL}" ] || GET_PIP_URL="https://bootstrap.pypa.io/get-pip.py"
|
||||
echo "Installing pip from ${GET_PIP_URL}..." 1>&2
|
||||
http get "${GET_PIP_URL}"
|
||||
fi
|
||||
} | PYENV_VERSION="${version}" pyenv-exec python
|
||||
} 1> "${get_pip}"
|
||||
pyenv-exec python "${get_pip}" ${GET_PIP_OPTS} 1>&2 || {
|
||||
echo "error: failed to install pip via get-pip.py" >&2
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
build_package_ensurepip() {
|
||||
pyenv-exec python -m ensurepip 2>/dev/null|| {
|
||||
build_package_ez_setup "$@" && build_package_get_pip "$@"
|
||||
} || return 1
|
||||
}
|
||||
|
||||
prepare_requirements() {
|
||||
pyenv-exec pip freeze > "${REQUIREMENTS}"
|
||||
mv -f "${VIRTUALENV_PATH}" "${VIRTUALENV_ORIG}"
|
||||
}
|
||||
|
||||
install_requirements() {
|
||||
if [ -f "${REQUIREMENTS}" ]; then
|
||||
## Migrate previously installed packages from requirements.txt
|
||||
pyenv-exec pip install $QUIET $VERBOSE --requirement "${REQUIREMENTS}" || {
|
||||
echo
|
||||
echo "PIP INSTALL FAILED"
|
||||
echo
|
||||
echo "Inspect or clean up the original tree at ${VIRTUALENV_ORIG}"
|
||||
echo
|
||||
echo "Package list:"
|
||||
cat "${REQUIREMENTS}" | sed 's/^/ * /'
|
||||
return 1
|
||||
} 1>&2
|
||||
rm -f "${REQUIREMENTS}"
|
||||
rm -fr "${VIRTUALENV_ORIG}"
|
||||
fi
|
||||
}
|
||||
|
||||
PYENV_VIRTUALENV_ROOT="$(abs_dirname "$0")/.."
|
||||
@@ -173,6 +217,7 @@ fi
|
||||
VIRTUALENV_OPTIONS=()
|
||||
|
||||
unset FORCE
|
||||
unset VIRTUALENV_PYTHON
|
||||
unset QUIET
|
||||
unset UPGRADE
|
||||
unset VERBOSE
|
||||
@@ -186,8 +231,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" )
|
||||
@@ -204,13 +249,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
|
||||
@@ -226,6 +275,11 @@ if [ -z "${VERSION_NAME}" ] || [ -z "${VIRTUALENV_NAME}" ]; then
|
||||
usage 1
|
||||
fi
|
||||
|
||||
if [ "$VIRTUALENV_NAME" != "${VIRTUALENV_NAME%[[:space:]]*}" ]; then
|
||||
echo "pyenv-virtualenv: no whitespace allowed in virtualenv name." 1>&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set VERSION_NAME as default version in this script
|
||||
export PYENV_VERSION="${VERSION_NAME}"
|
||||
|
||||
@@ -238,26 +292,40 @@ else
|
||||
TMP="${TMPDIR%/}"
|
||||
fi
|
||||
|
||||
SEED="$(date "+%Y%m%d%H%M%S").$$"
|
||||
UPGRADE_PATH="${TMP}/pyenv-virtualenv.${SEED}"
|
||||
UPGRADE_LIST="${TMP}/pyenv-virtualenv.${SEED}.txt"
|
||||
|
||||
VIRTUALENV_PATH="${PYENV_ROOT}/versions/${VIRTUALENV_NAME}"
|
||||
|
||||
if virtualenv_is_pyvenv; then
|
||||
# Unset some arguments not supported by pyvenv
|
||||
unset QUIET
|
||||
unset VERBOSE
|
||||
if [ -n "$UPGRADE" ]; then
|
||||
unset HAS_VIRTUALENV
|
||||
unset HAS_PYVENV
|
||||
unset USE_PYVENV
|
||||
detect_venv
|
||||
|
||||
SEED="$(date "+%Y%m%d%H%M%S").$$"
|
||||
VIRTUALENV_ORIG="${VIRTUALENV_PATH}.${SEED}"
|
||||
REQUIREMENTS="${TMP}/requirements.${SEED}.txt"
|
||||
|
||||
# Upgrade existing virtualenv
|
||||
if [ -n "$UPGRADE" ]; then
|
||||
FORCE=1
|
||||
# pyvenv has `--upgrade` by default
|
||||
if [ -n "${USE_PYVENV}" ]; then
|
||||
unset UPGRADE
|
||||
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--upgrade"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "${USE_PYVENV}" ]; then
|
||||
# Unset some arguments not supported by pyvenv
|
||||
unset QUIET
|
||||
unset VERBOSE
|
||||
else
|
||||
pyenv-which "virtualenv" 1>/dev/null 2>&1 || {
|
||||
if [ -n "${VIRTUALENV_PYTHON}" ]; then
|
||||
VIRTUALENV_OPTIONS[${#VIRTUALENV_OPTIONS[*]}]="--python=${VIRTUALENV_PYTHON}"
|
||||
fi
|
||||
if [ -z "${HAS_VIRTUALENV}" ]; then
|
||||
VIRTUALENV_VERSION="==${VIRTUALENV_VERSION}"
|
||||
pyenv-exec pip install $QUIET $VERBOSE "virtualenv${VIRTUALENV_VERSION%==}"
|
||||
pyenv-rehash
|
||||
}
|
||||
HAS_VIRTUALENV=1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Unset environment variables which starts with `VIRTUALENV_`.
|
||||
@@ -265,6 +333,15 @@ fi
|
||||
unset VIRTUALENV_VERSION
|
||||
|
||||
|
||||
# Download specified version of ez_setup.py/get-pip.py
|
||||
if [ -n "${SETUPTOOLS_VERSION}" ]; then
|
||||
EZ_SETUP_URL="https://bitbucket.org/pypa/setuptools/raw/${SETUPTOOLS_VERSION}/ez_setup.py"
|
||||
fi
|
||||
if [ -n "${PIP_VERSION}" ]; then
|
||||
GET_PIP_URL="https://raw.githubusercontent.com/pypa/pip/${PIP_VERSION}/contrib/get-pip.py"
|
||||
fi
|
||||
|
||||
|
||||
# Define `before_virtualenv` and `after_virtualenv` functions that allow
|
||||
# plugin hooks to register a string of code for execution before or
|
||||
# after the installation process.
|
||||
@@ -281,9 +358,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
|
||||
@@ -292,7 +370,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
|
||||
@@ -302,8 +380,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}"
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" prepare_requirements
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -323,37 +400,13 @@ 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
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-exec python -c 'import setuptools' 1>/dev/null 2>&1 || {
|
||||
install_setuptools "${VIRTUALENV_NAME}" 1>&2
|
||||
}
|
||||
venv $QUIET $VERBOSE "${VIRTUALENV_OPTIONS[@]}" "${VIRTUALENV_PATH}" || STATUS="$?"
|
||||
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" pyenv-which pip 1>/dev/null 2>&1 || {
|
||||
install_pip "${VIRTUALENV_NAME}" 1>&2
|
||||
}
|
||||
fi
|
||||
} || STATUS="$?"
|
||||
## Install setuptools and pip
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" build_package_ensurepip
|
||||
|
||||
## 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
|
||||
fi
|
||||
PYENV_VERSION="${VIRTUALENV_NAME}" install_requirements || true
|
||||
|
||||
# Execute `after_virtualenv` hooks
|
||||
for hook in "${after_hooks[@]}"; do eval "$hook"; done
|
||||
|
||||
156
bin/pyenv-virtualenv-init
Executable file
156
bin/pyenv-virtualenv-init
Executable file
@@ -0,0 +1,156 @@
|
||||
#!/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
|
||||
fish )
|
||||
echo "setenv PYENV_VIRTUALENV_INIT 1;"
|
||||
;;
|
||||
* )
|
||||
echo "export PYENV_VIRTUALENV_INIT=1;"
|
||||
;;
|
||||
esac
|
||||
|
||||
case "$shell" in
|
||||
fish )
|
||||
cat <<EOS
|
||||
function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
if [ -n "\$PYENV_ACTIVATE" ]
|
||||
if [ (pyenv version-name) = "system" ]
|
||||
pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
return 0
|
||||
end
|
||||
if [ "\$PYENV_ACTIVATE" != (pyenv prefix 2>/dev/null) ]
|
||||
if pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
|
||||
else
|
||||
pyenv activate --no-error --verbose
|
||||
end
|
||||
end
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null) ]
|
||||
pyenv activate --no-error --verbose
|
||||
end
|
||||
end
|
||||
end
|
||||
EOS
|
||||
;;
|
||||
ksh )
|
||||
cat <<EOS
|
||||
function _pyenv_virtualenv_hook() {
|
||||
EOS
|
||||
;;
|
||||
* )
|
||||
cat <<EOS
|
||||
_pyenv_virtualenv_hook() {
|
||||
EOS
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ "$shell" != "fish" ]]; then
|
||||
cat <<EOS
|
||||
if [ -n "\$PYENV_ACTIVATE" ]; then
|
||||
if [ "\$(pyenv version-name)" = "system" ]; then
|
||||
pyenv deactivate --no-error --verbose
|
||||
unset PYENV_DEACTIVATE
|
||||
return 0
|
||||
fi
|
||||
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
if pyenv deactivate --no-error --verbose; then
|
||||
unset PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE
|
||||
else
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
};
|
||||
EOS
|
||||
|
||||
case "$shell" in
|
||||
bash )
|
||||
cat <<EOS
|
||||
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
|
||||
PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
|
||||
fi
|
||||
EOS
|
||||
;;
|
||||
zsh )
|
||||
cat <<EOS
|
||||
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
|
||||
fi
|
||||
@@ -32,14 +32,16 @@ base_prefix() { # pyvenv
|
||||
|
||||
VIRTUALENV_PREFIX_PATHS=()
|
||||
for version in "${versions[@]}"; do
|
||||
if [ "$version" = "system" ]; then
|
||||
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
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
|
||||
|
||||
217
test/activate.bats
Normal file
217
test/activate.bats
Normal file
@@ -0,0 +1,217 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export HOME="${TMP}"
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
}
|
||||
|
||||
@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\""
|
||||
|
||||
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
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv";
|
||||
. "${PYENV_ROOT}/versions/venv/bin/activate";
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "activate virtualenv from current version (verbose)" {
|
||||
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\""
|
||||
|
||||
PYENV_SHELL="bash" PYENV_VERSION="venv" run pyenv-sh-activate --verbose
|
||||
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
pyenv-virtualenv: activate venv
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv";
|
||||
. "${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";
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv";
|
||||
. "${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\""
|
||||
|
||||
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
|
||||
set -e PYENV_DEACTIVATE;
|
||||
setenv PYENV_ACTIVATE "${PYENV_ROOT}/versions/venv";
|
||||
. "${PYENV_ROOT}/versions/venv/bin/activate.fish";
|
||||
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";
|
||||
set -e PYENV_DEACTIVATE;
|
||||
setenv PYENV_ACTIVATE "${PYENV_ROOT}/versions/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";
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv27";
|
||||
. "${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\""
|
||||
|
||||
run pyenv-sh-activate "venv27"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
unstub pyenv-prefix
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
pyenv shell "venv27";
|
||||
unset PYENV_DEACTIVATE;
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv27";
|
||||
. "${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
|
||||
assert_output <<EOS
|
||||
pyenv-virtualenv: version \`3.3.3' is not a virtualenv
|
||||
false
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "should fail if the version is not a virtualenv (no-error)" {
|
||||
stub pyenv-virtualenv-prefix "3.3.3 : false"
|
||||
|
||||
run pyenv-sh-activate --no-error "3.3.3"
|
||||
|
||||
unstub pyenv-virtualenv-prefix
|
||||
|
||||
assert_failure
|
||||
assert_output <<EOS
|
||||
false
|
||||
EOS
|
||||
}
|
||||
|
||||
@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
|
||||
false
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "should fail if there are multiple versions (no-error)" {
|
||||
run pyenv-sh-activate --no-error "venv" "venv27"
|
||||
|
||||
assert_failure
|
||||
assert_output <<EOS
|
||||
false
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "should fail if activate is invoked as a command" {
|
||||
run pyenv-activate
|
||||
|
||||
assert_failure
|
||||
}
|
||||
223
test/deactivate.bats
Normal file
223
test/deactivate.bats
Normal file
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (verbose)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate --verbose
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
echo "pyenv-virtualenv: deactivate venv" 1>&2;
|
||||
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (no-error)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (without pyenv-virtualenv-init)" {
|
||||
export PYENV_VIRTUALENV_INIT=
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
pyenv shell --unset;
|
||||
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (without pyenv-virtualenv-init) (no-error)" {
|
||||
export PYENV_VIRTUALENV_INIT=
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate --no-error
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
pyenv shell --unset;
|
||||
export PYENV_DEACTIVATE="$PYENV_ACTIVATE";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv which has been activated manually" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="bash" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if declare -f deactivate 1>/dev/null 2>&1; then
|
||||
export PYENV_DEACTIVATE="$VIRTUAL_ENV";
|
||||
unset PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
fi;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (fish)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="fish" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if functions -q deactivate;
|
||||
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
end;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (fish) (no-error)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if functions -q deactivate;
|
||||
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
false;
|
||||
end;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init)" {
|
||||
export PYENV_VIRTUALENV_INIT=
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="fish" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if functions -q deactivate;
|
||||
pyenv shell --unset;
|
||||
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
end;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv (fish) (without pyenv-virtualenv-init) (no-error)" {
|
||||
export PYENV_VIRTUALENV_INIT=
|
||||
export PYENV_ACTIVATE="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="fish" run pyenv-sh-deactivate --no-error
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if functions -q deactivate;
|
||||
pyenv shell --unset;
|
||||
setenv PYENV_DEACTIVATE "$PYENV_ACTIVATE";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
false;
|
||||
end;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "deactivate virtualenv which has been activated manually (fish)" {
|
||||
export PYENV_VIRTUALENV_INIT=1
|
||||
export VIRTUAL_ENV="${PYENV_ROOT}/versions/venv"
|
||||
|
||||
PYENV_SHELL="fish" run pyenv-sh-deactivate
|
||||
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
if functions -q deactivate;
|
||||
setenv PYENV_DEACTIVATE "$VIRTUAL_ENV";
|
||||
set -e PYENV_ACTIVATE;
|
||||
deactivate;
|
||||
else;
|
||||
echo "pyenv-virtualenv: no virtualenv has been activated." 1>&2;
|
||||
false;
|
||||
end;
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "should fail if deactivate is invoked as a command" {
|
||||
run pyenv-deactivate
|
||||
|
||||
assert_failure
|
||||
}
|
||||
35
test/hooks.bats
Normal file
35
test/hooks.bats
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/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/3.2.1'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
|
||||
stub pyenv-hooks "virtualenv : echo '$HOOK_PATH'/virtualenv.bash"
|
||||
stub pyenv-exec "echo PYENV_VERSION=3.2.1 \"\$@\""
|
||||
stub pyenv-rehash "echo rehashed"
|
||||
|
||||
create_executable "3.2.1" "virtualenv"
|
||||
remove_executable "3.2.1" "pyvenv"
|
||||
|
||||
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
|
||||
}
|
||||
120
test/init.bats
Normal file
120
test/init.bats
Normal file
@@ -0,0 +1,120 @@
|
||||
#!/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 <<EOS
|
||||
export PYENV_VIRTUALENV_INIT=1;
|
||||
_pyenv_virtualenv_hook() {
|
||||
if [ -n "\$PYENV_ACTIVATE" ]; then
|
||||
if [ "\$(pyenv version-name)" = "system" ]; then
|
||||
pyenv deactivate --no-error --verbose
|
||||
unset PYENV_DEACTIVATE
|
||||
return 0
|
||||
fi
|
||||
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
if pyenv deactivate --no-error --verbose; then
|
||||
unset PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE
|
||||
else
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
};
|
||||
if ! [[ "\$PROMPT_COMMAND" =~ _pyenv_virtualenv_hook ]]; then
|
||||
PROMPT_COMMAND="_pyenv_virtualenv_hook;\$PROMPT_COMMAND";
|
||||
fi
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "outputs fish-specific syntax" {
|
||||
run pyenv-virtualenv-init - fish
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
setenv PYENV_VIRTUALENV_INIT 1;
|
||||
function _pyenv_virtualenv_hook --on-event fish_prompt;
|
||||
if [ -n "\$PYENV_ACTIVATE" ]
|
||||
if [ (pyenv version-name) = "system" ]
|
||||
pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
return 0
|
||||
end
|
||||
if [ "\$PYENV_ACTIVATE" != (pyenv prefix 2>/dev/null) ]
|
||||
if pyenv deactivate --no-error --verbose
|
||||
set -e PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose; or set -e PYENV_DEACTIVATE
|
||||
else
|
||||
pyenv activate --no-error --verbose
|
||||
end
|
||||
end
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ]; and [ "\$PYENV_DEACTIVATE" != (pyenv prefix 2>/dev/null) ]
|
||||
pyenv activate --no-error --verbose
|
||||
end
|
||||
end
|
||||
end
|
||||
EOS
|
||||
}
|
||||
|
||||
@test "outputs zsh-specific syntax" {
|
||||
run pyenv-virtualenv-init - zsh
|
||||
assert_success
|
||||
assert_output <<EOS
|
||||
export PYENV_VIRTUALENV_INIT=1;
|
||||
_pyenv_virtualenv_hook() {
|
||||
if [ -n "\$PYENV_ACTIVATE" ]; then
|
||||
if [ "\$(pyenv version-name)" = "system" ]; then
|
||||
pyenv deactivate --no-error --verbose
|
||||
unset PYENV_DEACTIVATE
|
||||
return 0
|
||||
fi
|
||||
if [ "\$PYENV_ACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
if pyenv deactivate --no-error --verbose; then
|
||||
unset PYENV_DEACTIVATE
|
||||
pyenv activate --no-error --verbose || unset PYENV_DEACTIVATE
|
||||
else
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
else
|
||||
if [ -z "\$VIRTUAL_ENV" ] && [ "\$PYENV_DEACTIVATE" != "\$(pyenv prefix 2>/dev/null)" ]; then
|
||||
pyenv activate --no-error --verbose
|
||||
fi
|
||||
fi
|
||||
};
|
||||
typeset -a precmd_functions
|
||||
if [[ -z \$precmd_functions[(r)_pyenv_virtualenv_hook] ]]; then
|
||||
precmd_functions+=_pyenv_virtualenv_hook;
|
||||
fi
|
||||
EOS
|
||||
}
|
||||
48
test/installer.bats
Normal file
48
test/installer.bats
Normal 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}"
|
||||
}
|
||||
78
test/pip.bats
Normal file
78
test/pip.bats
Normal file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
}
|
||||
|
||||
stub_pyenv() {
|
||||
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() {
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-hooks
|
||||
unstub pyenv-rehash
|
||||
}
|
||||
|
||||
@test "install pip with ensurepip" {
|
||||
export PYENV_VERSION="3.4.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pyvenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";mkdir -p \${PYENV_ROOT}/versions/venv/bin"
|
||||
stub pyenv-exec "python -m ensurepip : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";touch \${PYENV_ROOT}/versions/venv/bin/pip"
|
||||
|
||||
remove_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.4.1 pyvenv ${PYENV_ROOT}/versions/venv
|
||||
PYENV_VERSION=venv python -m ensurepip
|
||||
rehashed
|
||||
OUT
|
||||
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "install pip without using ensurepip" {
|
||||
export PYENV_VERSION="3.3.5"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pyvenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";mkdir -p \${PYENV_ROOT}/versions/venv/bin"
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";touch \${PYENV_ROOT}/versions/venv/bin/easy_install"
|
||||
stub pyenv-exec "python */get-pip.py : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\";touch \${PYENV_ROOT}/versions/venv/bin/pip"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
remove_executable "3.3.5" "virtualenv"
|
||||
create_executable "3.3.5" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.3.5 pyvenv ${PYENV_ROOT}/versions/venv
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
PYENV_VERSION=venv python ${TMP}/pyenv/cache/ez_setup.py
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
PYENV_VERSION=venv python ${TMP}/pyenv/cache/get-pip.py
|
||||
rehashed
|
||||
OUT
|
||||
assert [ -e "${PYENV_ROOT}/versions/venv/bin/pip" ]
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
181
test/pyvenv.bats
Normal file
181
test/pyvenv.bats
Normal file
@@ -0,0 +1,181 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
}
|
||||
|
||||
stub_pyenv() {
|
||||
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() {
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-hooks
|
||||
unstub pyenv-rehash
|
||||
}
|
||||
|
||||
@test "use pyvenv if virtualenv is not available" {
|
||||
export PYENV_VERSION="3.4.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pyvenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : true"
|
||||
|
||||
remove_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.4.1 pyvenv ${PYENV_ROOT}/versions/venv
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "not use pyvenv if virtualenv is available" {
|
||||
export PYENV_VERSION="3.4.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "virtualenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : true"
|
||||
|
||||
create_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.4.1 virtualenv ${PYENV_ROOT}/versions/venv
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "install virtualenv if pyvenv is not avaialble" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pip install virtualenv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "virtualenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
remove_executable "3.2.1" "virtualenv"
|
||||
remove_executable "3.2.1" "pyvenv"
|
||||
|
||||
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
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
|
||||
@test "install virtualenv if -p has given" {
|
||||
export PYENV_VERSION="3.4.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pip install virtualenv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "virtualenv --python=python3 ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : true"
|
||||
|
||||
remove_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv -p python3 venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.4.1 pip install virtualenv
|
||||
PYENV_VERSION=3.4.1 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "install virtualenv if --python has given" {
|
||||
export PYENV_VERSION="3.4.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pip install virtualenv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "virtualenv --python=python3 ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : true"
|
||||
|
||||
remove_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv --python=python3 venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.4.1 pip install virtualenv
|
||||
PYENV_VERSION=3.4.1 virtualenv --python=python3 ${PYENV_ROOT}/versions/venv
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "install virtualenv with unsetting troublesome pip options" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix " : echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-exec "pip install virtualenv : echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "virtualenv ${PYENV_ROOT}/versions/venv : echo PIP_REQUIRE_VENV=\${PIP_REQUIRE_VENV} PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
remove_executable "3.2.1" "virtualenv"
|
||||
remove_executable "3.2.1" "pyvenv"
|
||||
|
||||
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
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
109
test/stubs/stub
Executable file
109
test/stubs/stub
Executable 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
|
||||
108
test/test_helper.bash
Normal file
108
test/test_helper.bash
Normal file
@@ -0,0 +1,108 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
1
test/tmp/.gitignore
vendored
Normal file
1
test/tmp/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*
|
||||
43
test/version.bats
Normal file
43
test/version.bats
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
export PYENV_VIRTUALENV_VERSION="20141106"
|
||||
}
|
||||
|
||||
@test "display virtualenv version" {
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/2.7.7'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/2.7.7'"
|
||||
stub pyenv-exec "virtualenv --version : echo \"1.11\""
|
||||
|
||||
create_executable "2.7.7" "virtualenv"
|
||||
remove_executable "2.7.7" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv --version
|
||||
|
||||
assert_success
|
||||
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (virtualenv 1.11)"
|
||||
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-exec
|
||||
}
|
||||
|
||||
@test "display pyvenv version" {
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.4.1'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.4.1'"
|
||||
stub pyenv-which "pyvenv : echo \"${PYENV_ROOT}/versions/3.4.1/bin/pyvenv\""
|
||||
stub pyenv-root "echo \"${PYENV_ROOT}\""
|
||||
|
||||
remove_executable "3.4.1" "virtualenv"
|
||||
create_executable "3.4.1" "pyvenv"
|
||||
|
||||
run pyenv-virtualenv --version
|
||||
|
||||
assert_success
|
||||
assert_output "pyenv-virtualenv ${PYENV_VIRTUALENV_VERSION} (pyvenv 3.4.1)"
|
||||
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-root
|
||||
}
|
||||
158
test/virtualenv-prefix.bats
Normal file
158
test/virtualenv-prefix.bats
Normal file
@@ -0,0 +1,158 @@
|
||||
#!/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 the system" {
|
||||
stub pyenv-version-name "echo system"
|
||||
|
||||
PYENV_VERSION="system" run pyenv-virtualenv-prefix
|
||||
|
||||
unstub pyenv-version-name
|
||||
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
pyenv-virtualenv: version \`system' is not a virtualenv
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
148
test/virtualenv.bats
Normal file
148
test/virtualenv.bats
Normal file
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
setup() {
|
||||
export PYENV_ROOT="${TMP}/pyenv"
|
||||
}
|
||||
|
||||
stub_pyenv() {
|
||||
create_executable "${PYENV_VERSION}" "virtualenv"
|
||||
remove_executable "${PYENV_VERSION}" "pyvenv"
|
||||
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/${PYENV_VERSION}'"
|
||||
stub pyenv-hooks "virtualenv : echo"
|
||||
stub pyenv-rehash " : echo rehashed"
|
||||
}
|
||||
|
||||
unstub_pyenv() {
|
||||
unstub pyenv-prefix
|
||||
unstub pyenv-hooks
|
||||
unstub pyenv-rehash
|
||||
}
|
||||
|
||||
@test "create virtualenv from given version" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-exec "virtualenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
run pyenv-virtualenv "3.2.1" "venv"
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
|
||||
@test "create virtualenv from current version" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-version-name "echo \${PYENV_VERSION}"
|
||||
stub pyenv-exec "virtualenv ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
run pyenv-virtualenv venv
|
||||
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
PYENV_VERSION=3.2.1 virtualenv ${PYENV_ROOT}/versions/venv
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
|
||||
@test "create virtualenv with short options" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-version-name "echo \${PYENV_VERSION}"
|
||||
stub pyenv-exec "virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
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
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
|
||||
@test "create virtualenv with long options" {
|
||||
export PYENV_VERSION="3.2.1"
|
||||
stub_pyenv "${PYENV_VERSION}"
|
||||
stub pyenv-version-name "echo \${PYENV_VERSION}"
|
||||
stub pyenv-exec "virtualenv --verbose --python=python ${PYENV_ROOT}/versions/venv : echo PYENV_VERSION=\${PYENV_VERSION} \"\$@\""
|
||||
stub pyenv-exec "python -m ensurepip : false"
|
||||
stub pyenv-exec "python */ez_setup.py : true"
|
||||
stub pyenv-exec "python */get-pip.py : true"
|
||||
stub curl true
|
||||
stub curl true
|
||||
|
||||
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
|
||||
Installing setuptools from https://bootstrap.pypa.io/ez_setup.py...
|
||||
Installing pip from https://bootstrap.pypa.io/get-pip.py...
|
||||
rehashed
|
||||
OUT
|
||||
|
||||
unstub_pyenv
|
||||
unstub pyenv-version-name
|
||||
unstub pyenv-exec
|
||||
unstub curl
|
||||
}
|
||||
|
||||
@test "no whitespace allowed in virtualenv name" {
|
||||
run pyenv-virtualenv "3.2.1" "foo bar"
|
||||
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
pyenv-virtualenv: no whitespace allowed in virtualenv name.
|
||||
OUT
|
||||
}
|
||||
|
||||
@test "no tab allowed in virtualenv name" {
|
||||
run pyenv-virtualenv "3.2.1" "foo bar baz"
|
||||
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
pyenv-virtualenv: no whitespace allowed in virtualenv name.
|
||||
OUT
|
||||
}
|
||||
68
test/virtualenvs.bats
Normal file
68
test/virtualenvs.bats
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user