Commits

Doug Hellmann committed 753b0af

convert path deriving code in startup of script to function so it is easier to test

Comments (0)

Files changed (2)

tests/test_derive_workon_home.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(cd $(dirname $0) && pwd)
+
+export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
+TMP_WORKON_HOME="$WORKON_HOME"
+
+oneTimeSetUp() {
+    rm -rf "$TMP_WORKON_HOME"
+    mkdir -p "$TMP_WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
+    echo $PYTHONPATH
+}
+
+oneTimeTearDown() {
+    rm -rf "$TMP_WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+    WORKON_HOME="$TMP_WORKON_HOME"
+}
+
+test_default() {
+    unset WORKON_HOME
+    assertSame "$HOME/.virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
+}
+
+test_includes_relative_path() {
+    WORKON_HOME="$WORKON_HOME/../$(basename $WORKON_HOME)"
+    assertSame "$WORKON_HOME" "$(virtualenvwrapper_derive_workon_home)"
+}
+
+test_begins_relative_path() {
+    WORKON_HOME=".test-virtualenvs"
+    assertSame "$HOME/.test-virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
+}
+
+test_includes_tilde() {
+    WORKON_HOME="~/.test-virtualenvs"
+    assertSame "$HOME/.test-virtualenvs" "$(virtualenvwrapper_derive_workon_home)"
+}
+
+. "$test_dir/shunit2"

virtualenvwrapper.sh

 # 11. The virtual environment is activated.
 #
 
-# Make sure there is a default value for WORKON_HOME.
-# You can override this setting in your .bashrc.
-if [ "$WORKON_HOME" = "" ]
-then
-    export WORKON_HOME="$HOME/.virtualenvs"
-fi
-
 # Locate the global Python where virtualenvwrapper is installed.
 if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ]
 then
     VIRTUALENVWRAPPER_PYTHON="$(\which python)"
 fi
 
-# If the path is relative, prefix it with $HOME
-# (note: for compatibility)
-if echo "$WORKON_HOME" | (unset GREP_OPTIONS; grep -e '^[^/~]' > /dev/null)
-then
-    export WORKON_HOME="$HOME/$WORKON_HOME"
-fi
+virtualenvwrapper_derive_workon_home() {
+    typeset workon_home_dir="$WORKON_HOME"
 
-# Only call on Python to fix the path if it looks like the
-# path might contain stuff to expand.
-# (it might be possible to do this in shell, but I don't know a
-# cross-shell-safe way of doing it -wolever)
-if echo "$WORKON_HOME" | (unset GREP_OPTIONS; egrep -e "([\$~]|//)" >/dev/null)
-then
-    # This will normalize the path by:
-    # - Removing extra slashes (e.g., when TMPDIR ends in a slash)
-    # - Expanding variables (e.g., $foo)
-    # - Converting ~s to complete paths (e.g., ~/ to /home/brian/ and ~arthur to /home/arthur)
-    WORKON_HOME=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.expandvars(os.path.expanduser(\"$WORKON_HOME\"))")
-    export WORKON_HOME
-fi
+    # Make sure there is a default value for WORKON_HOME.
+    # You can override this setting in your .bashrc.
+    if [ "$workon_home_dir" = "" ]
+    then
+        workon_home_dir="$HOME/.virtualenvs"
+    fi
+
+    # If the path is relative, prefix it with $HOME
+    # (note: for compatibility)
+    if echo "$workon_home_dir" | (unset GREP_OPTIONS; grep -e '^[^/~]' > /dev/null)
+    then
+        workon_home_dir="$HOME/$WORKON_HOME"
+    fi
+
+    # Only call on Python to fix the path if it looks like the
+    # path might contain stuff to expand.
+    # (it might be possible to do this in shell, but I don't know a
+    # cross-shell-safe way of doing it -wolever)
+    if echo "$workon_home_dir" | (unset GREP_OPTIONS; egrep -e "([\$~]|//)" >/dev/null)
+    then
+        # This will normalize the path by:
+        # - Removing extra slashes (e.g., when TMPDIR ends in a slash)
+        # - Expanding variables (e.g., $foo)
+        # - Converting ~s to complete paths (e.g., ~/ to /home/brian/ and ~arthur to /home/arthur)
+        workon_home_dir=$("$VIRTUALENVWRAPPER_PYTHON" -c "import os; print os.path.expandvars(os.path.expanduser(\"$workon_home_dir\"))")
+    fi
+
+    echo "$workon_home_dir"
+    return 0
+}
 
 # Verify that the WORKON_HOME directory exists
 virtualenvwrapper_verify_workon_home () {
 
 # Set up virtualenvwrapper properly
 virtualenvwrapper_initialize () {
+    export WORKON_HOME=$(virtualenvwrapper_derive_workon_home)
     virtualenvwrapper_verify_workon_home -q || return 1
     virtualenvwrapper_run_hook "initialize"
     if [ $? -ne 0 ]
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.