Commits

Doug Hellmann  committed dd04bba

convert test scripts to use tox instead of home-grown multi-version system in the Makefile

  • Participants
  • Parent commits 89212fb

Comments (0)

Files changed (20)

 trace.txt
 virtualenvwrapper.egg-info
 virtualenvwrapper/docs
+.tox
 
 syntax: re
 .DS_Store
+2010-12-24  Doug Hellmann  <dhellmann@racemi.com>
+
+	* virtualenvwrapper.sh (cpvirtualenv): Ensure that both pre hooks
+	are run, even if one produces an error.
+
+	* virtualenvwrapper/user_scripts.py: Change log message format.
+
+	* tox.ini: Configuration file for running tests under tox.
+
+	* tests/test_run_hook.sh (test_virtualenvwrapper_run_hook_permissions):
+	Fix the expected output message.
+
+	* tests/*: Make sure all of the test scripts have execute permission.
+
+	* tests/test.sh (test_virtualenvwrapper_verify_workon_home_missing_dir_quiet_init):
+	Use source instead of starting another shell, since zsh doesn't
+	play nicely with inheriting environment settings otherwise.
+	(test_python_interpreter_set_incorrectly): Force
+	VIRTUALENVWRAPPER_PYTHON before running the script in a sub-shell.
+
+	* tests/run_tests: Script to run through all of the other test
+	scripts one at a time.
+
 2010-09-18  Doug Hellmann  <dhellmann@racemi.com>
 
 	* virtualenvwrapper.sh: Apply patch from Zach Voase to fix
 # Get the version of the app.  This is used in the doc build.
 export VERSION=$(shell python setup.py --version)
 
-# Locations of Python interpreter binaries
-PYTHON27=/Users/dhellmann/Devel/virtualenvwrapper/Python/2.7b1/bin/python2.7
-ifeq ($VIRTUAL_ENV,)
-PYTHON26=$(shell which python2.6)
-else
-PYTHON26=/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
-endif
-PYTHON25=/Library/Frameworks/Python.framework/Versions/2.5/bin/python2.5
-PYTHON24=/Users/dhellmann/Devel/virtualenvwrapper/Python/2.4.6/bin/python2.4
-
-# The test-quick pattern changes the definition of
-# this variable to only run against a single version of python.
-ifeq ($(PYTHON_BINARIES),)
-PYTHON_BINARIES=$(PYTHON26) $(PYTHON27) $(PYTHON25) $(PYTHON24)
-endif
-
-SUPPORTED_SHELLS=bash sh ksh zsh
-
 # Default target is to show help
 help:
 	@echo "sdist          - Source distribution"
 	python setup.py register
 
 # Testing
-TEST_SCRIPTS=$(wildcard tests/test*.sh)
+test:
+	tox
 
-test:
-	for name in $(SUPPORTED_SHELLS) ; do \
-		$(MAKE) test-$$name || exit 1 ; \
-	done
-	$(MAKE) test-install
+test-quick:
+	tox -e py27
 
 develop:
 	python setup.py develop
-
-test-bash test-ksh test-sh:
-	TEST_SHELL=$(subst test-,,$@) $(MAKE) test-loop
-
-test-zsh:
-	TEST_SHELL="zsh -o shwordsplit" $(MAKE) test-loop
-
-# For each supported version of Python,
-# - Create a new virtualenv in a temporary directory.
-# - Install virtualenvwrapper into the new virtualenv
-# - Run each test script in tests
-test-loop:
-	for py_bin in $(PYTHON_BINARIES) ; do \
-		(cd $$TMPDIR/ && rm -rf virtualenvwrapper-test-env \
-			&& virtualenv -p $$py_bin --no-site-packages virtualenvwrapper-test-env) \
-			|| exit 1 ; \
-		$$TMPDIR/virtualenvwrapper-test-env/bin/python setup.py install || exit 1 ; \
-		for test_script in tests/test*.sh ; do \
-			echo ; \
-	 		echo '********************************************************************************' ; \
-			echo "Running $$test_script with $(TEST_SHELL) under Python $(basename $$py_bin)" ; \
-			echo ; \
-			HOOK_VERBOSE_OPTION=-v VIRTUALENVWRAPPER_PYTHON=$$TMPDIR/virtualenvwrapper-test-env/bin/python SHUNIT_PARENT=$$test_script $(TEST_SHELL) $$test_script || exit 1 ; \
-			echo ; \
-		done \
-	done
-
-test-quick:: test-26
-
-test-24:
-	PYTHON_BINARIES=$(PYTHON24) $(MAKE) test-bash
-
-test-25:
-	PYTHON_BINARIES=$(PYTHON25) $(MAKE) test-bash
-
-test-26:
-	PYTHON_BINARIES=$(PYTHON26) $(MAKE) test-bash
-
-test-27:
-	PYTHON_BINARIES=$(PYTHON27) $(MAKE) test-bash
-
-test-install:
-	bash ./tests/manual_test_install.sh `pwd`/dist "$(VERSION)"

File docs/en/developers.rst

 =============
 
 The test suite for virtualenvwrapper uses `shunit2
-<http://shunit2.googlecode.com/>`_.  To run the tests under bash, sh,
-and zsh, use ``make test``.  In order to add new tests, you will need
-to modify or create an appropriate script in the ``tests`` directory.
+<http://shunit2.googlecode.com/>`_ and `tox
+<http://codespeak.net/tox>`_.  To run the tests under bash, sh, and
+zsh, use ``make test`` or just ``tox``.  In order to add new tests,
+you will need to modify or create an appropriate script in the
+``tests`` directory.

File tests/run_tests

+#!/bin/sh
+
+##
+## ONLY RUN THIS VIA TOX
+##
+
+#set -x
+
+envdir="$1"
+shift
+scripts="$*"
+if [ -z "$scripts" ]
+then
+    scripts=$(ls tests/test*.sh)
+fi
+
+# Force the tox virtualenv to be active.  
+#
+# Since this script runs from within a separate shell created by tox,
+# the name of the virtualenv (in $VIRTUAL_ENV) is inherited, but the
+# shell functions and other settings created by the activate script
+# are *not* inherited.
+#
+source "$envdir/bin/activate"
+
+# Set up virtualenvwrapper.hook_loader to print more details than usual for debugging.
+export HOOK_VERBOSE_OPTION=-vvv
+
+# Force virtualenvwrapper to use the python interpreter in the
+# tox-created virtualenv.
+export VIRTUALENVWRAPPER_PYTHON="$envdir/bin/python"
+
+if [ -n "${ZSH_VERSION:-}" ]
+then
+    export SHELL=$(which zsh)
+fi
+
+# Run the test scripts with a little formatting around them to make it
+# easier to find where each script output starts.
+for test_script in $scripts
+do
+	echo
+ 	echo '********************************************************************************'
+	echo "Running $test_script"
+    echo "  VIRTUAL_ENV=$VIRTUAL_ENV"
+    echo "  VIRTUALENVWRAPPER_PYTHON=$VIRTUALENVWRAPPER_PYTHON"
+    echo "    $($VIRTUALENVWRAPPER_PYTHON -V 2>&1)"
+    echo "  PYTHONPATH=$PYTHONPATH"
+    echo "  SHELL=$SHELL"
+	echo
+    export SHUNIT_PARENT="$test_script"
+	$test_script || exit 1
+	echo
+done
+
+exit 0

File tests/test.sh

     rm -rf "$WORKON_HOME"
     mkdir -p "$WORKON_HOME"
     source "$test_dir/../virtualenvwrapper.sh"
-    echo $PYTHONPATH
 }
 
 oneTimeTearDown() {
 test_virtualenvwrapper_verify_workon_home_missing_dir_quiet_init() {
     old_home="$WORKON_HOME"
     export WORKON_HOME="$WORKON_HOME/not_there"
-    output=`$SHELL $test_dir/../virtualenvwrapper.sh 2>&1`
+    output=$(source $test_dir/../virtualenvwrapper.sh 2>&1)
     assertSame "" "$output"
     WORKON_HOME="$old_home"
 }
     cd "$WORKON_HOME"
     mkvirtualenv --no-site-packages no_wrappers
     expected="ImportError: No module named virtualenvwrapper.hook_loader"
-    output=$(VIRTUALENVWRAPPER_PYTHON=$VIRTUAL_ENV/bin/python $SHELL $return_to/virtualenvwrapper.sh 2>&1)
+    output=$(VIRTUALENVWRAPPER_PYTHON=$(which python) $SHELL $return_to/virtualenvwrapper.sh 2>&1)
     echo "$output" | grep "$expected" 2>&1
     found=$?
     assertTrue "Expected \"$expected\", got: \"$output\"" "[ $found -eq 0 ]"

File tests/test_add2virtualenv.sh

File contents unchanged.

File tests/test_cd.sh

File contents unchanged.

File tests/test_cp.sh

     mkvirtualenv "source"
 
     export pre_test_dir=$(cd "$test_dir"; pwd)
+
+    # Set the interpreter of the hook script to the simple shell
+    echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
     echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
     chmod +x "$WORKON_HOME/premkvirtualenv"
+
     echo "echo GLOBAL postmkvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postmkvirtualenv"
+
+    # Set the interpreter of the hook script to the simple shell
     echo "#!/bin/sh" > "$WORKON_HOME/precpvirtualenv"
     echo "echo GLOBAL precpvirtualenv \`pwd\` \"\$@\" >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/precpvirtualenv"
     chmod +x "$WORKON_HOME/precpvirtualenv"
+
+    # Set the interpreter of the hook script to the simple shell
     echo "#!/bin/sh" > "$WORKON_HOME/postcpvirtualenv"
     echo "echo GLOBAL postcpvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postcpvirtualenv"
 
 GLOBAL postcpvirtualenv"
 
     assertSame "$expected" "$output"
+
     rm -f "$WORKON_HOME/premkvirtualenv"
     rm -f "$WORKON_HOME/postmkvirtualenv"
 }

File tests/test_deactivate.sh

File contents unchanged.

File tests/test_derive_workon_home.sh

File contents unchanged.

File tests/test_ls.sh

File contents unchanged.

File tests/test_mkvirtualenv.sh

 
 test_hooks () {
     export pre_test_dir=$(cd "$test_dir"; pwd)
+
+    echo "#!/bin/sh" > "$WORKON_HOME/premkvirtualenv"
     echo "echo GLOBAL premkvirtualenv \`pwd\` \"\$@\" >> \"$pre_test_dir/catch_output\"" >> "$WORKON_HOME/premkvirtualenv"
     chmod +x "$WORKON_HOME/premkvirtualenv"
+
     echo "echo GLOBAL postmkvirtualenv >> $test_dir/catch_output" > "$WORKON_HOME/postmkvirtualenv"
     mkvirtualenv "env3"
     output=$(cat "$test_dir/catch_output")

File tests/test_rmvirtualenv.sh

File contents unchanged.

File tests/test_run_hook.sh

 test_virtualenvwrapper_run_hook_permissions() {
     echo "#!/bin/sh" > "$WORKON_HOME/prermvirtualenv"
     echo "echo run $@ >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/prermvirtualenv"
-    chmod -x "$WORKON_HOME/prermvirtualenv"
+    chmod 0444 "$WORKON_HOME/prermvirtualenv"
     touch "$test_dir/catch_output"
-    error=$(virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo" 2>&1 | grep "ERROR")
+    error=$(virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo" 2>&1 | grep "could not run" | cut -f2- -d'[')
     output=$(cat "$test_dir/catch_output")
     expected=""
     assertSame "$expected" "$output"
-    assertSame "virtualenvwrapper.user_scripts ERROR: Could not run $WORKON_HOME/prermvirtualenv. [Errno 13] Permission denied" "$error"
+    assertSame "Errno 13] Permission denied" "$error"
 }
 
 . "$test_dir/shunit2"

File tests/test_tempfile.sh

File contents unchanged.

File tests/test_workon.sh

 test_workon_activate_hooks () {
     for t in pre post
     do
-        echo "#!/bin/bash" > "$WORKON_HOME/${t}activate"
+        echo "#!/bin/sh" > "$WORKON_HOME/${t}activate"
         echo "echo GLOBAL ${t}activate >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/${t}activate"
         chmod +x "$WORKON_HOME/${t}activate"
-        echo "#!/bin/bash" > "$WORKON_HOME/env2/bin/${t}activate"
+
+        echo "#!/bin/sh" > "$WORKON_HOME/env2/bin/${t}activate"
         echo "echo ENV ${t}activate >> \"$test_dir/catch_output\"" >> "$WORKON_HOME/env1/bin/${t}activate"
         chmod +x "$WORKON_HOME/env1/bin/${t}activate"
     done
+[tox]
+envlist = py27,py26,py25,py24
+
+[testenv]
+commands = 
+  zsh -o shwordsplit ./tests/run_tests {envdir} []
+  ksh ./tests/run_tests {envdir} []
+  bash ./tests/run_tests {envdir} []
+
+# Not sure why this is needed, but on my system if it isn't included then
+# the python version picked up for 2.6 is actually 2.7.
+# IF THIS CAUSES YOU A PROBLEM COMMENT IT OUT BEFORE RUNNING THE TESTS.
+[testenv:py26]
+basepython=/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6

File virtualenvwrapper.sh

 
 # Duplicate the named virtualenv to make a new one.
 cpvirtualenv() {
-
     typeset env_name="$1"
     if [ "$env_name" = "" ]
     then
     virtualenv "$target_env" --relocatable
     \sed "s/VIRTUAL_ENV\(.*\)$env_name/VIRTUAL_ENV\1$new_env/g" < "$source_env/bin/activate" > "$target_env/bin/activate"
 
-    (cd "$WORKON_HOME" && 
-        virtualenvwrapper_run_hook "pre_cpvirtualenv" "$env_name" "$new_env" &&
+    (cd "$WORKON_HOME" && ( 
+        virtualenvwrapper_run_hook "pre_cpvirtualenv" "$env_name" "$new_env";
         virtualenvwrapper_run_hook "pre_mkvirtualenv" "$new_env"
-        )
+        ))
     workon "$new_env"
     virtualenvwrapper_run_hook "post_mkvirtualenv"
     virtualenvwrapper_run_hook "post_cpvirtualenv"

File virtualenvwrapper/user_scripts.py

 #             print f.read()
 #             print '+' * 80
         cmd = [script_path] + list(args)
-        log.debug('Running %s', str(cmd))
+        log.debug('running %s', str(cmd))
         try:
             return_code = subprocess.call(cmd)
         except OSError, msg:
-            log.error('ERROR: Could not run %s. %s', script_path, str(msg))
+            log.error('could not run "%s": %s', script_path, str(msg))
         #log.debug('Returned %s', return_code)
     return
 
     """
     filename = os.path.expanduser(os.path.expandvars(filename))
     if not os.path.exists(filename):
-        log.info('Creating %s', filename)
+        log.warning('creating %s', filename)
         f = open(filename, 'wt')
         try:
             f.write("""#!%(shell)s