Skip to content

Commit f2602e8

Browse files
committed
Merged in jessamynsmith/virtualenvwrapper/env_with_space (pull request #28)
Lots of work on handling spaces in names
2 parents 5a440b0 + 0eb367c commit f2602e8

File tree

8 files changed

+183
-52
lines changed

8 files changed

+183
-52
lines changed

tests/test_allvirtualenv.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ oneTimeSetUp() {
88
mkdir -p "$WORKON_HOME"
99
unset VIRTUAL_ENV
1010
source "$test_dir/../virtualenvwrapper.sh"
11+
# These three env names must sort the same whether the OS considers leading whitespace or not.
12+
# "test" is after " env" and after "env", so the asserts work on OSX and Linux.
1113
mkvirtualenv test1 >/dev/null 2>&1
1214
mkvirtualenv test2 >/dev/null 2>&1
15+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
16+
# and work with virtualenv on OSX, but error out on Linux.
17+
mkvirtualenv " env with space" >/dev/null 2>&1
1318
deactivate
1419
}
1520

@@ -28,6 +33,7 @@ tearDown () {
2833
test_allvirtualenv_all() {
2934
assertTrue "Did not find test1" "allvirtualenv pwd | grep -q 'test1$'"
3035
assertTrue "Did not find test2" "allvirtualenv pwd | grep -q 'test2$'"
36+
assertTrue "Did not find ' env with space'" "allvirtualenv pwd | grep -q ' env with space'"
3137
}
3238

3339
test_allvirtualenv_spaces() {

tests/test_cd_space_in_name.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- mode: shell-script -*-
2+
3+
test_dir=$(cd $(dirname $0) && pwd)
4+
source "$test_dir/setup.sh"
5+
6+
oneTimeSetUp() {
7+
export WORKON_HOME="$WORKON_HOME/ this has spaces"
8+
rm -rf "$WORKON_HOME"
9+
mkdir -p "$WORKON_HOME"
10+
unset VIRTUAL_ENV
11+
source "$test_dir/../virtualenvwrapper.sh"
12+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
13+
# and work with virtualenv on OSX, but error out on Linux.
14+
mkvirtualenv " env with space" >/dev/null 2>&1
15+
deactivate
16+
}
17+
18+
oneTimeTearDown() {
19+
rm -rf "$WORKON_HOME"
20+
}
21+
22+
setUp () {
23+
echo
24+
}
25+
26+
tearDown () {
27+
deactivate >/dev/null 2>&1
28+
}
29+
30+
cd () {
31+
fail "Should not be using override cd function"
32+
}
33+
34+
test_cdvirtual_space_in_workon_home_space_in_name() {
35+
workon " env with space"
36+
start_dir="$(pwd)"
37+
cdvirtualenv
38+
assertSame "$VIRTUAL_ENV" "$(pwd)"
39+
cdvirtualenv bin
40+
assertSame "$VIRTUAL_ENV/bin" "$(pwd)"
41+
virtualenvwrapper_cd "$start_dir"
42+
}
43+
44+
test_cdsitepackages_space_in_name () {
45+
workon " env with space"
46+
start_dir="$(pwd)"
47+
cdsitepackages
48+
pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
49+
sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
50+
assertSame "$sitepackages" "$(pwd)"
51+
virtualenvwrapper_cd "$start_dir"
52+
}
53+
54+
55+
. "$test_dir/shunit2"

tests/test_cp.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ test_virtual_env_variable () {
4040
assertTrue "$WORKON_HOME not in $VIRTUAL_ENV" "echo $VIRTUAL_ENV | grep -q $WORKON_HOME"
4141
}
4242

43+
test_virtual_env_variable_space_in_name () {
44+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
45+
# and work with virtualenv on OSX, but error out on Linux.
46+
mkvirtualenv " space source" >/dev/null 2>&1
47+
cpvirtualenv " space source" " space destination" >/dev/null 2>&1
48+
assertSame "Wrong virtualenv name" " space destination" "$(basename "$VIRTUAL_ENV")"
49+
assertTrue "$WORKON_HOME not in $VIRTUAL_ENV" "echo $VIRTUAL_ENV | grep -q $WORKON_HOME"
50+
}
51+
4352
fake_venv () {
4453
###
4554
# create a silly file to ensure copy happens

tests/test_ls.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ test_lssitepackages () {
3232
deactivate
3333
}
3434

35+
test_lssitepackages_space_in_name () {
36+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
37+
# and work with virtualenv on OSX, but error out on Linux.
38+
mkvirtualenv " space lssitepackagestest" >/dev/null 2>&1
39+
contents="$(lssitepackages)"
40+
assertTrue "did not find easy_install in site-packages" "echo $contents | grep -q easy_install"
41+
deactivate
42+
}
43+
3544
test_lssitepackages_add2virtualenv () {
3645
mkvirtualenv "lssitepackagestest" >/dev/null 2>&1
3746
parent_dir=$(dirname $(pwd))
@@ -75,5 +84,13 @@ test_lsvirtualenv_space_in_workon_home () {
7584
WORKON_HOME="$old_home"
7685
}
7786

87+
test_lsvirtualenv_space_in_env_name () {
88+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
89+
# and work with virtualenv on OSX, but error out on Linux.
90+
mkvirtualenv " env with space"
91+
lsvirtualenv -b >"$WORKON_HOME/output" 2>&1
92+
assertTrue "Did not see expected message in \"$output\"" "cat \"$WORKON_HOME/output\" | grep -q ' env with space'"
93+
}
94+
7895

7996
. "$test_dir/shunit2"

tests/test_mkvirtualenv.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ test_create() {
2929
}
3030

3131
test_create_space_in_name() {
32-
mkvirtualenv "env with space" >/dev/null 2>&1
33-
assertTrue "Environment directory was not created" "[ -d \"$WORKON_HOME/env with space\" ]"
32+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
33+
# and work with virtualenv on OSX, but error out on Linux.
34+
mkvirtualenv " env with space" >/dev/null 2>&1
35+
assertTrue "Environment directory was not created" "[ -d \"$WORKON_HOME/ env with space\" ]"
3436
for hook in postactivate predeactivate postdeactivate
3537
do
36-
assertTrue "$hook was not created" "[ -f \"$WORKON_HOME/env with space/bin/$hook\" ]"
37-
assertFalse "$hook is executable" "[ -x \"$WORKON_HOME/env with space/bin/$hook\" ]"
38+
assertTrue "$hook was not created" "[ -f \"$WORKON_HOME/ env with space/bin/$hook\" ]"
39+
assertFalse "$hook is executable" "[ -x \"$WORKON_HOME/ env with space/bin/$hook\" ]"
3840
done
41+
assertTrue virtualenvwrapper_verify_active_environment
42+
env_name=$(basename "$VIRTUAL_ENV")
43+
assertSame " env with space" "$env_name"
3944
}
4045

4146
test_activates () {

tests/test_rmvirtualenv.sh

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,53 @@ oneTimeTearDown() {
1414
}
1515

1616
setUp () {
17-
echo
17+
mkvirtualenv "deleteme" >/dev/null 2>&1
18+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
19+
# and work with virtualenv on OSX, but error out on Linux.
20+
mkvirtualenv " env with space" >/dev/null 2>&1
21+
deactivate >/dev/null 2>&1
1822
}
1923

2024
test_remove () {
21-
mkvirtualenv "deleteme" >/dev/null 2>&1
2225
assertTrue "[ -d $WORKON_HOME/deleteme ]"
23-
deactivate
2426
rmvirtualenv "deleteme"
2527
assertFalse "[ -d $WORKON_HOME/deleteme ]"
2628
}
2729

30+
test_remove_space_in_name () {
31+
assertTrue "[ -d $WORKON_HOME/\" env with space\" ]"
32+
rmvirtualenv " env with space"
33+
assertFalse "[ -d $WORKON_HOME/\" env with space\" ]"
34+
}
35+
2836
test_remove_several_envs () {
29-
mkvirtualenv "deleteme" >/dev/null 2>&1
3037
assertTrue "[ -d $WORKON_HOME/deleteme ]"
31-
deactivate
32-
mkvirtualenv "deleteme2" >/dev/null 2>&1
33-
assertTrue "[ -d $WORKON_HOME/deleteme2 ]"
34-
deactivate
35-
rmvirtualenv "deleteme deleteme2"
38+
assertTrue "[ -d $WORKON_HOME/\" env with space\" ]"
39+
rmvirtualenv deleteme " env with space"
3640
assertFalse "[ -d $WORKON_HOME/deleteme ]"
37-
assertFalse "[ -d $WORKON_HOME/deleteme2 ]"
41+
assertFalse "[ -d $WORKON_HOME/\" env with space\" ]"
3842
}
3943

4044
test_within_virtualenv () {
41-
mkvirtualenv "deleteme" >/dev/null 2>&1
42-
assertTrue "[ -d $WORKON_HOME/deleteme ]"
45+
mkvirtualenv "deleteme2" >/dev/null 2>&1
46+
assertTrue "[ -d $WORKON_HOME/deleteme2 ]"
4347
cdvirtualenv
4448
assertSame "$VIRTUAL_ENV" "$(pwd)"
4549
deactivate
46-
rmvirtualenv "deleteme"
50+
rmvirtualenv "deleteme2"
4751
assertSame "$WORKON_HOME" "$(pwd)"
48-
assertFalse "[ -d $WORKON_HOME/deleteme ]"
52+
assertFalse "[ -d $WORKON_HOME/deleteme2 ]"
4953
}
5054

5155
test_rm_aliased () {
52-
mkvirtualenv "deleteme" >/dev/null 2>&1
53-
deactivate
5456
alias rm='rm -i'
5557
rmvirtualenv "deleteme"
5658
unalias rm
5759
}
5860

5961
test_no_such_env () {
60-
assertFalse "[ -d $WORKON_HOME/deleteme ]"
61-
assertTrue "rmvirtualenv deleteme"
62+
assertFalse "[ -d $WORKON_HOME/deleteme2 ]"
63+
assertTrue "rmvirtualenv deleteme2"
6264
}
6365

6466
test_no_workon_home () {

tests/test_workon.sh

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ oneTimeSetUp() {
77
rm -rf "$WORKON_HOME"
88
mkdir -p "$WORKON_HOME"
99
source "$test_dir/../virtualenvwrapper.sh"
10-
mkvirtualenv "env1" >/dev/null 2>&1
11-
mkvirtualenv "env2" >/dev/null 2>&1
12-
deactivate >/dev/null 2>&1
10+
mkvirtualenv "test1" >/dev/null 2>&1
11+
mkvirtualenv "test2" >/dev/null 2>&1
12+
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,
13+
# and work with virtualenv on OSX, but error out on Linux.
14+
mkvirtualenv " env with space" >/dev/null 2>&1
15+
deactivate >/dev/null 2>&1
1316
}
1417

1518
oneTimeTearDown() {
@@ -26,9 +29,9 @@ tearDown () {
2629
}
2730

2831
test_workon () {
29-
workon env1
32+
workon test1
3033
assertTrue virtualenvwrapper_verify_active_environment
31-
assertSame "env1" $(basename "$VIRTUAL_ENV")
34+
assertSame "test1" $(basename "$VIRTUAL_ENV")
3235
}
3336

3437
test_workon_activate_hooks () {
@@ -38,15 +41,15 @@ test_workon_activate_hooks () {
3841
echo "echo GLOBAL ${t}activate >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/${t}activate"
3942
chmod +x "$WORKON_HOME/${t}activate"
4043

41-
echo "#!/bin/sh" > "$WORKON_HOME/env2/bin/${t}activate"
42-
echo "echo ENV ${t}activate >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/env1/bin/${t}activate"
43-
chmod +x "$WORKON_HOME/env1/bin/${t}activate"
44+
echo "#!/bin/sh" > "$WORKON_HOME/test2/bin/${t}activate"
45+
echo "echo ENV ${t}activate >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/test1/bin/${t}activate"
46+
chmod +x "$WORKON_HOME/test1/bin/${t}activate"
4447
done
4548

4649
rm -f "$TMPDIR/catch_output"
4750
touch "$TMPDIR/catch_output"
4851

49-
workon env1
52+
workon test1
5053

5154
output=$(cat "$TMPDIR/catch_output")
5255
expected="GLOBAL preactivate
@@ -58,27 +61,29 @@ ENV postactivate"
5861

5962
for t in pre post
6063
do
61-
rm -f "$WORKON_HOME/env1/bin/${t}activate"
64+
rm -f "$WORKON_HOME/test1/bin/${t}activate"
6265
rm -f "$WORKON_HOME/${t}activate"
6366
done
6467
}
6568

6669
test_virtualenvwrapper_show_workon_options () {
6770
mkdir "$WORKON_HOME/not_env"
68-
(cd "$WORKON_HOME"; ln -s env1 link_env)
71+
(cd "$WORKON_HOME"; ln -s test1 link_env)
6972
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
70-
assertSame "env1 env2 link_env " "$envs"
73+
# On OSX there are two trailing spaces, on Linux one, so compare substring
74+
assertSame " env with space link_env test1 test2 " "${envs:0:37}"
7175
rmdir "$WORKON_HOME/not_env"
7276
rm -f "$WORKON_HOME/link_env"
7377
}
7478

7579
test_virtualenvwrapper_show_workon_options_grep_options () {
7680
mkdir "$WORKON_HOME/not_env"
77-
(cd "$WORKON_HOME"; ln -s env1 link_env)
81+
(cd "$WORKON_HOME"; ln -s test1 link_env)
7882
export GREP_OPTIONS="--count"
7983
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
8084
unset GREP_OPTIONS
81-
assertSame "env1 env2 link_env " "$envs"
85+
# On OSX there are two trailing spaces, on Linux one, so compare substring
86+
assertSame " env with space link_env test1 test2 " "${envs:0:37}"
8287
rmdir "$WORKON_HOME/not_env"
8388
rm -f "$WORKON_HOME/link_env"
8489
}
@@ -94,15 +99,17 @@ test_virtualenvwrapper_show_workon_options_chpwd () {
9499
}
95100
mkdir "$WORKON_HOME/not_env"
96101
envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
97-
assertSame "env1 env2 " "$envs"
102+
# On OSX there are two trailing spaces, on Linux one, so compare substring
103+
assertSame " env with space test1 test2 " "${envs:0:28}"
98104
rmdir "$WORKON_HOME/not_env"
99105
rm -f "$WORKON_HOME/link_env"
100106
}
101107

102108
test_virtualenvwrapper_show_workon_options_no_envs () {
103109
old_home="$WORKON_HOME"
104110
export WORKON_HOME=${TMPDIR:-/tmp}/$$
105-
envs=$(virtualenvwrapper_show_workon_options 2>/dev/null | tr '\n' ' ')
111+
# On OSX there is a space and on Linux there is not, so strip all spaces
112+
envs=$(virtualenvwrapper_show_workon_options 2>/dev/null | sed 's/\n //g')
106113
assertSame "" "$envs"
107114
export WORKON_HOME="$old_home"
108115
}
@@ -117,10 +124,25 @@ test_no_workon_home () {
117124
}
118125

119126
test_workon_dot () {
120-
cd $WORKON_HOME/env1
127+
cd $WORKON_HOME/test1
128+
workon .
129+
assertTrue virtualenvwrapper_verify_active_environment
130+
assertSame "test1" $(basename "$VIRTUAL_ENV")
131+
}
132+
133+
test_workon_dot_with_space () {
134+
cd $WORKON_HOME/" env with space"
121135
workon .
122136
assertTrue virtualenvwrapper_verify_active_environment
123-
assertSame "env1" $(basename "$VIRTUAL_ENV")
137+
env_name=$(basename "$VIRTUAL_ENV")
138+
assertSame " env with space" "$env_name"
139+
}
140+
141+
test_workon_with_space () {
142+
workon " env with space"
143+
assertTrue virtualenvwrapper_verify_active_environment
144+
env_name=$(basename "$VIRTUAL_ENV")
145+
assertSame " env with space" "$env_name"
124146
}
125147

126148
. "$test_dir/shunit2"

0 commit comments

Comments
 (0)