Skip to content

Commit 35e5407

Browse files
committed
improve handling for missing WORKON_HOME variable or directory; add test for #18 - can't reproduce
1 parent 72b282d commit 35e5407

File tree

6 files changed

+73
-19
lines changed

6 files changed

+73
-19
lines changed

docsource/history.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Release History
33
===============
44

5+
1.21
6+
7+
- Better protection of ``$WORKON_HOME`` does not exist when the wrapper script is sourced.
8+
59
1.20
610

711
- Incorporate lssitepackages feature from Sander Smits.

docsource/index.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,22 @@ Features
2222
Quick Setup
2323
===========
2424

25-
1. Add two lines to your .bashrc to set the location where the virtual environments should live and the location of the script installed with this package::
25+
1. Create a directory to hold all of the virtual environments. The default is
26+
``$HOME/.virtualenvs``.
27+
28+
2. Add two lines to your .bashrc to set the location where the virtual environments should
29+
live and the location of the script installed with this package::
2630

2731
export WORKON_HOME=$HOME/.virtualenvs
2832
source /usr/local/bin/virtualenvwrapper_bashrc
2933

30-
2. Run: ``source ~/.bashrc``
31-
3. Run: ``workon``
32-
4. A list of environments, empty, is printed.
33-
5. Run: ``mkvirtualenv temp``
34-
6. A new environment, ``temp`` is created and activated.
35-
7. Run: ``workon``
36-
8. This time, the ``temp`` environment is included.
34+
3. Run: ``source ~/.bashrc``
35+
4. Run: ``workon``
36+
5. A list of environments, empty, is printed.
37+
6. Run: ``mkvirtualenv temp``
38+
7. A new environment, ``temp`` is created and activated.
39+
8. Run: ``workon``
40+
9. This time, the ``temp`` environment is included.
3741

3842
=======
3943
Details

pavement.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,6 @@ def test_install(options):
194194
def test():
195195
sh('bash ./tests/test.sh')
196196
sh('SHUNIT_PARENT=./tests/test.sh zsh -o shwordsplit ./tests/test.sh')
197+
sh('bash ./tests/test_misconfigured.sh')
198+
sh('SHUNIT_PARENT=./tests/test_misconfigured.sh zsh -o shwordsplit ./tests/test.sh')
197199
return

tests/test.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ test_virtualenvwrapper_show_workon_options () {
135135
rm -f "$WORKON_HOME/link_env"
136136
}
137137

138+
test_virtualenvwrapper_show_workon_options_no_envs () {
139+
old_home="$WORKON_HOME"
140+
export WORKON_HOME=${TMPDIR:/tmp}/$$
141+
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
142+
assertSame "" "$envs"
143+
export WORKON_HOME="$old_home"
144+
}
145+
138146
test_rmvirtualenv () {
139147
mkvirtualenv "deleteme"
140148
assertTrue "[ -d $WORKON_HOME/deleteme ]"

tests/test_misconfigured.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(dirname $0)
6+
7+
export SCRATCH_DIR="${TMPDIR:-/tmp}/$$"
8+
9+
oneTimeSetUp() {
10+
rm -rf "$SCRATCH_DIR"
11+
mkdir -p "$SCRATCH_DIR"
12+
}
13+
14+
oneTimeTearDown() {
15+
rm -rf "$SCRATCH_DIR"
16+
}
17+
18+
test_workon_home_missing () {
19+
export WORKON_HOME="$SCRATCH_DIR/no_such_subdir"
20+
$SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
21+
RC=$?
22+
assertSame "1" "$RC"
23+
}
24+
25+
test_shell_expansion_fails () {
26+
export WORKON_HOME="$SCRATCH_DIR/\$no_such_var"
27+
$SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
28+
RC=$?
29+
assertSame "1" "$RC"
30+
}
31+
32+
. "$test_dir/shunit2"

virtualenvwrapper_bashrc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
#
77
# Setup:
88
#
9-
# 1. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
9+
# 1. Create a directory to hold the virtual environments.
10+
# (mkdir $HOME/.virtualenvs).
11+
# 2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
1012
# to your .bashrc.
11-
# 2. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
13+
# 3. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
1214
# to your .bashrc.
13-
# 3. Run: source ~/.bashrc
14-
# 4. Run: workon
15-
# 5. A list of environments, empty, is printed.
16-
# 6. Run: mkvirtualenv temp
17-
# 7. Run: workon
18-
# 8. This time, the "temp" environment is included.
19-
# 9. Run: workon temp
20-
# 10. The virtual environment is activated.
15+
# 4. Run: source ~/.bashrc
16+
# 5. Run: workon
17+
# 6. A list of environments, empty, is printed.
18+
# 7. Run: mkvirtualenv temp
19+
# 8. Run: workon
20+
# 9. This time, the "temp" environment is included.
21+
# 10. Run: workon temp
22+
# 11. The virtual environment is activated.
2123
#
2224

2325
# Make sure there is a default value for WORKON_HOME.
@@ -29,7 +31,7 @@ fi
2931

3032
# Normalize the directory name in case it includes
3133
# relative path components.
32-
WORKON_HOME=$(sh -c 'cd "$WORKON_HOME"; pwd')
34+
WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandshell(os.path.expanduser(\"$pydir\")))")
3335
export WORKON_HOME
3436

3537
# Verify that the WORKON_HOME directory exists
@@ -42,6 +44,8 @@ function virtualenvwrapper_verify_workon_home () {
4244
return 0
4345
}
4446

47+
virtualenvwrapper_verify_workon_home
48+
4549
# Verify that the requested environment exists
4650
function virtualenvwrapper_verify_workon_environment () {
4751
typeset env_name="$1"

0 commit comments

Comments
 (0)