diff options
Diffstat (limited to 'setuptools/tests/test_easy_install.py')
| -rw-r--r-- | setuptools/tests/test_easy_install.py | 150 |
1 files changed, 59 insertions, 91 deletions
diff --git a/setuptools/tests/test_easy_install.py b/setuptools/tests/test_easy_install.py index 189e3d55..a90ae23f 100644 --- a/setuptools/tests/test_easy_install.py +++ b/setuptools/tests/test_easy_install.py @@ -6,32 +6,22 @@ import shutil import tempfile import unittest import site -from setuptools.compat import StringIO, BytesIO, next -from setuptools.compat import urlparse +import contextlib import textwrap import tarfile +import logging import distutils.core from setuptools.compat import StringIO, BytesIO, next, urlparse from setuptools.sandbox import run_setup, SandboxViolation -from setuptools.command.easy_install import easy_install, fix_jython_executable, get_script_args, nt_quote_arg -from setuptools.command.easy_install import PthDistributions +from setuptools.command.easy_install import ( + easy_install, fix_jython_executable, get_script_args, nt_quote_arg) +from setuptools.command.easy_install import PthDistributions from setuptools.command import easy_install as easy_install_pkg from setuptools.dist import Distribution from pkg_resources import Distribution as PRDistribution import setuptools.tests.server -try: - # import multiprocessing solely for the purpose of testing its existence - __import__('multiprocessing') - import logging - _LOG = logging.getLogger('test_easy_install') - logging.basicConfig(level=logging.INFO, stream=sys.stderr) - _MULTIPROC = True -except ImportError: - _MULTIPROC = False - _LOG = None - class FakeDist(object): def get_entry_map(self, group): if group != 'console_scripts': @@ -140,31 +130,29 @@ class TestUserInstallTest(unittest.TestCase): f.close() self.old_cwd = os.getcwd() os.chdir(self.dir) - if sys.version >= "2.6": - self.old_has_site = easy_install_pkg.HAS_USER_SITE - self.old_file = easy_install_pkg.__file__ - self.old_base = site.USER_BASE - site.USER_BASE = tempfile.mkdtemp() - self.old_site = site.USER_SITE - site.USER_SITE = tempfile.mkdtemp() - easy_install_pkg.__file__ = site.USER_SITE + + self.old_enable_site = site.ENABLE_USER_SITE + self.old_file = easy_install_pkg.__file__ + self.old_base = site.USER_BASE + site.USER_BASE = tempfile.mkdtemp() + self.old_site = site.USER_SITE + site.USER_SITE = tempfile.mkdtemp() + easy_install_pkg.__file__ = site.USER_SITE def tearDown(self): os.chdir(self.old_cwd) shutil.rmtree(self.dir) - if sys.version >= "2.6": - shutil.rmtree(site.USER_BASE) - shutil.rmtree(site.USER_SITE) - site.USER_BASE = self.old_base - site.USER_SITE = self.old_site - easy_install_pkg.HAS_USER_SITE = self.old_has_site - easy_install_pkg.__file__ = self.old_file + + shutil.rmtree(site.USER_BASE) + shutil.rmtree(site.USER_SITE) + site.USER_BASE = self.old_base + site.USER_SITE = self.old_site + site.ENABLE_USER_SITE = self.old_enable_site + easy_install_pkg.__file__ = self.old_file def test_user_install_implied(self): - easy_install_pkg.HAS_USER_SITE = True # disabled sometimes + site.ENABLE_USER_SITE = True # disabled sometimes #XXX: replace with something meaningfull - if sys.version < "2.6": - return #SKIP dist = Distribution() dist.script_name = 'setup.py' cmd = easy_install(dist) @@ -173,15 +161,19 @@ class TestUserInstallTest(unittest.TestCase): self.assertTrue(cmd.user, 'user should be implied') def test_multiproc_atexit(self): - if not _MULTIPROC: + try: + __import__('multiprocessing') + except ImportError: + # skip the test if multiprocessing is not available return - _LOG.info('this should not break') + + log = logging.getLogger('test_easy_install') + logging.basicConfig(level=logging.INFO, stream=sys.stderr) + log.info('this should not break') def test_user_install_not_implied_without_usersite_enabled(self): - easy_install_pkg.HAS_USER_SITE = False # usually enabled + site.ENABLE_USER_SITE = False # usually enabled #XXX: replace with something meaningfull - if sys.version < "2.6": - return #SKIP dist = Distribution() dist.script_name = 'setup.py' cmd = easy_install(dist) @@ -246,7 +238,6 @@ class TestUserInstallTest(unittest.TestCase): test_pkg = os.path.join(self.dir, 'test_pkg') test_setup_py = os.path.join(test_pkg, 'setup.py') - test_setup_cfg = os.path.join(test_pkg, 'setup.cfg') os.mkdir(test_pkg) f = open(test_setup_py, 'w') @@ -273,9 +264,8 @@ class TestUserInstallTest(unittest.TestCase): sys.stderr = StringIO() try: try: - reset_setup_stop_context( - lambda: run_setup(test_setup_py, ['install']) - ) + with reset_setup_stop_context(): + run_setup(test_setup_py, ['install']) except SandboxViolation: self.fail('Installation caused SandboxViolation') finally: @@ -300,45 +290,33 @@ class TestSetupRequires(unittest.TestCase): # Some platforms (Jython) don't find a port to which to bind, # so skip this test for them. return - - # I realize this is all-but-impossible to read, because it was - # ported from some well-factored, safe code using 'with'. If you - # need to maintain this code, consider making the changes in - # the parent revision (of this comment) and then port the changes - # back for Python 2.4 (or deprecate Python 2.4). - - def install(dist_file): - def install_at(temp_install_dir): - def install_env(): + # create an sdist that has a build-time dependency. + with TestSetupRequires.create_sdist() as dist_file: + with tempdir_context() as temp_install_dir: + with environment_context(PYTHONPATH=temp_install_dir): ei_params = ['--index-url', p_index.url, '--allow-hosts', p_index_loc, '--exclude-scripts', '--install-dir', temp_install_dir, dist_file] - def install_clean_reset(): - def install_clean_argv(): + with reset_setup_stop_context(): + with argv_context(['easy_install']): # attempt to install the dist. It should fail because # it doesn't exist. self.assertRaises(SystemExit, easy_install_pkg.main, ei_params) - argv_context(install_clean_argv, ['easy_install']) - reset_setup_stop_context(install_clean_reset) - environment_context(install_env, PYTHONPATH=temp_install_dir) - tempdir_context(install_at) - - # create an sdist that has a build-time dependency. - self.create_sdist(install) - # there should have been two or three requests to the server # (three happens on Python 3.3a) self.assertTrue(2 <= len(p_index.requests) <= 3) self.assertEqual(p_index.requests[0].path, '/does-not-exist/') - def create_sdist(self, installer): + @staticmethod + @contextlib.contextmanager + def create_sdist(): """ - Create an sdist with a setup_requires dependency (of something that - doesn't exist) and invoke installer on it. + Return an sdist with a setup_requires dependency (of something that + doesn't exist) """ - def build_sdist(dir): + with tempdir_context() as dir: dist_path = os.path.join(dir, 'setuptools-test-fetcher-1.0.tar.gz') make_trivial_sdist( dist_path, @@ -350,8 +328,7 @@ class TestSetupRequires(unittest.TestCase): setup_requires = ['does-not-exist'], ) """).lstrip()) - installer(dist_path) - tempdir_context(build_sdist) + yield dist_path def make_trivial_sdist(dist_path, setup_py): @@ -374,44 +351,37 @@ def make_trivial_sdist(dist_path, setup_py): dist.close() -def tempdir_context(f, cd=lambda dir:None): - """ - Invoke f in the context - """ +@contextlib.contextmanager +def tempdir_context(cd=lambda dir:None): temp_dir = tempfile.mkdtemp() orig_dir = os.getcwd() try: cd(temp_dir) - f(temp_dir) + yield temp_dir finally: cd(orig_dir) shutil.rmtree(temp_dir) -def environment_context(f, **updates): - """ - Invoke f in the context - """ +@contextlib.contextmanager +def environment_context(**updates): old_env = os.environ.copy() os.environ.update(updates) try: - f() + yield finally: for key in updates: del os.environ[key] os.environ.update(old_env) -def argv_context(f, repl): - """ - Invoke f in the context - """ +@contextlib.contextmanager +def argv_context(repl): old_argv = sys.argv[:] sys.argv[:] = repl - try: - f() - finally: - sys.argv[:] = old_argv + yield + sys.argv[:] = old_argv -def reset_setup_stop_context(f): +@contextlib.contextmanager +def reset_setup_stop_context(): """ When the setuptools tests are run using setup.py test, and then one wants to invoke another setup() command (such as easy_install) @@ -420,7 +390,5 @@ def reset_setup_stop_context(f): """ setup_stop_after = distutils.core._setup_stop_after distutils.core._setup_stop_after = None - try: - f() - finally: - distutils.core._setup_stop_after = setup_stop_after + yield + distutils.core._setup_stop_after = setup_stop_after |
