Skip to content

Commit 753b0af

Browse files
committed
convert path deriving code in startup of script to function so it is easier to test
1 parent b4792af commit 753b0af

File tree

2 files changed

+80
-26
lines changed

2 files changed

+80
-26
lines changed

tests/test_derive_workon_home.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
3+
#set -x
4+
5+
test_dir=$(cd $(dirname $0) && pwd)
6+
7+
export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
8+
TMP_WORKON_HOME="$WORKON_HOME"
9+
10+
oneTimeSetUp() {
11+
rm -rf "$TMP_WORKON_HOME"
12+
mkdir -p "$TMP_WORKON_HOME"
13+
source "$test_dir/../virtualenvwrapper.sh"
14+
echo $PYTHONPATH
15+
}
16+
17+
oneTimeTearDown() {
18+
rm -rf "$TMP_WORKON_HOME"
19+
}
20+
21+
setUp () {
22+
echo
23+
rm -f "$test_dir/catch_output"
24+
WORKON_HOME="$TMP_WORKON_HOME"
25+
}
26+
27+
test_default() {
28+
unset WORKON_HOME
29+
assertSame "$HOME/.virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
30+
}
31+
32+
test_includes_relative_path() {
33+
WORKON_HOME="$WORKON_HOME/../$(basename $WORKON_HOME)"
34+
assertSame "$WORKON_HOME" "$(virtualenvwrapper_derive_workon_home)"
35+
}
36+
37+
test_begins_relative_path() {
38+
WORKON_HOME=".test-virtualenvs"
39+
assertSame "$HOME/.test-virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
40+
}
41+
42+
test_includes_tilde() {
43+
WORKON_HOME="~/.test-virtualenvs"
44+
assertSame "$HOME/.test-virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
45+
}
46+
47+
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,45 @@
4444
# 11. The virtual environment is activated.
4545
#
4646

47-
# Make sure there is a default value for WORKON_HOME.
48-
# You can override this setting in your .bashrc.
49-
if [ "$WORKON_HOME" = "" ]
50-
then
51-
export WORKON_HOME="$HOME/.virtualenvs"
52-
fi
53-
5447
# Locate the global Python where virtualenvwrapper is installed.
5548
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ]
5649
then
5750
VIRTUALENVWRAPPER_PYTHON="$(\which python)"
5851
fi
5952

60-
# If the path is relative, prefix it with $HOME
61-
# (note: for compatibility)
62-
if echo "$WORKON_HOME" | (unset GREP_OPTIONS; grep -e '^[^/~]' > /dev/null)
63-
then
64-
export WORKON_HOME="$HOME/$WORKON_HOME"
65-
fi
53+
virtualenvwrapper_derive_workon_home() {
54+
typeset workon_home_dir="$WORKON_HOME"
6655

67-
# Only call on Python to fix the path if it looks like the
68-
# path might contain stuff to expand.
69-
# (it might be possible to do this in shell, but I don't know a
70-
# cross-shell-safe way of doing it -wolever)
71-
if echo "$WORKON_HOME" | (unset GREP_OPTIONS; egrep -e "([\$~]|//)" >/dev/null)
72-
then
73-
# This will normalize the path by:
74-
# - Removing extra slashes (e.g., when TMPDIR ends in a slash)
75-
# - Expanding variables (e.g., $foo)
76-
# - Converting ~s to complete paths (e.g., ~/ to /home/brian/ and ~arthur to /home/arthur)
77-
WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\"))")
78-
export WORKON_HOME
79-
fi
56+
# Make sure there is a default value for WORKON_HOME.
57+
# You can override this setting in your .bashrc.
58+
if [ "$workon_home_dir" = "" ]
59+
then
60+
workon_home_dir="$HOME/.virtualenvs"
61+
fi
62+
63+
# If the path is relative, prefix it with $HOME
64+
# (note: for compatibility)
65+
if echo "$workon_home_dir" | (unset GREP_OPTIONS; grep -e '^[^/~]' > /dev/null)
66+
then
67+
workon_home_dir="$HOME/$WORKON_HOME"
68+
fi
69+
70+
# Only call on Python to fix the path if it looks like the
71+
# path might contain stuff to expand.
72+
# (it might be possible to do this in shell, but I don't know a
73+
# cross-shell-safe way of doing it -wolever)
74+
if echo "$workon_home_dir" | (unset GREP_OPTIONS; egrep -e "([\$~]|//)" >/dev/null)
75+
then
76+
# This will normalize the path by:
77+
# - Removing extra slashes (e.g., when TMPDIR ends in a slash)
78+
# - Expanding variables (e.g., $foo)
79+
# - Converting ~s to complete paths (e.g., ~/ to /home/brian/ and ~arthur to /home/arthur)
80+
workon_home_dir=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.expandvars(os.path.expanduser(\"$workon_home_dir\"))")
81+
fi
82+
83+
echo "$workon_home_dir"
84+
return 0
85+
}
8086

8187
# Verify that the WORKON_HOME directory exists
8288
virtualenvwrapper_verify_workon_home () {
@@ -131,6 +137,7 @@ virtualenvwrapper_run_hook () {
131137

132138
# Set up virtualenvwrapper properly
133139
virtualenvwrapper_initialize () {
140+
export WORKON_HOME=$(virtualenvwrapper_derive_workon_home)
134141
virtualenvwrapper_verify_workon_home -q || return 1
135142
virtualenvwrapper_run_hook "initialize"
136143
if [ $? -ne 0 ]

0 commit comments

Comments
 (0)