Skip to content

Commit 50208a0

Browse files
committed
all existing tests are passing again
1 parent 6674ea3 commit 50208a0

File tree

7 files changed

+138
-40
lines changed

7 files changed

+138
-40
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ docs/sphinx/web/templates/base.html: ~/Devel/doughellmann/doughellmann/templates
3434
# Testing
3535
TEST_SCRIPTS=$(wildcard tests/test*.sh)
3636

37-
.PHONY: test test-bash test-sh test-zsh test-loop test-install
38-
test: test-bash test-sh test-zsh test-install
37+
.PHONY: develop test test-bash test-sh test-zsh test-loop test-install
38+
test: develop test-bash test-sh test-zsh test-install
39+
40+
develop:
41+
python setup.py develop
3942

4043
test-bash:
4144
TEST_SHELL=bash $(MAKE) test-loop

setup.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,41 @@ def find_package_data(
151151

152152
entry_points = {
153153
#'console_scripts': [ 'venvw_hook = virtualenvwrapper.hook_loader:main' ],
154+
'virtualenvwrapper.initialize': [
155+
'make_hooks = virtualenvwrapper.make_hooks:initialize',
156+
],
154157
'virtualenvwrapper.initialize_source': [
155158
'user_scripts = virtualenvwrapper.user_scripts:initialize_source',
156159
],
160+
157161
'virtualenvwrapper.pre_mkvirtualenv': [
158162
'user_scripts = virtualenvwrapper.user_scripts:pre_mkvirtualenv',
159163
'make_hooks = virtualenvwrapper.make_hooks:pre_mkvirtualenv',
160164
],
161165
'virtualenvwrapper.post_mkvirtualenv_source': [
162166
'user_scripts = virtualenvwrapper.user_scripts:post_mkvirtualenv_source',
163167
],
164-
'virtualenvwrapper.initialize': [
165-
'make_hooks = virtualenvwrapper.make_hooks:initialize',
166-
]
168+
169+
'virtualenvwrapper.pre_rmvirtualenv': [
170+
'user_scripts = virtualenvwrapper.user_scripts:pre_rmvirtualenv',
171+
],
172+
'virtualenvwrapper.post_rmvirtualenv': [
173+
'user_scripts = virtualenvwrapper.user_scripts:post_rmvirtualenv',
174+
],
175+
176+
'virtualenvwrapper.pre_activate': [
177+
'user_scripts = virtualenvwrapper.user_scripts:pre_activate',
178+
],
179+
'virtualenvwrapper.post_activate_source': [
180+
'user_scripts = virtualenvwrapper.user_scripts:post_activate_source',
181+
],
182+
183+
'virtualenvwrapper.pre_deactivate_source': [
184+
'user_scripts = virtualenvwrapper.user_scripts:pre_deactivate_source',
185+
],
186+
'virtualenvwrapper.post_deactivate_source': [
187+
'user_scripts = virtualenvwrapper.user_scripts:post_deactivate_source',
188+
],
167189
},
168190

169191
zip_safe=False,

tests/test_run_hook.sh

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#set -x
44

5-
test_dir=$(dirname $0)
6-
source "$test_dir/../virtualenvwrapper.sh"
7-
85
export WORKON_HOME="${TMPDIR:-/tmp}/WORKON_HOME"
96

7+
test_dir=$(dirname $0)
8+
109
oneTimeSetUp() {
1110
rm -rf "$WORKON_HOME"
1211
mkdir -p "$WORKON_HOME"
12+
source "$test_dir/../virtualenvwrapper.sh"
1313
}
1414

1515
oneTimeTearDown() {
@@ -19,21 +19,33 @@ oneTimeTearDown() {
1919
setUp () {
2020
echo
2121
rm -f "$test_dir/catch_output"
22+
rm -f "$WORKON_HOME/initialize"
23+
rm -f "$WORKON_HOME/prermvirtualenv"
2224
}
2325

2426
test_virtualenvwrapper_run_hook() {
25-
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
26-
chmod +x "$WORKON_HOME/test_hook"
27-
virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
27+
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
28+
chmod +x "$WORKON_HOME/initialize"
29+
virtualenvwrapper_run_hook "initialize"
30+
output=$(cat "$test_dir/catch_output")
31+
expected="run"
32+
assertSame "$expected" "$output"
33+
}
34+
35+
test_virtualenvwrapper_source_hook_permissions() {
36+
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/initialize"
37+
chmod -x "$WORKON_HOME/initialize"
38+
virtualenvwrapper_run_hook "initialize"
2839
output=$(cat "$test_dir/catch_output")
2940
expected="run"
3041
assertSame "$expected" "$output"
3142
}
3243

3344
test_virtualenvwrapper_run_hook_permissions() {
34-
echo "echo run >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/test_hook"
35-
chmod -x "$WORKON_HOME/test_hook"
36-
virtualenvwrapper_run_hook "$WORKON_HOME/test_hook"
45+
echo "echo run $@ >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/prermvirtualenv"
46+
chmod -x "$WORKON_HOME/prermvirtualenv"
47+
touch "$test_dir/catch_output"
48+
virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo"
3749
output=$(cat "$test_dir/catch_output")
3850
expected=""
3951
assertSame "$expected" "$output"

tests/test_workon.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ test_workon_activate_hooks () {
3737
chmod +x "$WORKON_HOME/env1/bin/${t}activate"
3838
done
3939

40-
rm "$test_dir/catch_output"
40+
rm -f "$test_dir/catch_output"
41+
touch "$test_dir/catch_output"
4142

4243
workon env1
4344

@@ -73,6 +74,8 @@ test_deactivate_hooks () {
7374
echo "echo ENV ${t}deactivate >> $test_dir/catch_output" > "$WORKON_HOME/env1/bin/${t}deactivate"
7475
done
7576

77+
touch "$test_dir/catch_output"
78+
7679
deactivate
7780

7881
output=$(cat "$test_dir/catch_output")

virtualenvwrapper.sh

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,23 @@ function virtualenvwrapper_verify_workon_home () {
7171

7272
HOOK_VERBOSE_OPTION="-v"
7373

74-
# Run a hook script in the current shell
75-
function virtualenvwrapper_source_hook () {
74+
# Run the hooks
75+
function virtualenvwrapper_run_hook () {
76+
# First anything that runs directly from the plugin
77+
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
78+
# Now anything that wants to run inside this shell
79+
hook_name="$1"
80+
shift # get rid of hook name
7681
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION \
77-
--source "${1}_source" >>$TMPDIR/$$.hook
82+
--source "${hook_name}_source" "$@" >>$TMPDIR/$$.hook
7883
source $TMPDIR/$$.hook
7984
rm -f $TMPDIR/$$.hook
8085
}
8186

82-
# Run a hook script in its own shell
83-
function virtualenvwrapper_run_hook () {
84-
"$VIRTUALENVWRAPPER_PYTHON" -m virtualenvwrapper.hook_loader $HOOK_VERBOSE_OPTION "$@"
85-
}
86-
8787
# Set up virtualenvwrapper properly
8888
function virtualenvwrapper_initialize () {
8989
virtualenvwrapper_verify_workon_home -q || return 1
90-
virtualenvwrapper_run_hook initialize
91-
virtualenvwrapper_source_hook initialize
90+
virtualenvwrapper_run_hook "initialize"
9291
}
9392

9493
virtualenvwrapper_initialize
@@ -150,7 +149,6 @@ function mkvirtualenv () {
150149
# Now activate the new environment
151150
workon "$envname"
152151
virtualenvwrapper_run_hook "post_mkvirtualenv"
153-
virtualenvwrapper_source_hook "post_mkvirtualenv"
154152
}
155153

156154
# Remove an environment, in the WORKON_HOME.
@@ -169,9 +167,9 @@ function rmvirtualenv () {
169167
echo "Either switch to another environment, or run 'deactivate'." >&2
170168
return 1
171169
fi
172-
virtualenvwrapper_run_hook "$WORKON_HOME/prermvirtualenv" "$env_dir"
170+
virtualenvwrapper_run_hook "pre_rmvirtualenv" "$env_name"
173171
rm -rf "$env_dir"
174-
virtualenvwrapper_run_hook "$WORKON_HOME/postrmvirtualenv" "$env_dir"
172+
virtualenvwrapper_run_hook "post_rmvirtualenv" "$env_name"
175173
}
176174

177175
# List the available environments.
@@ -215,8 +213,7 @@ function workon () {
215213
deactivate
216214
fi
217215

218-
virtualenvwrapper_run_hook "$WORKON_HOME/preactivate"
219-
virtualenvwrapper_run_hook "$WORKON_HOME/$env_name/bin/preactivate"
216+
virtualenvwrapper_run_hook "pre_activate" "$env_name"
220217

221218
source "$activate"
222219

@@ -227,23 +224,21 @@ function workon () {
227224
eval 'function deactivate () {
228225
# Call the local hook before the global so we can undo
229226
# any settings made by the local postactivate first.
230-
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
231-
virtualenvwrapper_source_hook "$WORKON_HOME/predeactivate"
227+
virtualenvwrapper_run_hook "pre_deactivate"
232228
233229
env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate"
230+
old_env=$(basename "$VIRTUAL_ENV")
234231
235232
# Restore the original definition of deactivate
236233
eval "$virtualenvwrapper_saved_deactivate"
237234
238235
# Instead of recursing, this calls the now restored original function.
239236
deactivate
240237
241-
virtualenvwrapper_source_hook "$env_postdeactivate_hook"
242-
virtualenvwrapper_source_hook "$WORKON_HOME/postdeactivate"
238+
virtualenvwrapper_run_hook "post_deactivate" "$old_env"
243239
}'
244240

245-
virtualenvwrapper_source_hook "$WORKON_HOME/postactivate"
246-
virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/postactivate"
241+
virtualenvwrapper_run_hook "post_activate"
247242

248243
return 0
249244
}

virtualenvwrapper/hook_loader.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def main():
5656
console.setFormatter(formatter)
5757
logging.getLogger('').addHandler(console)
5858

59+
logging.getLogger(__name__).debug('cli args %s', args)
60+
5961
# Determine which hook we're running
6062
if not args:
6163
parser.error('Please specify the hook to run')

virtualenvwrapper/user_scripts.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,88 @@ def run_script(script_path, *args):
3232
subprocess.call([script_path] + list(args), shell=True)
3333
return
3434

35+
def run_global(script_name, *args):
36+
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', script_name))
37+
run_script(script_path, *args)
38+
return
39+
40+
def run_local_from_arg(script_name, *args):
41+
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', args[0], 'bin', script_name))
42+
run_script(script_path, *args)
43+
return
44+
45+
def run_local_from_env(script_name, *args):
46+
script_path = os.path.expandvars(os.path.join('$VIRTUAL_ENV', 'bin', script_name))
47+
run_script(script_path, *args)
48+
return
49+
50+
3551
# HOOKS
3652

3753
def initialize_source(args):
3854
return """
3955
#
40-
# Run user-provided initialization scripts
56+
# Run user-provided scripts
4157
#
4258
[ -f "$WORKON_HOME/initialize" ] && source "$WORKON_HOME/initialize"
4359
"""
4460

4561
def pre_mkvirtualenv(args):
4662
log.debug('pre_mkvirtualenv')
47-
script_path = os.path.expandvars(os.path.join('$WORKON_HOME', 'premkvirtualenv'))
48-
run_script(script_path, *args)
63+
run_global('premkvirtualenv')
4964
return
5065

5166
def post_mkvirtualenv_source(args):
5267
return """
5368
#
54-
# Run user-provided mkvirtualenv scripts
69+
# Run user-provided scripts
5570
#
5671
[ -f "$WORKON_HOME/postmkvirtualenv" ] && source "$WORKON_HOME/postmkvirtualenv"
5772
[ -f "$VIRTUAL_ENV/bin/postmkvirtualenv" ] && source "$VIRTUAL_ENV/bin/postmkvirtualenv"
5873
"""
74+
75+
def pre_rmvirtualenv(args):
76+
log.debug('pre_rmvirtualenv')
77+
run_global('prermvirtualenv', *args)
78+
return
79+
80+
def post_rmvirtualenv(args):
81+
log.debug('post_rmvirtualenv')
82+
run_global('postrmvirtualenv', *args)
83+
return
84+
85+
def pre_activate(args):
86+
log.debug('pre_activate')
87+
run_global('preactivate', *args)
88+
run_local_from_arg('preactivate', *args)
89+
return
90+
91+
def post_activate_source(args):
92+
log.debug('post_activate')
93+
return """
94+
#
95+
# Run user-provided scripts
96+
#
97+
[ -f "$WORKON_HOME/postactivate" ] && source "$WORKON_HOME/postactivate"
98+
[ -f "$VIRTUAL_ENV/bin/postactivate" ] && source "$VIRTUAL_ENV/bin/postactivate"
99+
"""
100+
101+
def pre_deactivate_source(args):
102+
log.debug('pre_deactivate')
103+
return """
104+
#
105+
# Run user-provided scripts
106+
#
107+
[ -f "$VIRTUAL_ENV/bin/predeactivate" ] && source "$VIRTUAL_ENV/bin/predeactivate"
108+
[ -f "$WORKON_HOME/predeactivate" ] && source "$WORKON_HOME/predeactivate"
109+
"""
110+
111+
def post_deactivate_source(args):
112+
log.debug('post_deactivate')
113+
return """
114+
#
115+
# Run user-provided scripts
116+
#
117+
[ -f "$WORKON_HOME/%(env_name)s/bin/postdeactivate" ] && source "$WORKON_HOME/%(env_name)s/bin/postdeactivate"
118+
[ -f "$WORKON_HOME/postdeactivate" ] && source "$WORKON_HOME/postdeactivate"
119+
""" % { 'env_name':args[0] }

0 commit comments

Comments
 (0)