Skip to content

Commit 5ff4739

Browse files
committed
verify that virtualenv is installed; correct use of python to fix the WORKON_HOME value; more tests
1 parent 35e5407 commit 5ff4739

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

pavement.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,4 @@ def test():
195195
sh('bash ./tests/test.sh')
196196
sh('SHUNIT_PARENT=./tests/test.sh zsh -o shwordsplit ./tests/test.sh')
197197
sh('bash ./tests/test_misconfigured.sh')
198-
sh('SHUNIT_PARENT=./tests/test_misconfigured.sh zsh -o shwordsplit ./tests/test.sh')
199198
return

tests/test.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ test_mkvirtualenv_activates () {
5656
assertSame "env2" $(basename "$VIRTUAL_ENV")
5757
}
5858

59+
test_no_virtualenv () {
60+
old_path="$PATH"
61+
PATH="/usr/bin:/usr/local/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$HOME/bin"
62+
mkvirtualenv should_not_be_created # 2>/dev/null
63+
RC=$?
64+
# Restore the path before testing because
65+
# the test script depends on commands in the
66+
# path.
67+
export PATH="$old_path"
68+
assertSame "$RC" "1"
69+
}
70+
5971
# test_mkvirtualenv_sitepackages () {
6072
# # Without the option verify that site-packages are copied.
6173
# mkvirtualenv "env3"
@@ -137,8 +149,8 @@ test_virtualenvwrapper_show_workon_options () {
137149

138150
test_virtualenvwrapper_show_workon_options_no_envs () {
139151
old_home="$WORKON_HOME"
140-
export WORKON_HOME=${TMPDIR:/tmp}/$$
141-
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
152+
export WORKON_HOME=${TMPDIR:-/tmp}/$$
153+
envs=$(virtualenvwrapper_show_workon_options 2>/dev/null | tr '\n' ' ')
142154
assertSame "" "$envs"
143155
export WORKON_HOME="$old_home"
144156
}

tests/test_misconfigured.sh

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@ oneTimeTearDown() {
1515
rm -rf "$SCRATCH_DIR"
1616
}
1717

18-
test_workon_home_missing () {
18+
test_workon_home_missing_bash () {
1919
export WORKON_HOME="$SCRATCH_DIR/no_such_subdir"
20-
$SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
20+
bash -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
2121
RC=$?
2222
assertSame "1" "$RC"
2323
}
2424

25-
test_shell_expansion_fails () {
25+
test_workon_home_missing_zsh () {
26+
export WORKON_HOME="$SCRATCH_DIR/no_such_subdir"
27+
zsh -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
28+
RC=$?
29+
assertSame "1" "$RC"
30+
}
31+
32+
test_shell_expansion_fails_bash () {
33+
export WORKON_HOME="$SCRATCH_DIR/\$no_such_var"
34+
bash -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
35+
RC=$?
36+
assertSame "1" "$RC"
37+
}
38+
39+
test_shell_expansion_fails_zsh () {
2640
export WORKON_HOME="$SCRATCH_DIR/\$no_such_var"
27-
$SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
41+
zsh -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
2842
RC=$?
2943
assertSame "1" "$RC"
3044
}

virtualenvwrapper_bashrc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fi
3131

3232
# Normalize the directory name in case it includes
3333
# relative path components.
34-
WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandshell(os.path.expanduser(\"$pydir\")))")
34+
WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\")))")
3535
export WORKON_HOME
3636

3737
# Verify that the WORKON_HOME directory exists
@@ -46,6 +46,22 @@ function virtualenvwrapper_verify_workon_home () {
4646

4747
virtualenvwrapper_verify_workon_home
4848

49+
# Verify that virtualenv is installed and visible
50+
function virtualenvwrapper_verify_virtualenv () {
51+
venv=$(which virtualenv | grep -v "not found")
52+
if [ "$venv" = "" ]
53+
then
54+
echo "ERROR: virtualenvwrapper could not find virtualenv in your path" >&2
55+
return 1
56+
fi
57+
if [ ! -e "$venv" ]
58+
then
59+
echo "ERROR: Found virtualenv in path as \"$venv\" but that does not exist" >&2
60+
return 1
61+
fi
62+
return 0
63+
}
64+
4965
# Verify that the requested environment exists
5066
function virtualenvwrapper_verify_workon_environment () {
5167
typeset env_name="$1"
@@ -94,6 +110,7 @@ function virtualenvwrapper_run_hook () {
94110
function mkvirtualenv () {
95111
eval "envname=\$$#"
96112
virtualenvwrapper_verify_workon_home || return 1
113+
virtualenvwrapper_verify_virtualenv || return 1
97114
(cd "$WORKON_HOME" &&
98115
virtualenv "$@" &&
99116
virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
@@ -133,7 +150,7 @@ function virtualenvwrapper_show_workon_options () {
133150
# NOTE: DO NOT use ls here because colorized versions spew control characters
134151
# into the output list.
135152
# echo seems a little faster than find, even with -depth 3.
136-
(cd "$WORKON_HOME"; for f in */bin/activate; do echo $f; done) | sed 's|^\./||' | sed 's|/bin/activate||' | sort
153+
(cd "$WORKON_HOME"; for f in */bin/activate; do echo $f; done) 2>/dev/null | sed 's|^\./||' | sed 's|/bin/activate||' | sort
137154
# (cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
138155
}
139156

0 commit comments

Comments
 (0)