summaryrefslogtreecommitdiff
path: root/setuptools/command/test.py
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2016-09-27 14:24:22 -0500
committerJason R. Coombs <jaraco@jaraco.com>2016-09-27 14:24:22 -0500
commit66a6724da8eda3336643dee086da2a3495e6422a (patch)
tree64043e9782491bde3a3a9ae2314cc59451a6c9c0 /setuptools/command/test.py
parentdf3905616933c90af95e99f705b800a2f5c1c921 (diff)
parent35ea365b50bd1a64375fdbcce187affab22af3b7 (diff)
downloadpython-setuptools-git-setuptools-scm.tar.gz
Merge with mastersetuptools-scm
Diffstat (limited to 'setuptools/command/test.py')
-rw-r--r--setuptools/command/test.py64
1 files changed, 53 insertions, 11 deletions
diff --git a/setuptools/command/test.py b/setuptools/command/test.py
index 39746a02..38bbcd8b 100644
--- a/setuptools/command/test.py
+++ b/setuptools/command/test.py
@@ -1,10 +1,13 @@
+import os
+import operator
import sys
import contextlib
+import itertools
from distutils.errors import DistutilsOptionError
from unittest import TestLoader
from setuptools.extern import six
-from setuptools.extern.six.moves import map
+from setuptools.extern.six.moves import map, filter
from pkg_resources import (resource_listdir, resource_exists, normalize_path,
working_set, _namespace_packages,
@@ -14,6 +17,7 @@ from setuptools.py31compat import unittest_main
class ScanningLoader(TestLoader):
+
def loadTestsFromModule(self, module, pattern=None):
"""Return a suite of all tests cases contained in the given module
@@ -46,6 +50,7 @@ class ScanningLoader(TestLoader):
# adapted from jaraco.classes.properties:NonDataProperty
class NonDataProperty(object):
+
def __init__(self, fget):
self.fget = fget
@@ -110,7 +115,7 @@ class test(Command):
func()
@contextlib.contextmanager
- def project_on_sys_path(self):
+ def project_on_sys_path(self, include_dists=[]):
with_2to3 = six.PY3 and getattr(self.distribution, 'use_2to3', False)
if with_2to3:
@@ -142,23 +147,57 @@ class test(Command):
old_modules = sys.modules.copy()
try:
- sys.path.insert(0, normalize_path(ei_cmd.egg_base))
+ project_path = normalize_path(ei_cmd.egg_base)
+ sys.path.insert(0, project_path)
working_set.__init__()
add_activation_listener(lambda dist: dist.activate())
require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
- yield
+ with self.paths_on_pythonpath([project_path]):
+ yield
finally:
sys.path[:] = old_path
sys.modules.clear()
sys.modules.update(old_modules)
working_set.__init__()
+ @staticmethod
+ @contextlib.contextmanager
+ def paths_on_pythonpath(paths):
+ """
+ Add the indicated paths to the head of the PYTHONPATH environment
+ variable so that subprocesses will also see the packages at
+ these paths.
+
+ Do this in a context that restores the value on exit.
+ """
+ nothing = object()
+ orig_pythonpath = os.environ.get('PYTHONPATH', nothing)
+ current_pythonpath = os.environ.get('PYTHONPATH', '')
+ try:
+ prefix = os.pathsep.join(paths)
+ to_join = filter(None, [prefix, current_pythonpath])
+ new_path = os.pathsep.join(to_join)
+ if new_path:
+ os.environ['PYTHONPATH'] = new_path
+ yield
+ finally:
+ if orig_pythonpath is nothing:
+ os.environ.pop('PYTHONPATH', None)
+ else:
+ os.environ['PYTHONPATH'] = orig_pythonpath
+
+ @staticmethod
+ def install_dists(dist):
+ """
+ Install the requirements indicated by self.distribution and
+ return an iterable of the dists that were built.
+ """
+ ir_d = dist.fetch_build_eggs(dist.install_requires or [])
+ tr_d = dist.fetch_build_eggs(dist.tests_require or [])
+ return itertools.chain(ir_d, tr_d)
+
def run(self):
- if self.distribution.install_requires:
- self.distribution.fetch_build_eggs(
- self.distribution.install_requires)
- if self.distribution.tests_require:
- self.distribution.fetch_build_eggs(self.distribution.tests_require)
+ installed_dists = self.install_dists(self.distribution)
cmd = ' '.join(self._argv)
if self.dry_run:
@@ -166,8 +205,11 @@ class test(Command):
return
self.announce('running "%s"' % cmd)
- with self.project_on_sys_path():
- self.run_tests()
+
+ paths = map(operator.attrgetter('location'), installed_dists)
+ with self.paths_on_pythonpath(paths):
+ with self.project_on_sys_path():
+ self.run_tests()
def run_tests(self):
# Purge modules under test from sys.modules. The test loader will