-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_mkvirtualenv.sh
More file actions
executable file
·220 lines (189 loc) · 7.4 KB
/
test_mkvirtualenv.sh
File metadata and controls
executable file
·220 lines (189 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# -*- mode: shell-script -*-
test_dir=$(cd $(dirname $0) && pwd)
source "$test_dir/setup.sh"
oneTimeSetUp() {
rm -rf "$WORKON_HOME"
mkdir -p "$WORKON_HOME"
load_wrappers
}
oneTimeTearDown() {
rm -rf "$WORKON_HOME"
}
setUp () {
echo
rm -f "$TMPDIR/catch_output"
}
test_create() {
mkvirtualenv "env1" >/dev/null 2>&1
assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
for hook in postactivate predeactivate postdeactivate
do
assertTrue "env1 $hook was not created" "[ -f $WORKON_HOME/env1/bin/$hook ]"
assertFalse "env1 $hook is executable" "[ -x $WORKON_HOME/env1/bin/$hook ]"
done
}
test_create_space_in_name() {
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
# and work with virtualenv on OSX, but error out on Linux.
mkvirtualenv " env with space" >/dev/null 2>&1
assertTrue "Environment directory was not created" "[ -d \"$WORKON_HOME/ env with space\" ]"
for hook in postactivate predeactivate postdeactivate
do
assertTrue "$hook was not created" "[ -f \"$WORKON_HOME/ env with space/bin/$hook\" ]"
assertFalse "$hook is executable" "[ -x \"$WORKON_HOME/ env with space/bin/$hook\" ]"
done
assertTrue virtualenvwrapper_verify_active_environment
env_name=$(basename "$VIRTUAL_ENV")
assertSame " env with space" "$env_name"
}
test_activates () {
mkvirtualenv "env2" >/dev/null 2>&1
assertTrue virtualenvwrapper_verify_active_environment
assertSame "env2" $(basename "$VIRTUAL_ENV")
}
test_hooks () {
echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
chmod +x "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL postmkvirtualenv >> $TMPDIR/catch_output" > "$WORKON_HOME/postmkvirtualenv"
mkvirtualenv "env3" >/dev/null 2>&1
output=$(cat "$TMPDIR/catch_output")
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
expected="GLOBAL premkvirtualenv $workon_home_as_pwd env3
GLOBAL postmkvirtualenv"
assertSame "$expected" "$output"
rm -f "$WORKON_HOME/premkvirtualenv"
rm -f "$WORKON_HOME/postmkvirtualenv"
deactivate
rmvirtualenv "env3" >/dev/null 2>&1
}
test_no_virtualenv () {
# Find "which" before we change the path
which=$(which which)
old_path="$PATH"
PATH="/bin:/usr/sbin:/sbin"
venv=$($which virtualenv 2>/dev/null)
if [ ! -z "$venv" ]
then
echo "FOUND \"$venv\" in PATH so skipping this test"
export PATH="$old_path"
return 0
fi
mkvirtualenv should_not_be_created >/dev/null 2>&1
RC=$?
# Restore the path before testing because
# the test script depends on commands in the
# path.
export PATH="$old_path"
assertSame "$RC" "1"
}
test_no_args () {
mkvirtualenv 2>/dev/null 1>&2
RC=$?
assertSame "2" "$RC"
}
test_no_workon_home () {
old_home="$WORKON_HOME"
export WORKON_HOME="$WORKON_HOME/not_there"
mkvirtualenv should_be_created >"$old_home/output" 2>&1
output=$(cat "$old_home/output")
assertTrue "Did not see expected message in \"$output\"" "cat \"$old_home/output\" | grep 'does not exist'"
assertTrue "Did not create environment" "[ -d \"$WORKON_HOME/should_be_created\" ]"
WORKON_HOME="$old_home"
}
test_mkvirtualenv_hooks_system_site_packages () {
# See issue #189
echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
chmod +x "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL postmkvirtualenv >> $TMPDIR/catch_output" > "$WORKON_HOME/postmkvirtualenv"
mkvirtualenv --system-site-packages "env189" >/dev/null 2>&1
output=$(cat "$TMPDIR/catch_output")
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
expected="GLOBAL premkvirtualenv $workon_home_as_pwd env189
GLOBAL postmkvirtualenv"
assertSame "$expected" "$output"
rm -f "$WORKON_HOME/premkvirtualenv"
rm -f "$WORKON_HOME/postmkvirtualenv"
deactivate
rmvirtualenv "env189" >/dev/null 2>&1
}
test_mkvirtualenv_args () {
# See issue #102
VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--without-pip"
# With the argument, verify that they are not copied.
mkvirtualenv "without_pip" >/dev/null 2>&1
local RC=$?
assertTrue "mkvirtualenv failed" "[ $RC -eq 0 ]"
contents="$(lssitepackages)"
assertFalse "found pip in site-packages: ${contents}" "echo $contents | grep -q pip"
rmvirtualenv "without_pip" >/dev/null 2>&1
unset VIRTUALENVWRAPPER_VIRTUALENV_ARGS
}
test_no_such_virtualenv () {
VIRTUALENVWRAPPER_VIRTUALENV=/path/to/missing/program
echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
chmod +x "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL postmkvirtualenv >> $TMPDIR/catch_output" > "$WORKON_HOME/postmkvirtualenv"
mkvirtualenv "env3" >/dev/null 2>&1
output=$(cat "$TMPDIR/catch_output" 2>/dev/null)
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
expected=""
assertSame "$expected" "$output"
rm -f "$WORKON_HOME/premkvirtualenv"
rm -f "$WORKON_HOME/postmkvirtualenv"
VIRTUALENVWRAPPER_VIRTUALENV=virtualenv
}
test_virtualenv_fails () {
# Test to reproduce the conditions in issue #76
# https://bitbucket.org/dhellmann/virtualenvwrapper/issue/76/
#
# Should not run the premkvirtualenv or postmkvirtualenv hooks
# because the environment is not created and even the
# premkvirtualenv hooks are run *after* the environment exists
# (but before it is activated).
export pre_test_dir=$(cd "$test_dir"; pwd)
VIRTUALENVWRAPPER_VIRTUALENV=false
echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
chmod +x "$WORKON_HOME/premkvirtualenv"
echo "echo GLOBAL postmkvirtualenv >> $TMPDIR/catch_output" > "$WORKON_HOME/postmkvirtualenv"
mkvirtualenv "env3" >/dev/null 2>&1
output=$(cat "$TMPDIR/catch_output" 2>/dev/null)
workon_home_as_pwd=$(cd $WORKON_HOME; pwd)
expected=""
assertSame "$expected" "$output"
rm -f "$WORKON_HOME/premkvirtualenv"
rm -f "$WORKON_HOME/postmkvirtualenv"
VIRTUALENVWRAPPER_VIRTUALENV=virtualenv
}
test_mkvirtualenv_python_not_sticky () {
typeset _save=$VIRTUALENVWRAPPER_VIRTUALENV
VIRTUALENVWRAPPER_VIRTUALENV=true
mkvirtualenv --python blah foo
assertSame "" "$interpreter"
VIRTUALENVWRAPPER_VIRTUALENV=$_save
}
test_mkvirtualenv_python_short_option () {
typeset _save=$VIRTUALENVWRAPPER_VIRTUALENV
VIRTUALENVWRAPPER_VIRTUALENV=echo
output="$(mkvirtualenv -p python foo)"
assertSame "--python=python foo" "$output"
VIRTUALENVWRAPPER_VIRTUALENV=$_save
}
test_mkvirtualenv_python_long_option () {
typeset _save=$VIRTUALENVWRAPPER_VIRTUALENV
VIRTUALENVWRAPPER_VIRTUALENV=echo
output="$(mkvirtualenv --python python foo)"
assertSame "--python=python foo" "$output"
VIRTUALENVWRAPPER_VIRTUALENV=$_save
}
test_mkvirtualenv_python_long_option_equal () {
typeset _save=$VIRTUALENVWRAPPER_VIRTUALENV
VIRTUALENVWRAPPER_VIRTUALENV=echo
output="$(mkvirtualenv --python=python foo)"
assertSame "--python=python foo" "$output"
VIRTUALENVWRAPPER_VIRTUALENV=$_save
}
. "$test_dir/shunit2"