Skip to content

Commit b7b9962

Browse files
committed
fix preactivate scripts; warn for existing scripts that need to be executable but are not
1 parent 885f773 commit b7b9962

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

.hgignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ trace.txt
77
docs
88
*.pyc
99
sphinx/web/templates/base.html
10+
tests/catch_output
1011

1112
syntax: re
1213
.DS_Store

tests/test.sh

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ test_virtualenvwrapper_initialize() {
4040
done
4141
}
4242

43+
test_virtualenvwrapper_run_hook() {
44+
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
45+
chmod +x "$WORKON_HOME/test_hook"
46+
virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
47+
output=$(cat "$test_dir/catch_output")
48+
expected="run"
49+
assertSame "$expected" "$output"
50+
}
51+
52+
test_virtualenvwrapper_run_hook_permissions() {
53+
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
54+
chmod -x "$WORKON_HOME/test_hook"
55+
virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
56+
output=$(cat "$test_dir/catch_output")
57+
expected=""
58+
assertSame "$expected" "$output"
59+
}
60+
4361
test_get_python_version() {
4462
expected=$(python -V 2>&1 | cut -f2 -d' ' | cut -f-2 -d.)
4563
actual=$(virtualenvwrapper_get_python_version)
@@ -125,15 +143,32 @@ test_workon () {
125143
assertSame "env1" $(basename "$VIRTUAL_ENV")
126144
}
127145

128-
test_postactivate_hook () {
129-
echo "echo ENV postactivate > $test_dir/catch_output" > "$WORKON_HOME/env1/bin/postactivate"
130-
146+
test_workon_activate_hooks () {
147+
for t in pre post
148+
do
149+
echo "echo GLOBAL ${t}activate >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/${t}activate"
150+
chmod +x "$WORKON_HOME/${t}activate"
151+
echo "echo ENV ${t}activate >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/env1/bin/${t}activate"
152+
chmod +x "$WORKON_HOME/env1/bin/${t}activate"
153+
done
154+
155+
rm "$test_dir/catch_output"
156+
131157
workon env1
132158

133159
output=$(cat "$test_dir/catch_output")
134-
assertSame "ENV postactivate" "$output"
160+
expected="GLOBAL preactivate
161+
ENV preactivate
162+
GLOBAL postactivate
163+
ENV postactivate"
164+
165+
assertSame "$expected" "$output"
135166

136-
rm -f "$WORKON_HOME/env1/bin/postactivate"
167+
for t in pre post
168+
do
169+
rm -f "$WORKON_HOME/env1/bin/${t}activate"
170+
rm -f "$WORKON_HOME/${t}activate"
171+
done
137172
}
138173

139174
test_deactivate () {
@@ -147,11 +182,11 @@ test_deactivate () {
147182
test_deactivate_hooks () {
148183
workon env1
149184

150-
echo "echo GLOBAL predeactivate >> $test_dir/catch_output" > "$WORKON_HOME/predeactivate"
151-
echo "echo GLOBAL postdeactivate >> $test_dir/catch_output" > "$WORKON_HOME/postdeactivate"
152-
153-
echo "echo ENV predeactivate >> $test_dir/catch_output" > "$WORKON_HOME/env1/bin/predeactivate"
154-
echo "echo ENV postdeactivate >> $test_dir/catch_output" > "$WORKON_HOME/env1/bin/postdeactivate"
185+
for t in pre post
186+
do
187+
echo "echo GLOBAL ${t}deactivate >> $test_dir/catch_output" > "$WORKON_HOME/${t}deactivate"
188+
echo "echo ENV ${t}deactivate >> $test_dir/catch_output" > "$WORKON_HOME/env1/bin/${t}deactivate"
189+
done
155190

156191
deactivate
157192

@@ -162,10 +197,11 @@ ENV postdeactivate
162197
GLOBAL postdeactivate"
163198
assertSame "$expected" "$output"
164199

165-
rm -f "$WORKON_HOME/env1/bin/predeactivate"
166-
rm -f "$WORKON_HOME/env1/bin/postdeactivate"
167-
rm -f "$WORKON_HOME/predeactivate"
168-
rm -f "$WORKON_HOME/postdeactivate"
200+
for t in pre post
201+
do
202+
rm -f "$WORKON_HOME/env1/bin/${t}activate"
203+
rm -f "$WORKON_HOME/${t}activate"
204+
done
169205
}
170206

171207
test_virtualenvwrapper_show_workon_options () {

virtualenvwrapper_bashrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function virtualenvwrapper_verify_active_environment () {
134134
# Run a hook script in the current shell
135135
function virtualenvwrapper_source_hook () {
136136
scriptname="$1"
137+
#echo "Looking for hook $scriptname"
137138
if [ -f "$scriptname" ]
138139
then
139140
source "$scriptname"
@@ -144,9 +145,13 @@ function virtualenvwrapper_source_hook () {
144145
function virtualenvwrapper_run_hook () {
145146
scriptname="$1"
146147
shift
148+
#echo "Looking for hook $scriptname"
147149
if [ -x "$scriptname" ]
148150
then
149151
"$scriptname" "$@"
152+
elif [ -e "$scriptname" ]
153+
then
154+
echo "Warning: Found \"$scriptname\" but it is not executable." 1>&2
150155
fi
151156
}
152157

@@ -237,6 +242,9 @@ function workon () {
237242
then
238243
deactivate
239244
fi
245+
246+
virtualenvwrapper_run_hook "$WORKON_HOME/preactivate"
247+
virtualenvwrapper_run_hook "$WORKON_HOME/$env_name/bin/preactivate"
240248

241249
source "$activate"
242250

@@ -263,7 +271,6 @@ function workon () {
263271
}'
264272

265273
virtualenvwrapper_source_hook "$WORKON_HOME/postactivate"
266-
267274
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"
268275

269276
return 0

0 commit comments

Comments
 (0)