Commits

Doug Hellmann committed 9c004a3

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

  • Participants
  • Parent commits f0f0077

Comments (0)

Files changed (3)

File docs/source/history.rst

   Andy Dirnberger (:bbuser:`dirn`).
 - Add ``--force`` option to :ref:`command-mkproject`, contributed by
   Clay McClure (:bbuser:`claymcclure`).
+- Fix handling for project directory argument ``-a`` to
+  :ref:`command-mkvirtualenv`, based on work by Xupeng Yun.
 
 4.1.1
 =====

File tests/test_mkvirtualenv_associate.sh

     echo "#!/bin/sh" > "$WORKON_HOME/preactivate"
     echo "#!/bin/sh" > "$WORKON_HOME/postactivate"
     rm -f "$TMPDIR/catch_output"
+    cd "$WORKON_HOME"
 }
 
 test_associate() {
-    project="/dev/null"
-    env="env1"
+    n=1
+    project="$WORKON_HOME/project$n"
+    mkdir "$project"
+    env="env$n"
     ptrfile="$WORKON_HOME/$env/.project"
     mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
     assertTrue ".project not found" "[ -f $ptrfile ]"
     assertEquals "$ptrfile contains wrong content" "$project" "$(cat $ptrfile)"
 }
 
+test_associate_relative_path() {
+    n=2
+    project="project$n"
+    mkdir "$project"
+    env="env$n"
+    ptrfile="$WORKON_HOME/$env/.project"
+    mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
+    assertTrue ".project not found" "[ -f $ptrfile ]"
+    assertEquals "$ptrfile contains wrong content" "$WORKON_HOME/$project" "$(cat $ptrfile)"
+}
+
+test_associate_not_a_directory() {
+    n=3
+    project="project$n"
+    touch "$project"
+    env="env$n"
+    ptrfile="$WORKON_HOME/$env/.project"
+    mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
+    RC=$?
+    assertTrue "mkvirtualenv should have failed" "[ $RC -ne 0 ]"
+}
+
+test_associate_does_not_exist() {
+    n=4
+    project="project$n"
+    env="env$n"
+    ptrfile="$WORKON_HOME/$env/.project"
+    mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
+    RC=$?
+    assertTrue "mkvirtualenv should have failed" "[ $RC -ne 0 ]"
+}
+
 test_preactivate() {
-    project="/dev/null"
-    env="env2"
+    n=5
+    project="project$n"
+    mkdir "$project"
+    env="env$n"
     ptrfile="$WORKON_HOME/$env/.project"
 	cat - >"$WORKON_HOME/preactivate" <<EOF
 #!/bin/sh
 }
 
 test_postactivate() {
-    project="/dev/null"
-    env="env3"
+    n=6
+    project="project$n"
+    mkdir "$project"
+    env="env$n"
     ptrfile="$WORKON_HOME/$env/.project"
 cat - >"$WORKON_HOME/postactivate" <<EOF
 #!/bin/sh
 	assertSame "postactivate did not find file" "exists" "$(cat $TMPDIR/catch_output)"
 }
 
+test_associate_relative_with_dots() {
+    cd "$WORKON_HOME"
+    n=7
+    project="project$n"
+    mkdir $project
+    mkdir $project.sibling
+    cd $project.sibling
+    # Change the reference to a sibling directory
+    project="../$project"
+    env="env$n"
+    ptrfile="$WORKON_HOME/$env/.project"
+    mkvirtualenv -a "$project" "$env" >/dev/null 2>&1
+    assertTrue ".project not found" "[ -f $ptrfile ]"
+    assertEquals "$ptrfile contains wrong content" "$WORKON_HOME/project$n" "$(cat $ptrfile)"
+}
+
 . "$test_dir/shunit2"

File virtualenvwrapper.sh

     typeset requirements
     typeset packages
     typeset interpreter
+    typeset project
 
     in_args=( "$@" )
 
             -a)
                 i=$(( $i + 1 ));
                 project="${in_args[$i]}";;
+                if [ ! -d "$project" ]
+                then
+                    echo "$project is not a directory" 1>&2
+                    return 1
+                fi
+                project="$(virtualenvwrapper_absolutepath ${project})";;
             -h|--help)
                 virtualenvwrapper_mkvirtualenv_help $a;
                 return;;