Commits

Doug Hellmann committed 4c8de91

error handling in mkvirtualenv

Comments (0)

Files changed (3)

 Updates
 =======
 
+upcoming
+  - Better error handling in mkvirtualenv.
+
 1.14
   - Wrap the virtualenv version of deactivate() with one that lets us invoke
     the predeactivate hooks.
 
 test_mkvirtualenv() {
     mkvirtualenv "env1"
-    assertTrue "[ -d $WORKON_HOME/env1 ]"
-    assertSame "env1" $(basename "$VIRTUAL_ENV")
+    assertTrue "Environment directory was not created" "[ -d $WORKON_HOME/env1 ]"
 }
 
 test_cdvirtual() {
     pushd "$(pwd)" >/dev/null   
     cdsitepackages
     pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
-    assertSame "$VIRTUAL_ENV/lib/python${pyvers}/site-packages" "$(pwd)"
+    sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
+    assertSame "$sitepackages" "$(pwd)"
     popd >/dev/null
 }
 
-test_mkvirtualenv_switches () {
+test_mkvirtualenv_activates () {
     mkvirtualenv "env2"
     assertTrue virtualenvwrapper_verify_active_environment
     assertSame "env2" $(basename "$VIRTUAL_ENV")
 }
 
+# test_mkvirtualenv_sitepackages () {
+#     # Without the option verify that site-packages are copied.
+#     mkvirtualenv "env3"
+#     assertSame "env3" "$(basename $VIRTUAL_ENV)"
+#     pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
+#     sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
+#     #cat "$sitepackages/easy-install.pth"
+#     assertTrue "Do not have expected virtualenv.py" "[ -f $sitepackages/virtualenv.py ]"
+#     rmvirtualenv "env3"
+#     
+#     # With the argument, verify that they are not copied.
+#     mkvirtualenv --no-site-packages "env4"
+#     assertSame "env4" $(basename "$VIRTUAL_ENV")
+#     pyvers=$(python -V 2>&1 | cut -f2 -d' ' | cut -f1-2 -d.)
+#     sitepackages="$VIRTUAL_ENV/lib/python${pyvers}/site-packages"
+#     assertTrue "[ -f $sitepackages/setuptools.pth ]"
+#     assertTrue "[ -f $sitepackages/easy-install.pth ]"
+#     assertFalse "Have virtualenv.py but should not" "[ -f $sitepackages/virtualenv.py ]"    
+#     rmvirtualenv "env4"
+# }
+
 test_workon () {
     workon env1
     assertTrue virtualenvwrapper_verify_active_environment
-    assertSame "env1" $(basename "$VIRTUAL_ENV")    
+    assertSame "env1" $(basename "$VIRTUAL_ENV")
 }
 
 test_postactivate_hook () {

virtualenvwrapper_bashrc

 WORKON_HOME=$(cd "$WORKON_HOME"; pwd)
 export WORKON_HOME
 
-VIRTUALENV_WRAPPER_BIN=$(dirname "$0")
-if [ "$VIRTUALENV_WRAPPER_BIN" = "." ]
-then
-    VIRTUALENV_WRAPPER_BIN=$(pwd)
-fi
-
 # Verify that the WORKON_HOME directory exists
 function virtualenvwrapper_verify_workon_home () {
     if [ ! -d "$WORKON_HOME" ]
 function mkvirtualenv () {
     eval "envname=\$$#"
     virtualenvwrapper_verify_workon_home || return 1
-    (cd "$WORKON_HOME"; 
-        virtualenv "$@"; 
+    (cd "$WORKON_HOME" &&
+        virtualenv "$@" &&
         virtualenvwrapper_run_hook "./premkvirtualenv" "$envname"
         )
+    # 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.
+    [ ! -d "$WORKON_HOME/$envname" ] && return 0
     workon "$envname"
     virtualenvwrapper_source_hook "$WORKON_HOME/postmkvirtualenv"
 }
     virtualenvwrapper_verify_workon_home || return 1
     # NOTE: DO NOT use ls here because colorized versions spew control characters
     #       into the output list.
-    (cd "$WORKON_HOME"; find -L . -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
+    # echo seems a little faster than find, even with -depth 3.
+    (cd "$WORKON_HOME"; for f in */bin/activate; do echo $f; done) | sed 's|^\./||' | sed 's|/bin/activate||' | sort
+#    (cd "$WORKON_HOME"; find -L . -depth 3 -path '*/bin/activate') | sed 's|^\./||' | sed 's|/bin/activate||' | sort
 }
 
 # List or change working virtual environments
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.