Commits

Doug Hellmann committed a6c12dd

Set VIRTUALENVWRAPPER_SCRIPT correctly for different shells

The VIRTUALENVWRAPPER_SCRIPT variable was being set incorrectly.
Each shell has its own way to identify the script being sourced,
so use the right method based on which shell is in use.

Reported by Alexandre de Verteuil <claudelepoisson@gmail.com>, with
an initial patch for bash.

Change-Id: Ia0dbb1ebae726fa3bee2e23cc63bf25196a8a0fa

Comments (0)

Files changed (2)

tests/test_virtualenvwrapper.sh

+# -*- mode: shell-script -*-
+#
+# Tests for help function 'virtualenvwrapper'
+
+test_dir=$(cd $(dirname $0) && pwd)
+source "$test_dir/setup.sh"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    unset VIRTUALENVWRAPPER_SCRIPT
+    rm -f "$TMPDIR/catch_output"
+}
+
+test_virtualenvwrapper_script_set() {
+    source "$test_dir/../virtualenvwrapper.sh"
+    assertTrue "VIRTUALENVWRAPPER_SCRIPT is not right: $VIRTUALENVWRAPPER_SCRIPT" \
+        "echo $VIRTUALENVWRAPPER_SCRIPT | grep -q /virtualenvwrapper.sh"
+}
+
+. "$test_dir/shunit2"

virtualenvwrapper.sh

 # Remember where we are running from.
 if [ -z "$VIRTUALENVWRAPPER_SCRIPT" ]
 then
-    export VIRTUALENVWRAPPER_SCRIPT="$0"
+    if [ -n "$BASH" ]
+    then
+        export VIRTUALENVWRAPPER_SCRIPT="$BASH_SOURCE"
+    elif [ -n "$ZSH_VERSION" ]
+    then
+        export VIRTUALENVWRAPPER_SCRIPT="$0"
+    else
+        export VIRTUALENVWRAPPER_SCRIPT="${.sh.file}"
+    fi
 fi
 
 # Portable shell scripting is hard, let's go shopping.