Doug Hellmann avatar Doug Hellmann committed 35e5407

improve handling for missing WORKON_HOME variable or directory; add test for #18 - can't reproduce

Comments (0)

Files changed (6)

docsource/history.rst

 Release History
 ===============
 
+1.21
+
+  - Better protection of ``$WORKON_HOME`` does not exist when the wrapper script is sourced.
+
 1.20
 
   - Incorporate lssitepackages feature from Sander Smits.

docsource/index.rst

 Quick Setup
 ===========
 
-1. Add two lines to your .bashrc to set the location where the virtual environments should live and the location of the script installed with this package::
+1. Create a directory to hold all of the virtual environments. The default is
+   ``$HOME/.virtualenvs``.
+
+2. Add two lines to your .bashrc to set the location where the virtual environments should
+   live and the location of the script installed with this package::
 
     export WORKON_HOME=$HOME/.virtualenvs
     source /usr/local/bin/virtualenvwrapper_bashrc
 
-2. Run: ``source ~/.bashrc``
-3. Run: ``workon``
-4. A list of environments, empty, is printed.
-5. Run: ``mkvirtualenv temp``
-6. A new environment, ``temp`` is created and activated.
-7. Run: ``workon``
-8. This time, the ``temp`` environment is included.
+3. Run: ``source ~/.bashrc``
+4. Run: ``workon``
+5. A list of environments, empty, is printed.
+6. Run: ``mkvirtualenv temp``
+7. A new environment, ``temp`` is created and activated.
+8. Run: ``workon``
+9. This time, the ``temp`` environment is included.
 
 =======
 Details
 def test():
     sh('bash ./tests/test.sh')
     sh('SHUNIT_PARENT=./tests/test.sh zsh -o shwordsplit ./tests/test.sh')
+    sh('bash ./tests/test_misconfigured.sh')
+    sh('SHUNIT_PARENT=./tests/test_misconfigured.sh zsh -o shwordsplit ./tests/test.sh')
     return
     rm -f "$WORKON_HOME/link_env"
 }
 
+test_virtualenvwrapper_show_workon_options_no_envs () {
+    old_home="$WORKON_HOME"
+    export WORKON_HOME=${TMPDIR:/tmp}/$$
+    envs=$(virtualenvwrapper_show_workon_options | tr '\n' ' ')
+    assertSame "" "$envs"
+    export WORKON_HOME="$old_home"
+}
+
 test_rmvirtualenv () {
     mkvirtualenv "deleteme"
     assertTrue "[ -d $WORKON_HOME/deleteme ]"

tests/test_misconfigured.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(dirname $0)
+
+export SCRATCH_DIR="${TMPDIR:-/tmp}/$$"
+
+oneTimeSetUp() {
+    rm -rf "$SCRATCH_DIR"
+    mkdir -p "$SCRATCH_DIR"
+}
+
+oneTimeTearDown() {
+    rm -rf "$SCRATCH_DIR"
+}
+
+test_workon_home_missing () {
+    export WORKON_HOME="$SCRATCH_DIR/no_such_subdir"
+    $SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
+    RC=$?
+    assertSame "1" "$RC"
+}
+
+test_shell_expansion_fails () {
+    export WORKON_HOME="$SCRATCH_DIR/\$no_such_var"
+    $SHELL -ec "source $test_dir/../virtualenvwrapper_bashrc" 2>/dev/null
+    RC=$?
+    assertSame "1" "$RC"
+}
+
+. "$test_dir/shunit2"

virtualenvwrapper_bashrc

 #
 # Setup:
 #
-#  1. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
+#  1. Create a directory to hold the virtual environments.
+#     (mkdir $HOME/.virtualenvs).
+#  2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
 #     to your .bashrc.
-#  2. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
+#  3. Add a line like "source /path/to/this/file/virtualenvwrapper_bashrc"
 #     to your .bashrc.
-#  3. Run: source ~/.bashrc
-#  4. Run: workon
-#  5. A list of environments, empty, is printed.
-#  6. Run: mkvirtualenv temp
-#  7. Run: workon
-#  8. This time, the "temp" environment is included.
-#  9. Run: workon temp
-# 10. The virtual environment is activated.
+#  4. Run: source ~/.bashrc
+#  5. Run: workon
+#  6. A list of environments, empty, is printed.
+#  7. Run: mkvirtualenv temp
+#  8. Run: workon
+#  9. This time, the "temp" environment is included.
+# 10. Run: workon temp
+# 11. The virtual environment is activated.
 #
 
 # Make sure there is a default value for WORKON_HOME.
 
 # Normalize the directory name in case it includes 
 # relative path components.
-WORKON_HOME=$(sh -c 'cd "$WORKON_HOME"; pwd')
+WORKON_HOME=$(python -c "import os; print os.path.abspath(os.path.expandshell(os.path.expanduser(\"$pydir\")))")
 export WORKON_HOME
 
 # Verify that the WORKON_HOME directory exists
     return 0
 }
 
+virtualenvwrapper_verify_workon_home
+
 # Verify that the requested environment exists
 function virtualenvwrapper_verify_workon_environment () {
     typeset env_name="$1"
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.