Skip to content

Commit 4c8de91

Browse files
committed
error handling in mkvirtualenv
1 parent 0e1946a commit 4c8de91

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ For more details, refer to the column published in the May 2008 issue of Python
130130
Updates
131131
=======
132132

133+
upcoming
134+
- Better error handling in mkvirtualenv.
135+
133136
1.14
134137
- Wrap the virtualenv version of deactivate() with one that lets us invoke
135138
the predeactivate hooks.

tests/test.sh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ setUp () {
2323

2424
test_mkvirtualenv() {
2525
mkvirtualenv "env1"
26-
assertTrue "[ -d $WORKON_HOME/env1 ]"
27-
assertSame "env1" $(basename "$VIRTUAL_ENV")
26+
assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
2827
}
2928

3029
test_cdvirtual() {
@@ -38,20 +37,42 @@ test_cdsitepackages () {
3837
pushd "$(pwd)" >/dev/null
3938
cdsitepackages
4039
pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
41-
assertSame "$VIRTUAL_ENV/lib/python${pyvers}/site-packages" "$(pwd)"
40+
sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
41+
assertSame "$sitepackages" "$(pwd)"
4242
popd >/dev/null
4343
}
4444

45-
test_mkvirtualenv_switches () {
45+
test_mkvirtualenv_activates () {
4646
mkvirtualenv "env2"
4747
assertTrue virtualenvwrapper_verify_active_environment
4848
assertSame "env2" $(basename "$VIRTUAL_ENV")
4949
}
5050

51+
# test_mkvirtualenv_sitepackages () {
52+
# # Without the option verify that site-packages are copied.
53+
# mkvirtualenv "env3"
54+
# assertSame "env3" "$(basename $VIRTUAL_ENV)"
55+
# pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
56+
# sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
57+
# #cat "$sitepackages/easy-install.pth"
58+
# assertTrue "Do not have expected virtualenv.py" "[ -f $sitepackages/virtualenv.py ]"
59+
# rmvirtualenv "env3"
60+
#
61+
# # With the argument, verify that they are not copied.
62+
# mkvirtualenv --no-site-packages "env4"
63+
# assertSame "env4" $(basename "$VIRTUAL_ENV")
64+
# pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
65+
# sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
66+
# assertTrue "[ -f $sitepackages/setuptools.pth ]"
67+
# assertTrue "[ -f $sitepackages/easy-install.pth ]"
68+
# assertFalse "Have virtualenv.py but should not" "[ -f $sitepackages/virtualenv.py ]"
69+
# rmvirtualenv "env4"
70+
# }
71+
5172
test_workon () {
5273
workon env1
5374
assertTrue virtualenvwrapper_verify_active_environment
54-
assertSame "env1" $(basename "$VIRTUAL_ENV")
75+
assertSame "env1" $(basename "$VIRTUAL_ENV")
5576
}
5677

5778
test_postactivate_hook () {

virtualenvwrapper_bashrc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ fi
3232
WORKON_HOME=$(cd "$WORKON_HOME"; pwd)
3333
export WORKON_HOME
3434

35-
VIRTUALENV_WRAPPER_BIN=$(dirname "$0")
36-
if [ "$VIRTUALENV_WRAPPER_BIN" = "." ]
37-
then
38-
VIRTUALENV_WRAPPER_BIN=$(pwd)
39-
fi
40-
4135
# Verify that the WORKON_HOME directory exists
4236
function virtualenvwrapper_verify_workon_home () {
4337
if [ ! -d "$WORKON_HOME" ]
@@ -94,10 +88,14 @@ function virtualenvwrapper_run_hook () {
9488
function mkvirtualenv () {
9589
eval "envname=\$$#"
9690
virtualenvwrapper_verify_workon_home || return 1
97-
(cd "$WORKON_HOME";
98-
virtualenv "$@";
91+
(cd "$WORKON_HOME" &&
92+
virtualenv "$@" &&
9993
virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
10094
)
95+
# If they passed a help option or got an error from virtualenv,
96+
# the environment won't exist. Use that to tell whether
97+
# we should switch to the environment and run the hook.
98+
[ ! -d "$WORKON_HOME/$envname" ] && return 0
10199
workon "$envname"
102100
virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
103101
}
@@ -128,7 +126,9 @@ function virtualenvwrapper_show_workon_options () {
128126
virtualenvwrapper_verify_workon_home || return 1
129127
# NOTE: DO NOT use ls here because colorized versions spew control characters
130128
# into the output list.
131-
(cd "$WORKON_HOME"; find -L . -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
129+
# echo seems a little faster than find, even with -depth 3.
130+
(cd "$WORKON_HOME"; for f in */bin/activate; do echo $f; done) | sed 's|^\./||' | sed 's|/bin/activate||' | sort
131+
# (cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
132132
}
133133

134134
# List or change working virtual environments

0 commit comments

Comments
 (0)