Commit e7266809 authored by Jason R. Coombs's avatar Jason R. Coombs

Extract context manager for project_on_sys_path in test command

parent 7dadb206
import sys
import contextlib
from distutils.errors import DistutilsOptionError from distutils.errors import DistutilsOptionError
from unittest import TestLoader from unittest import TestLoader
import sys
from setuptools.extern import six from setuptools.extern import six
from setuptools.extern.six.moves import map from setuptools.extern.six.moves import map
...@@ -102,6 +103,14 @@ class test(Command): ...@@ -102,6 +103,14 @@ class test(Command):
yield self.test_suite yield self.test_suite
def with_project_on_sys_path(self, func): def with_project_on_sys_path(self, func):
"""
Backward compatibility for project_on_sys_path context.
"""
with self.project_on_sys_path():
func()
@contextlib.contextmanager
def project_on_sys_path(self):
with_2to3 = six.PY3 and getattr(self.distribution, 'use_2to3', False) with_2to3 = six.PY3 and getattr(self.distribution, 'use_2to3', False)
if with_2to3: if with_2to3:
...@@ -137,7 +146,7 @@ class test(Command): ...@@ -137,7 +146,7 @@ class test(Command):
working_set.__init__() working_set.__init__()
add_activation_listener(lambda dist: dist.activate()) add_activation_listener(lambda dist: dist.activate())
require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version)) require('%s==%s' % (ei_cmd.egg_name, ei_cmd.egg_version))
func() yield
finally: finally:
sys.path[:] = old_path sys.path[:] = old_path
sys.modules.clear() sys.modules.clear()
...@@ -154,9 +163,11 @@ class test(Command): ...@@ -154,9 +163,11 @@ class test(Command):
cmd = ' '.join(self._argv) cmd = ' '.join(self._argv)
if self.dry_run: if self.dry_run:
self.announce('skipping "%s" (dry run)' % cmd) self.announce('skipping "%s" (dry run)' % cmd)
else: return
self.announce('running "%s"' % cmd) self.announce('running "%s"' % cmd)
self.with_project_on_sys_path(self.run_tests) with self.project_on_sys_path():
self.run_tests()
def run_tests(self): def run_tests(self):
# Purge modules under test from sys.modules. The test loader will # Purge modules under test from sys.modules. The test loader will
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment