diff --git a/docs/source/command_ref.rst b/docs/source/command_ref.rst index f2e2f33..eadb850 100644 --- a/docs/source/command_ref.rst +++ b/docs/source/command_ref.rst @@ -465,36 +465,6 @@ for the environment. *Based on a contribution from James Bennett and Jannis Leidel.* -.. _command-toggleglobalsitepackages: - -toggleglobalsitepackages ------------------------- - -Controls whether the active virtualenv will access the packages in the -global Python ``site-packages`` directory. - -Syntax:: - - toggleglobalsitepackages [-q] - -Outputs the new state of the virtualenv. Use the ``-q`` switch to turn off all -output. - -:: - - $ mkvirtualenv env1 - New python executable in env1/bin/python - Installing setuptools............................................. - .................................................................. - .................................................................. - done. - (env1)$ toggleglobalsitepackages - Disabled global site-packages - (env1)$ toggleglobalsitepackages - Enabled global site-packages - (env1)$ toggleglobalsitepackages -q - (env1)$ - ============================ Project Directory Management ============================ diff --git a/docs/source/developers.rst b/docs/source/developers.rst index 83ce8e4..8de06e5 100644 --- a/docs/source/developers.rst +++ b/docs/source/developers.rst @@ -69,22 +69,29 @@ The test suite for virtualenvwrapper uses shunit2_ and tox_. The shunit2 source is included in the ``tests`` directory, but tox must be installed separately (``pip install tox``). -To run the tests under bash, zsh, and ksh for Python 2.7 through 3.6, -run ``tox`` from the top level directory of the hg repository. +To run the tests under bash and zsh for the default Python, +run ``tox`` from the top level directory of the hg repository:: + + $ tox To run individual test scripts, use a command like:: - $ tox tests/test_cd.sh + $ tox -- tests/test_cd.sh To run tests under a single version of Python, specify the appropriate environment when running tox:: - $ tox -e py27 + $ tox -e py311 Combine the two modes to run specific tests with a single version of Python:: - $ tox -e py27 tests/test_cd.sh + $ tox -e py311 -- tests/test_cd.sh + +To stop the test suite as soon as any test fails, use the `fast` tox +target:: + + $ tox -e fast Add new tests by modifying an existing file or creating new script in the ``tests`` directory. diff --git a/tests/run_tests b/tests/run_tests index aa2e55c..68cb362 100755 --- a/tests/run_tests +++ b/tests/run_tests @@ -1,7 +1,7 @@ # -*- mode: shell-script -*- #set -x -envdir="$1" +envdir="$(cd $1 && pwd)" shift scripts="$*" if [ -z "$scripts" ] @@ -14,6 +14,9 @@ then fi fi +# Override FAIL_FAST to true to exit as soon as any test fails +FAIL_FAST=${FAIL_FAST:-false} + # Force the tox virtualenv to be active. # # Since this script runs from within a separate shell created by tox, @@ -33,7 +36,7 @@ export HOOK_VERBOSE_OPTION # Force virtualenvwrapper to use the python interpreter in the # tox-created virtualenv. -VIRTUALENVWRAPPER_PYTHON="$envdir/bin/python" +VIRTUALENVWRAPPER_PYTHON="$envdir/bin/python3" export VIRTUALENVWRAPPER_PYTHON # Clear any user settings for the hook directory or log directory @@ -44,6 +47,7 @@ unset VIRTUALENVWRAPPER_VIRTUALENV_ARGS # Run the test scripts with a little formatting around them to make it # easier to find where each script output starts. +RC=0 for test_script in $scripts do @@ -66,9 +70,14 @@ do echo SHUNIT_PARENT="$test_script" export SHUNIT_PARENT - $SHELL $test_shell_opts $test_script || exit 1 + if ! $SHELL $test_shell_opts $test_script; then + RC=1 + if $FAIL_FAST; then + exit $RC + fi + fi echo done -exit 0 +exit $RC diff --git a/tests/setup.sh b/tests/setup.sh index a28d360..6e006fb 100755 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -10,8 +10,3 @@ export WORKON_HOME=$(mktemp -d -t "WORKON_HOME.XXXX.$$") export PROJECT_HOME=$(mktemp -d -t "PROJECT_HOME.XXXX.$$") #unset HOOK_VERBOSE_OPTION - -# tox no longer exports these variables by default, so set them -# ourselves to ensure the tests that rely on the values work -export USER=$(id -u -n) -export HOME=$(id -P | cut -f9 -d:) diff --git a/tests/test_cp.sh b/tests/test_cp.sh index fc93ec1..3bd9196 100755 --- a/tests/test_cp.sh +++ b/tests/test_cp.sh @@ -98,17 +98,6 @@ test_virtualenvwrapper_virtualenv_clone_variable () { assertTrue "VIRTUALENVWRAPPER_CLONE did not clone fake file" "[ -f $WORKON_HOME/destination/fake_virtualenv_clone_was_here ]" } -test_source_relocatable () { - mkvirtualenv "source" >/dev/null 2>&1 - (cd tests/testpackage && python setup.py install) >/dev/null 2>&1 - assertTrue "virtualenv --relocatable \"$WORKON_HOME/source\"" - cpvirtualenv "source" "destination" >/dev/null 2>&1 - testscript="$(which testscript.py)" - assertTrue "Environment test script not the same as copy" "[ $WORKON_HOME/destination/bin/testscript.py -ef $testscript ]" - assertTrue virtualenvwrapper_verify_active_environment - assertSame "Wrong virtualenv name" "destination" $(basename "$VIRTUAL_ENV") -} - test_source_does_not_exist () { assertSame "Please provide a valid virtualenv to copy." "$(cpvirtualenv virtualenvthatdoesntexist foo)" } @@ -148,33 +137,4 @@ GLOBAL postcpvirtualenv" rm -f "$WORKON_HOME/postmkvirtualenv" } -test_no_site_packages () { - # See issue #102 - mkvirtualenv "source" --no-site-packages >/dev/null 2>&1 - cpvirtualenv "source" "destination" >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist in copied env" "[ -f \"$ngsp_file\" ]" -} - -test_no_site_packages_default_args () { - # See issue #102 - VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--no-site-packages" - # With the argument, verify that they are not copied. - mkvirtualenv "source" >/dev/null 2>&1 - cpvirtualenv "source" "destination" >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist" "[ -f \"$ngsp_file\" ]" - unset VIRTUALENVWRAPPER_VIRTUALENV_ARGS -} - -test_no_site_packages_default_behavior () { - # See issue #102 - # virtualenv 1.7 changed to make --no-site-packages the default - mkvirtualenv "source" >/dev/null 2>&1 - cpvirtualenv "source" "destination" >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist in copied env" "[ -f \"$ngsp_file\" ]" -} - . "$test_dir/shunit2" - diff --git a/tests/test_lazy_loaded.sh b/tests/test_lazy_loaded.sh index 46e4f58..9d217c2 100755 --- a/tests/test_lazy_loaded.sh +++ b/tests/test_lazy_loaded.sh @@ -64,10 +64,6 @@ test_lssitepackages_defined_normal() { function_defined_normal lssitepackages } -test_toggleglobalsitepackages_defined_normal() { - function_defined_normal toggleglobalsitepackages -} - test_cpvirtualenv_defined_normal() { function_defined_normal cpvirtualenv } diff --git a/tests/test_ls.sh b/tests/test_ls.sh index 8d26b3a..25ceab2 100755 --- a/tests/test_ls.sh +++ b/tests/test_ls.sh @@ -27,8 +27,8 @@ test_get_site_packages_dir () { test_lssitepackages () { mkvirtualenv "lssitepackagestest" >/dev/null 2>&1 - contents="$(lssitepackages)" - assertTrue "did not find easy_install in site-packages" "echo $contents | grep -q easy_install" + contents="$(lssitepackages)" + assertTrue "did not find pip in site-packages: ${contents}" "echo $contents | grep -q pip" deactivate } @@ -36,8 +36,8 @@ test_lssitepackages_space_in_name () { # Only test with leading and internal spaces. Directory names with trailing spaces are legal, # and work with virtualenv on OSX, but error out on Linux. mkvirtualenv " space lssitepackagestest" >/dev/null 2>&1 - contents="$(lssitepackages)" - assertTrue "did not find easy_install in site-packages" "echo $contents | grep -q easy_install" + contents="$(lssitepackages)" + assertTrue "did not find pip in site-packages: ${contents}" "echo $contents | grep -q pip" deactivate } @@ -46,7 +46,7 @@ test_lssitepackages_add2virtualenv () { parent_dir=$(dirname $(pwd)) base_dir=$(basename $(pwd)) add2virtualenv "../$base_dir" - contents="$(lssitepackages)" + contents="$(lssitepackages)" actual=$(echo $contents | grep $base_dir) expected=$(echo $contents) assertSame "$expected" "$actual" diff --git a/tests/test_mktmpenv.sh b/tests/test_mktmpenv.sh index c8989bc..6a1171d 100755 --- a/tests/test_mktmpenv.sh +++ b/tests/test_mktmpenv.sh @@ -43,9 +43,9 @@ test_mktmpenv_no_cd() { } test_mktmpenv_virtualenv_args() { - mktmpenv --no-site-packages >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist" "[ -f \"$ngsp_file\" ]" + mktmpenv --without-pip >/dev/null 2>&1 + contents="$(lssitepackages)" + assertFalse "found pip in site-packages: ${contents}" "echo $contents | grep -q pip" } test_deactivate() { diff --git a/tests/test_mkvirtualenv.sh b/tests/test_mkvirtualenv.sh index bbfd543..3c37dfe 100755 --- a/tests/test_mkvirtualenv.sh +++ b/tests/test_mkvirtualenv.sh @@ -104,23 +104,6 @@ test_no_workon_home () { WORKON_HOME="$old_home" } -test_mkvirtualenv_sitepackages () { - # This part of the test is not reliable because - # creating a new virtualenv from inside the - # tox virtualenv inherits the setting from there. -# # Without the option, verify that site-packages are copied. -# mkvirtualenv "with_sp" >/dev/null 2>&1 -# ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" -# assertFalse "$ngsp_file exists" "[ -f \"$ngsp_file\" ]" -# rmvirtualenv "env3" - - # With the argument, verify that they are not copied. - mkvirtualenv --no-site-packages "without_sp" >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist" "[ -f \"$ngsp_file\" ]" - rmvirtualenv "env4" >/dev/null 2>&1 -} - test_mkvirtualenv_hooks_system_site_packages () { # See issue #189 @@ -143,12 +126,14 @@ GLOBAL postmkvirtualenv" test_mkvirtualenv_args () { # See issue #102 - VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--no-site-packages" + VIRTUALENVWRAPPER_VIRTUALENV_ARGS="--without-pip" # With the argument, verify that they are not copied. - mkvirtualenv "without_sp2" >/dev/null 2>&1 - ngsp_file="`virtualenvwrapper_get_site_packages_dir`/../no-global-site-packages.txt" - assertTrue "$ngsp_file does not exist" "[ -f \"$ngsp_file\" ]" - rmvirtualenv "env4" >/dev/null 2>&1 + mkvirtualenv "without_pip" >/dev/null 2>&1 + local RC=$? + assertTrue "mkvirtualenv failed" "[ $RC -eq 0 ]" + contents="$(lssitepackages)" + assertFalse "found pip in site-packages: ${contents}" "echo $contents | grep -q pip" + rmvirtualenv "without_pip" >/dev/null 2>&1 unset VIRTUALENVWRAPPER_VIRTUALENV_ARGS } diff --git a/tests/test_run_hook.sh b/tests/test_run_hook.sh index 2d6983d..e1e8644 100755 --- a/tests/test_run_hook.sh +++ b/tests/test_run_hook.sh @@ -57,7 +57,7 @@ test_virtualenvwrapper_run_hook_permissions() { echo "echo run $@ >> \"$TMPDIR/catch_output\"" >> "$WORKON_HOME/prermvirtualenv" chmod 0444 "$WORKON_HOME/prermvirtualenv" touch "$TMPDIR/catch_output" - error=$(virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo" 2>&1 | grep "could not run" | cut -f2- -d'[') + error=$(virtualenvwrapper_run_hook "pre_rmvirtualenv" "foo" 2>&1 | grep "could not run" | cut -f2- -d'[' | cut -f1 -d:) output=$(cat "$TMPDIR/catch_output") expected="" assertSame "$expected" "$output" diff --git a/tests/test_tempfile.sh b/tests/test_tempfile.sh index 9fb6b79..be1f89d 100755 --- a/tests/test_tempfile.sh +++ b/tests/test_tempfile.sh @@ -21,6 +21,11 @@ setUp () { } test_tempfile () { + if [ "$(uname)" = "Darwin" ]; then + # macOS doesn't seem to allow controlling where mktemp creates + # the output files + return 0 + fi filename=$(virtualenvwrapper_tempfile hook) assertTrue "Filename is empty" "[ ! -z \"$filename\" ]" assertTrue "File doesn't exist" "[ -f \"$filename\" ]" @@ -52,6 +57,11 @@ test_bad_mktemp() { } test_no_such_tmpdir () { + if [ "$(uname)" = "Darwin" ]; then + # macOS doesn't seem to allow controlling where mktemp creates + # the output files + return 0 + fi old_tmpdir="$TMPDIR" export TMPDIR="$TMPDIR/does-not-exist" virtualenvwrapper_run_hook "initialize" >/dev/null 2>&1 @@ -61,6 +71,11 @@ test_no_such_tmpdir () { } test_tmpdir_not_writable () { + if [ "$(uname)" = "Darwin" ]; then + # macOS doesn't seem to allow controlling where mktemp creates + # the output files + return 0 + fi old_tmpdir="$TMPDIR" export TMPDIR="$TMPDIR/cannot-write" mkdir "$TMPDIR" diff --git a/tests/test_toggleglobalsitepackages.sh b/tests/test_toggleglobalsitepackages.sh deleted file mode 100755 index c1d3d82..0000000 --- a/tests/test_toggleglobalsitepackages.sh +++ /dev/null @@ -1,43 +0,0 @@ -# -*- mode: shell-script -*- - -test_dir=$(cd $(dirname $0) && pwd) -source "$test_dir/setup.sh" - -oneTimeSetUp() { - rm -rf "$WORKON_HOME" - mkdir -p "$WORKON_HOME" - source "$test_dir/../virtualenvwrapper.sh" -} - -oneTimeTearDown() { - rm -rf "$WORKON_HOME" -} - -setUp () { - echo -} - -tearDown () { - deactivate >/dev/null 2>&1 -} - -test_toggleglobalsitepackages () { - mkvirtualenv --no-site-packages "test1" - ngsp_file=$(dirname "`virtualenvwrapper_get_site_packages_dir`")/no-global-site-packages.txt - assertTrue "$ngsp_file does not exist 1" "[ -f \"$ngsp_file\" ]" - toggleglobalsitepackages -q - assertFalse "$ngsp_file exists" "[ -f \"$ngsp_file\" ]" - toggleglobalsitepackages -q - assertTrue "$ngsp_file does not exist 2" "[ -f \"$ngsp_file\" ]" -} - -test_toggleglobalsitepackages_quiet () { - mkvirtualenv --no-site-packages "test2" - 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" diff --git a/tests/test_wipeenv.sh b/tests/test_wipeenv.sh index 2dbd9cf..9b8895c 100755 --- a/tests/test_wipeenv.sh +++ b/tests/test_wipeenv.sh @@ -32,10 +32,10 @@ test_wipeenv_pip_e () { mkvirtualenv "wipetest" >/dev/null 2>&1 (cd tests/testpackage && pip install -e .) >/dev/null 2>&1 before="$(pip freeze)" - assertTrue "testpackage not installed" "pip freeze | grep testpackage" + assertTrue "testpackage not installed: $before" "pip freeze | grep testpackage" wipeenv >/dev/null 2>&1 after="$(pip freeze)" - assertFalse "testpackage still installed" "pip freeze | grep testpackage" + assertFalse "testpackage still installed: $after" "pip freeze | grep testpackage" } # test_wipeenv_pip_e_url () { diff --git a/tox.ini b/tox.ini index f8b2751..c97b5ef 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,13 @@ [tox] -envlist = py27,py33,py34,py35,py36,zsh,ksh,style +envlist = py,zsh,style [testenv] install_command = pip install -U {opts} {packages} commands = bash ./tests/run_tests {envdir} [] deps = -rrequirements.txt +pass_env = + HOME + USER setenv = TOXIC = true SHELL = /bin/bash @@ -13,12 +16,17 @@ allowlist_externals = zsh ksh +[testenv:fast] +setenv = + TOXIC = true + SHELL = /bin/bash + FAIL_FAST = true + [testenv:style] deps = flake8 commands = flake8 virtualenvwrapper docs/source/conf.py [testenv:zsh] -basepython=python2.7 setenv = SHELL = /bin/zsh test_shell_opts = -o shwordsplit diff --git a/virtualenvwrapper.sh b/virtualenvwrapper.sh index edf0313..79def0d 100644 --- a/virtualenvwrapper.sh +++ b/virtualenvwrapper.sh @@ -948,22 +948,6 @@ function lssitepackages { fi } -# Toggles the currently-active virtualenv between having and not having -# access to the global site-packages. -#:help:toggleglobalsitepackages: turn access to global site-packages on/off -function 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 -} - #:help:cpvirtualenv: duplicate the named virtualenv to make a new one function cpvirtualenv { virtualenvwrapper_verify_workon_home || return 1 @@ -1310,9 +1294,28 @@ function wipeenv { if [ -n "$(cat "$req_file")" ] then echo "Uninstalling packages:" - cat "$req_file" echo - pip uninstall -y $(cat "$req_file" | grep -v '^-f' | sed 's/>/=/g' | cut -f1 -d=) + while read line; do + typeset pkg="" + if [[ "$line" =~ ^-f ]]; then + # ignore lines starting -f which pip sometimes + # includes and that do not point to specific + # dependencies + continue + fi + if [[ "$line" =~ ^-e ]]; then + # fix lines pointing to editable packages, which look like: + # -e git+ssh://git@github.com/python-virtualenvwrapper/virtualenvwrapper.git@1dc9e5f52102f0133b804c0c8a6b76c55db908bf#egg=testpackage&subdirectory=tests/testpackage + # and parse out the egg name to pass to pip + pkg=$(echo "$line" | cut -f2 -d' ' | sed -e 's|&subdirectory.*||g' -e 's|.*egg=||g') + else + # Strip version specifiers off of the end of the line + # to keep only the package name. + pkg=$(echo "$line" | sed -e 's/[<>!=].*//g') + fi + echo $pkg + pip uninstall -y "$pkg" + done < "$req_file" else echo "Nothing to remove." fi