Skip to content

Commit 9c004a3

Browse files
committed
Fix mkvirtualenv -a relative paths
Ensure that the path passed to mkvirtualenv -a is a directory and expand the path before writing it to the .project file in case the value given was a relative directory name. Change-Id: I4abd72b87be7edc0785387dc1bb7f99dc2f4b500
1 parent f0f0077 commit 9c004a3

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

docs/source/history.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ dev
1313
Andy Dirnberger (:bbuser:`dirn`).
1414
- Add ``--force`` option to :ref:`command-mkproject`, contributed by
1515
Clay McClure (:bbuser:`claymcclure`).
16+
- Fix handling for project directory argument ``-a`` to
17+
:ref:`command-mkvirtualenv`, based on work by Xupeng Yun.
1618

1719
4.1.1
1820
=====

tests/test_mkvirtualenv_associate.sh

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,57 @@ setUp () {
1919
echo "#!/bin/sh" > "$WORKON_HOME/preactivate"
2020
echo "#!/bin/sh" > "$WORKON_HOME/postactivate"
2121
rm -f "$TMPDIR/catch_output"
22+
cd "$WORKON_HOME"
2223
}
2324

2425
test_associate() {
25-
project="/dev/null"
26-
env="env1"
26+
n=1
27+
project="$WORKON_HOME/project$n"
28+
mkdir "$project"
29+
env="env$n"
2730
ptrfile="$WORKON_HOME/$env/.project"
2831
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
2932
assertTrue ".project not found" "[ -f $ptrfile ]"
3033
assertEquals "$ptrfile contains wrong content" "$project" "$(cat $ptrfile)"
3134
}
3235

36+
test_associate_relative_path() {
37+
n=2
38+
project="project$n"
39+
mkdir "$project"
40+
env="env$n"
41+
ptrfile="$WORKON_HOME/$env/.project"
42+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
43+
assertTrue ".project not found" "[ -f $ptrfile ]"
44+
assertEquals "$ptrfile contains wrong content" "$WORKON_HOME/$project" "$(cat $ptrfile)"
45+
}
46+
47+
test_associate_not_a_directory() {
48+
n=3
49+
project="project$n"
50+
touch "$project"
51+
env="env$n"
52+
ptrfile="$WORKON_HOME/$env/.project"
53+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
54+
RC=$?
55+
assertTrue "mkvirtualenv should have failed" "[ $RC -ne 0 ]"
56+
}
57+
58+
test_associate_does_not_exist() {
59+
n=4
60+
project="project$n"
61+
env="env$n"
62+
ptrfile="$WORKON_HOME/$env/.project"
63+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
64+
RC=$?
65+
assertTrue "mkvirtualenv should have failed" "[ $RC -ne 0 ]"
66+
}
67+
3368
test_preactivate() {
34-
project="/dev/null"
35-
env="env2"
69+
n=5
70+
project="project$n"
71+
mkdir "$project"
72+
env="env$n"
3673
ptrfile="$WORKON_HOME/$env/.project"
3774
cat - >"$WORKON_HOME/preactivate" <<EOF
3875
#!/bin/sh
@@ -49,8 +86,10 @@ EOF
4986
}
5087

5188
test_postactivate() {
52-
project="/dev/null"
53-
env="env3"
89+
n=6
90+
project="project$n"
91+
mkdir "$project"
92+
env="env$n"
5493
ptrfile="$WORKON_HOME/$env/.project"
5594
cat - >"$WORKON_HOME/postactivate" <<EOF
5695
#!/bin/sh
@@ -66,4 +105,20 @@ EOF
66105
assertSame "postactivate did not find file" "exists" "$(cat $TMPDIR/catch_output)"
67106
}
68107

108+
test_associate_relative_with_dots() {
109+
cd "$WORKON_HOME"
110+
n=7
111+
project="project$n"
112+
mkdir $project
113+
mkdir $project.sibling
114+
cd $project.sibling
115+
# Change the reference to a sibling directory
116+
project="../$project"
117+
env="env$n"
118+
ptrfile="$WORKON_HOME/$env/.project"
119+
mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
120+
assertTrue ".project not found" "[ -f $ptrfile ]"
121+
assertEquals "$ptrfile contains wrong content" "$WORKON_HOME/project$n" "$(cat $ptrfile)"
122+
}
123+
69124
. "$test_dir/shunit2"

virtualenvwrapper.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ function mkvirtualenv {
383383
typeset requirements
384384
typeset packages
385385
typeset interpreter
386+
typeset project
386387

387388
in_args=( "$@" )
388389

@@ -402,6 +403,12 @@ function mkvirtualenv {
402403
-a)
403404
i=$(( $i + 1 ));
404405
project="${in_args[$i]}";;
406+
if [ ! -d "$project" ]
407+
then
408+
echo "$project is not a directory" 1>&2
409+
return 1
410+
fi
411+
project="$(virtualenvwrapper_absolutepath ${project})";;
405412
-h|--help)
406413
virtualenvwrapper_mkvirtualenv_help $a;
407414
return;;

0 commit comments

Comments
 (0)