Skip to content

Commit 3a195c6

Browse files
committed
associate project before enabling the new virtualenv; fixes #122
1 parent 80cf4cd commit 3a195c6

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

tests/test_mkvirtualenv_associate.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ oneTimeTearDown() {
2020
setUp () {
2121
echo
2222
rm -f "$test_dir/catch_output"
23+
echo "#!/bin/sh" > "$WORKON_HOME/preactivate"
24+
echo "#!/bin/sh" > "$WORKON_HOME/postactivate"
2325
}
2426

2527
test_associate() {
@@ -31,4 +33,40 @@ test_associate() {
3133
assertEquals "$ptrfile contains wrong content" "$project" "$(cat $ptrfile)"
3234
}
3335

36+
test_preactivate() {
37+
project="/dev/null"
38+
env="env2"
39+
ptrfile="$WORKON_HOME/$env/.project"
40+
cat - >"$WORKON_HOME/preactivate" <<EOF
41+
#!/bin/sh
42+
if [ -f "$ptrfile" ]
43+
then
44+
echo exists >> "$test_dir/catch_output"
45+
else
46+
echo noexists >> "$test_dir/catch_output"
47+
fi
48+
EOF
49+
chmod +x "$WORKON_HOME/preactivate"
50+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
51+
assertSame "preactivate did not find file" "exists" "$(cat $test_dir/catch_output)"
52+
}
53+
54+
test_postactivate() {
55+
project="/dev/null"
56+
env="env3"
57+
ptrfile="$WORKON_HOME/$env/.project"
58+
cat - >"$WORKON_HOME/postactivate" <<EOF
59+
#!/bin/sh
60+
if [ -f "$ptrfile" ]
61+
then
62+
echo exists >> "$test_dir/catch_output"
63+
else
64+
echo noexists >> "$test_dir/catch_output"
65+
fi
66+
EOF
67+
chmod +x "$WORKON_HOME/postactivate"
68+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
69+
assertSame "postactivate did not find file" "exists" "$(cat $test_dir/catch_output)"
70+
}
71+
3472
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,14 @@ function mkvirtualenv {
373373
# the environment won't exist. Use that to tell whether
374374
# we should switch to the environment and run the hook.
375375
[ ! -d "$WORKON_HOME/$envname" ] && return 0
376+
377+
# If they gave us a project directory, set it up now
378+
# so the activate hooks can find it.
379+
if [ ! -z "$project" ]
380+
then
381+
setvirtualenvproject "$WORKON_HOME/$envname" "$project"
382+
fi
383+
376384
# Now activate the new environment
377385
workon "$envname"
378386

@@ -386,11 +394,6 @@ function mkvirtualenv {
386394
pip install $a
387395
done
388396

389-
if [ ! -z "$project" ]
390-
then
391-
setvirtualenvproject "" "$project"
392-
fi
393-
394397
virtualenvwrapper_run_hook "post_mkvirtualenv"
395398
}
396399

0 commit comments

Comments
 (0)