Commits

Doug Hellmann  committed ebcf42a

add a test to verify that when virtualenv fails to create an environment the hook scripts are not run. see #76

  • Participants
  • Parent commits ef54ffd

Comments (0)

Files changed (4)

+2011-01-24  Doug Hellmann  <dhellmann@racemi.com>
+
+	* virtualenvwrapper.sh: Replace hard-coded name "virtualenv" with
+	the variable VIRTUALENVWRAPPER_VIRTUALENV to allow tests (and
+	users) to override it.
+
+	* tests/test_mkvirtualenv.sh (test_virtualenv_fails): Add a test
+	to reproduce the conditions reported in issue #76.
+
 2010-12-27  Doug Hellmann  <dhellmann@racemi.com>
 
 	* virtualenvwrapper.sh (virtualenvwrapper_get_python_version):

File docs/en/history.rst

   - Merged in `Japanese translation of the documentation
     <http://www.doughellmann.com/docs/virtualenvwrapper/ja/>`__ from
     Tetsuya Morimoto.
+  - Incorporate a suggestion from Ales Zoulek to let the user specify
+    the virtualenv binary through an environment variable
+    (``VIRTUALENVWRAPPER_VIRTUALENV``).
 
 2.6.1
 

File tests/test_mkvirtualenv.sh

     WORKON_HOME="$old_home"
 }
 
+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\` \"\$@\" >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
+    chmod +x "$WORKON_HOME/premkvirtualenv"
+
+    echo "echo GLOBAL postmkvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postmkvirtualenv"
+    mkvirtualenv "env3"
+    output=$(cat "$test_dir/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_sitepackages () {
 #     # Without the option verify that site-packages are copied.
 #     mkvirtualenv "env3"

File virtualenvwrapper.sh

     VIRTUALENVWRAPPER_PYTHON="$(\which python)"
 fi
 
+# Set the name of the virtualenv app to use.
+if [ "$VIRTUALENVWRAPPER_VIRTUALENV" = "" ]
+then
+    VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
+fi
+
 virtualenvwrapper_derive_workon_home() {
     typeset workon_home_dir="$WORKON_HOME"
 
 
 # Verify that virtualenv is installed and visible
 virtualenvwrapper_verify_virtualenv () {
-    typeset venv=$(\which virtualenv | (unset GREP_OPTIONS; \grep -v "not found"))
+    typeset venv=$(\which "$VIRTUALENVWRAPPER_VIRTUALENV" | (unset GREP_OPTIONS; \grep -v "not found"))
     if [ "$venv" = "" ]
     then
-        echo "ERROR: virtualenvwrapper could not find virtualenv in your path" >&2
+        echo "ERROR: virtualenvwrapper could not find $VIRTUALENVWRAPPER_VIRTUALENV in your path" >&2
         return 1
     fi
     if [ ! -e "$venv" ]
     then
-        echo "ERROR: Found virtualenv in path as \"$venv\" but that does not exist" >&2
+        echo "ERROR: Found $VIRTUALENVWRAPPER_VIRTUALENV in path as \"$venv\" but that does not exist" >&2
         return 1
     fi
     return 0
     virtualenvwrapper_verify_workon_home || return 1
     virtualenvwrapper_verify_virtualenv || return 1
     (cd "$WORKON_HOME" &&
-        virtualenv "$@" &&
-        [ -d "$WORKON_HOME/$envname" ] && virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
+        "$VIRTUALENVWRAPPER_VIRTUALENV" "$@" &&
+        [ -d "$WORKON_HOME/$envname" ] && \
+            virtualenvwrapper_run_hook "pre_mkvirtualenv" "$envname"
         )
     typeset RC=$?
     [ $RC -ne 0 ] && return $RC
+
     # If they passed a help option or got an error from virtualenv,
     # the environment won't exist.  Use that to tell whether
     # we should switch to the environment and run the hook.