Skip to content

Commit 0eb367c

Browse files
committed
Changes as per code review.
1 parent 04664ca commit 0eb367c

File tree

7 files changed

+93
-31
lines changed

7 files changed

+93
-31
lines changed

tests/test_allvirtualenv.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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
1315
# Only test with leading and internal spaces. Directory names with trailing spaces are legal,

tests/test_cd.sh

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ oneTimeSetUp() {
99
unset VIRTUAL_ENV
1010
source "$test_dir/../virtualenvwrapper.sh"
1111
mkvirtualenv cd-test >/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
1512
deactivate
1613
}
1714

@@ -41,16 +38,6 @@ test_cdvirtual() {
4138
virtualenvwrapper_cd "$start_dir"
4239
}
4340

44-
test_cdvirtual_space_in_name() {
45-
workon " env with space"
46-
start_dir="$(pwd)"
47-
cdvirtualenv
48-
assertSame "$VIRTUAL_ENV" "$(pwd)"
49-
cdvirtualenv bin
50-
assertSame "$VIRTUAL_ENV/bin" "$(pwd)"
51-
virtualenvwrapper_cd "$start_dir"
52-
}
53-
5441
test_cdsitepackages () {
5542
start_dir="$(pwd)"
5643
cdsitepackages
@@ -60,16 +47,6 @@ test_cdsitepackages () {
6047
virtualenvwrapper_cd "$start_dir"
6148
}
6249

63-
test_cdsitepackages_space_in_name () {
64-
workon " env with space"
65-
start_dir="$(pwd)"
66-
cdsitepackages
67-
pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
68-
sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
69-
assertSame "$sitepackages" "$(pwd)"
70-
virtualenvwrapper_cd "$start_dir"
71-
}
72-
7350
test_cdsitepackages_with_arg () {
7451
start_dir="$(pwd)"
7552
pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)

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
@@ -33,7 +33,16 @@ test_new_env_activated () {
3333
assertTrue virtualenvwrapper_verify_active_environment
3434
}
3535

36+
test_virtual_env_variable () {
37+
mkvirtualenv "source" >/dev/null 2>&1
38+
cpvirtualenv "source" "destination" >/dev/null 2>&1
39+
assertSame "Wrong virtualenv name" "destination" $(basename "$VIRTUAL_ENV")
40+
assertTrue "$WORKON_HOME not in $VIRTUAL_ENV" "echo $VIRTUAL_ENV | grep -q $WORKON_HOME"
41+
}
42+
3643
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.
3746
mkvirtualenv " space source" >/dev/null 2>&1
3847
cpvirtualenv " space source" " space destination" >/dev/null 2>&1
3948
assertSame "Wrong virtualenv name" " space destination" "$(basename "$VIRTUAL_ENV")"

tests/test_ls.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,16 @@ test_get_site_packages_dir () {
2525
deactivate
2626
}
2727

28+
test_lssitepackages () {
29+
mkvirtualenv "lssitepackagestest" >/dev/null 2>&1
30+
contents="$(lssitepackages)"
31+
assertTrue "did not find easy_install in site-packages" "echo $contents | grep -q easy_install"
32+
deactivate
33+
}
34+
2835
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.
2938
mkvirtualenv " space lssitepackagestest" >/dev/null 2>&1
3039
contents="$(lssitepackages)"
3140
assertTrue "did not find easy_install in site-packages" "echo $contents | grep -q easy_install"

tests/test_workon.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ test_virtualenvwrapper_show_workon_options_chpwd () {
108108
test_virtualenvwrapper_show_workon_options_no_envs () {
109109
old_home="$WORKON_HOME"
110110
export WORKON_HOME=${TMPDIR:-/tmp}/$$
111-
envs=$(virtualenvwrapper_show_workon_options 2>/dev/null\
112-
| tr '\n' '' 2>/dev/null | tr ' ' '' 2>/dev/null)
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')
113113
assertSame "" "$envs"
114114
export WORKON_HOME="$old_home"
115115
}

virtualenvwrapper.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ function rmvirtualenv {
521521

522522
# support to remove several environments
523523
typeset env_name
524+
# Must quote the parameters, as environments could have spaces in their names
524525
for env_name in "$@"
525526
do
526527
echo "Removing $env_name..."
@@ -559,13 +560,18 @@ function virtualenvwrapper_show_workon_options {
559560
# NOTE: DO NOT use ls or cd here because colorized versions spew control
560561
# characters into the output list.
561562
# echo seems a little faster than find, even with -depth 3.
563+
# Note that this is a little tricky, as there may be spaces in the path.
562564
#
563565
# 1. Look for environments by finding the activate scripts.
564566
# Use a subshell so we can suppress the message printed
565567
# by zsh if the glob pattern fails to match any files.
566-
# 2. Strip the WORKON_HOME prefix from each name.
567-
# 3. Strip the bindir/activate script suffix.
568-
# 4. Format the output to show one name on a line.
568+
# This yields a single, space-separated line containing all matches.
569+
# 2. Replace the trailing newline with a space, so every
570+
# possible env has a space following it.
571+
# 3. Strip the bindir/activate script suffix, replacing it with
572+
# a slash, as that is an illegal character in a directory name.
573+
# This yields a slash-separated list of possible env names.
574+
# 4. Replace each slash with a newline to show the output one name per line.
569575
# 5. Eliminate any lines with * on them because that means there
570576
# were no envs.
571577
(virtualenvwrapper_cd "$WORKON_HOME" && echo */$VIRTUALENVWRAPPER_ENV_BIN_DIR/activate) 2>/dev/null \
@@ -723,6 +729,8 @@ function workon {
723729
return 1
724730
elif [ "$env_name" = "." ]
725731
then
732+
# The IFS default of breaking on whitespace causes issues if there
733+
# are spaces in the env_name, so change it.
726734
IFS='%'
727735
env_name="$(basename $(pwd))"
728736
unset IFS
@@ -885,15 +893,15 @@ function cdsitepackages {
885893
virtualenvwrapper_verify_workon_home || return 1
886894
virtualenvwrapper_verify_active_environment || return 1
887895
typeset site_packages="`virtualenvwrapper_get_site_packages_dir`"
888-
virtualenvwrapper_cd "$site_packages"/$1
896+
virtualenvwrapper_cd "$site_packages/$1"
889897
}
890898

891899
# Does a ``cd`` to the root of the currently-active virtualenv.
892900
#:help:cdvirtualenv: change to the $VIRTUAL_ENV directory
893901
function cdvirtualenv {
894902
virtualenvwrapper_verify_workon_home || return 1
895903
virtualenvwrapper_verify_active_environment || return 1
896-
virtualenvwrapper_cd "$VIRTUAL_ENV"/$1
904+
virtualenvwrapper_cd "$VIRTUAL_ENV/$1"
897905
}
898906

899907
# Shows the content of the site-packages directory of the currently-active
@@ -1293,7 +1301,9 @@ function allvirtualenv {
12931301
virtualenvwrapper_verify_workon_home || return 1
12941302
typeset d
12951303

1296-
IFS=''
1304+
# The IFS default of breaking on whitespace causes issues if there
1305+
# are spaces in the env_name, so change it.
1306+
IFS='%'
12971307
virtualenvwrapper_show_workon_options | while read d
12981308
do
12991309
[ ! -d "$WORKON_HOME/$d" ] && continue

0 commit comments

Comments
 (0)