Commits

Paul McLanahan  committed 0359a7c

Added "toggleglobalsitepackages" command.
Added tests for the new command.

  • Participants
  • Parent commits 7a4a540

Comments (0)

Files changed (2)

File tests/test_toggleglobalsitepackages.sh

+#!/bin/sh
+
+#set -x
+
+test_dir=$(cd $(dirname $0) && pwd)
+
+export WORKON_HOME="$(echo ${TMPDIR:-/tmp}/WORKON_HOME | sed 's|//|/|g')"
+
+oneTimeSetUp() {
+    rm -rf "$WORKON_HOME"
+    mkdir -p "$WORKON_HOME"
+    source "$test_dir/../virtualenvwrapper.sh"
+}
+
+oneTimeTearDown() {
+    rm -rf "$WORKON_HOME"
+}
+
+setUp () {
+    echo
+    rm -f "$test_dir/catch_output"
+    mkvirtualenv --no-site-packages "globaltest"
+}
+
+tearDown () {
+    deactivate
+    rmvirtualenv "globaltest"
+}
+
+test_toggleglobalsitepackages () {
+    ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"
+    assertTrue "$ngsp_file does not exist" "[ -f "$ngsp_file" ]"
+    toggleglobalsitepackages -q
+    assertFalse "$ngsp_file exists" "[ -f "$ngsp_file" ]"
+    toggleglobalsitepackages -q
+    assertTrue "$ngsp_file does not exist" "[ -f "$ngsp_file" ]"
+}
+
+test_toggleglobalsitepackages_quiet () {
+    assertEquals "Command output is not correct" "Enabled global site-packages" "`toggleglobalsitepackages`"
+    assertEquals "Command output is not correct" "Disabled global site-packages" "`toggleglobalsitepackages`"
+    
+    assertEquals "Command output is not correct" "" "`toggleglobalsitepackages -q`"
+    assertEquals "Command output is not correct" "" "`toggleglobalsitepackages -q`"
+}
+
+. "$test_dir/shunit2"

File virtualenvwrapper.sh

     fi
 }
 
+# Toggles the currently-active virtualenv between having and not having
+# access to the global site-packages.
+toggleglobalsitepackages () {
+    virtualenvwrapper_verify_workon_home || return 1
+    virtualenvwrapper_verify_active_environment || return 1
+    typeset no_global_site_packages_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt"
+    if [ -f $no_global_site_packages_file ]; then
+        rm $no_global_site_packages_file
+        [ "$1" = "-q" ] || echo "Enabled global site-packages"
+    else
+        touch $no_global_site_packages_file
+        [ "$1" = "-q" ] || echo "Disabled global site-packages"
+    fi
+}
+
 # Duplicate the named virtualenv to make a new one.
 cpvirtualenv() {
     typeset env_name="$1"