Skip to content

Commit eeb7efe

Browse files
committed
add a test to verify that when virtualenv fails to create an environment the hook scripts are not run. see #76
1 parent fe1639e commit eeb7efe

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

ChangeLog

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
2011-01-24 Doug Hellmann <[email protected]>
2+
3+
* virtualenvwrapper.sh: Replace hard-coded name "virtualenv" with
4+
the variable VIRTUALENVWRAPPER_VIRTUALENV to allow tests (and
5+
users) to override it.
6+
7+
* tests/test_mkvirtualenv.sh (test_virtualenv_fails): Add a test
8+
to reproduce the conditions reported in issue #76.
9+
110
2010-12-27 Doug Hellmann <[email protected]>
211

312
* virtualenvwrapper.sh (virtualenvwrapper_get_python_version):

docs/en/history.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Dev
77
- Merged in `Japanese translation of the documentation
88
<http://www.doughellmann.com/docs/virtualenvwrapper/ja/>`__ from
99
Tetsuya Morimoto.
10+
- Incorporate a suggestion from Ales Zoulek to let the user specify
11+
the virtualenv binary through an environment variable
12+
(``VIRTUALENVWRAPPER_VIRTUALENV``).
1013

1114
2.6.1
1215

tests/test_mkvirtualenv.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ test_no_workon_home () {
8484
WORKON_HOME="$old_home"
8585
}
8686

87+
test_virtualenv_fails () {
88+
# Test to reproduce the conditions in issue #76
89+
# https://bitbucket.org/dhellmann/virtualenvwrapper/issue/76/
90+
#
91+
# Should not run the premkvirtualenv or postmkvirtualenv hooks
92+
# because the environment is not created and even the
93+
# premkvirtualenv hooks are run *after* the environment exists
94+
# (but before it is activated).
95+
export pre_test_dir=$(cd "$test_dir"; pwd)
96+
97+
VIRTUALENVWRAPPER_VIRTUALENV=false
98+
99+
echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
100+
echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
101+
chmod +x "$WORKON_HOME/premkvirtualenv"
102+
103+
echo "echo GLOBAL postmkvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postmkvirtualenv"
104+
mkvirtualenv "env3"
105+
output=$(cat "$test_dir/catch_output" 2>/dev/null)
106+
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
107+
expected=""
108+
assertSame "$expected" "$output"
109+
rm -f "$WORKON_HOME/premkvirtualenv"
110+
rm -f "$WORKON_HOME/postmkvirtualenv"
111+
112+
VIRTUALENVWRAPPER_VIRTUALENV=virtualenv
113+
}
114+
87115
# test_mkvirtualenv_sitepackages () {
88116
# # Without the option verify that site-packages are copied.
89117
# mkvirtualenv "env3"

virtualenvwrapper.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ then
5050
VIRTUALENVWRAPPER_PYTHON="$(\which python)"
5151
fi
5252

53+
# Set the name of the virtualenv app to use.
54+
if [ "$VIRTUALENVWRAPPER_VIRTUALENV" = "" ]
55+
then
56+
VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
57+
fi
58+
5359
virtualenvwrapper_derive_workon_home() {
5460
typeset workon_home_dir="$WORKON_HOME"
5561

@@ -149,15 +155,15 @@ virtualenvwrapper_initialize () {
149155

150156
# Verify that virtualenv is installed and visible
151157
virtualenvwrapper_verify_virtualenv () {
152-
typeset venv=$(\which virtualenv | (unset GREP_OPTIONS; \grep -v "not found"))
158+
typeset venv=$(\which "$VIRTUALENVWRAPPER_VIRTUALENV" | (unset GREP_OPTIONS; \grep -v "not found"))
153159
if [ "$venv" = "" ]
154160
then
155-
echo "ERROR: virtualenvwrapper could not find virtualenv in your path" >&2
161+
echo "ERROR: virtualenvwrapper could not find $VIRTUALENVWRAPPER_VIRTUALENV in your path" >&2
156162
return 1
157163
fi
158164
if [ ! -e "$venv" ]
159165
then
160-
echo "ERROR: Found virtualenv in path as \"$venv\" but that does not exist" >&2
166+
echo "ERROR: Found $VIRTUALENVWRAPPER_VIRTUALENV in path as \"$venv\" but that does not exist" >&2
161167
return 1
162168
fi
163169
return 0
@@ -194,11 +200,13 @@ mkvirtualenv () {
194200
virtualenvwrapper_verify_workon_home || return 1
195201
virtualenvwrapper_verify_virtualenv || return 1
196202
(cd "$WORKON_HOME" &&
197-
virtualenv "$@" &&
198-
[ -d "$WORKON_HOME/$envname" ] && virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
203+
"$VIRTUALENVWRAPPER_VIRTUALENV" "$@" &&
204+
[ -d "$WORKON_HOME/$envname" ] && \
205+
virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
199206
)
200207
typeset RC=$?
201208
[ $RC -ne 0 ] && return $RC
209+
202210
# If they passed a help option or got an error from virtualenv,
203211
# the environment won't exist. Use that to tell whether
204212
# we should switch to the environment and run the hook.

0 commit comments

Comments
 (0)