Skip to content

Commit

Permalink
Remove tests reliant on sandbox.run_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Nov 26, 2021
1 parent b19d64f commit 1da50ce
Showing 1 changed file with 2 additions and 364 deletions.
366 changes: 2 additions & 364 deletions setuptools/tests/test_easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import tempfile
import site
import contextlib
import tarfile
import logging
import itertools
Expand All @@ -15,21 +14,18 @@
import mock
import time
import re
import pathlib

import pytest
from jaraco import path

from setuptools.sandbox import run_setup
import setuptools.command.easy_install as ei
from setuptools.command.easy_install import (
EasyInstallDeprecationWarning, ScriptWriter, PthDistributions,
WindowsScriptWriter,
)
from setuptools.dist import Distribution
from pkg_resources import normalize_path, working_set
from pkg_resources import normalize_path
from pkg_resources import Distribution as PRDistribution
from setuptools.tests.server import MockServer, path_to_url
from setuptools.tests.server import MockServer
import pkg_resources

from . import contexts
Expand Down Expand Up @@ -369,364 +365,6 @@ def mock_index():
return p_index


class TestDistutilsPackage:
def test_bdist_egg_available_on_distutils_pkg(self, distutils_package):
run_setup('setup.py', ['bdist_egg'])


class TestSetupRequires:

@staticmethod
@contextlib.contextmanager
def create_sdist():
"""
Return an sdist with a setup_requires dependency (of something that
doesn't exist)
"""
with contexts.tempdir() as dir:
dist_path = os.path.join(dir, 'setuptools-test-fetcher-1.0.tar.gz')
make_sdist(dist_path, [
('setup.py', DALS("""
import setuptools
setuptools.setup(
name="setuptools-test-fetcher",
version="1.0",
setup_requires = ['does-not-exist'],
)
""")),
('setup.cfg', ''),
])
yield dist_path

use_setup_cfg = (
(),
('dependency_links',),
('setup_requires',),
('dependency_links', 'setup_requires'),
)

@pytest.mark.parametrize('use_setup_cfg', use_setup_cfg)
def test_setup_requires_overrides_version_conflict(self, use_setup_cfg):
"""
Regression test for distribution issue 323:
https://bitbucket.org/tarek/distribute/issues/323
Ensures that a distribution's setup_requires requirements can still be
installed and used locally even if a conflicting version of that
requirement is already on the path.
"""

fake_dist = PRDistribution('does-not-matter', project_name='foobar',
version='0.0')
working_set.add(fake_dist)

with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
test_pkg = create_setup_requires_package(
temp_dir, use_setup_cfg=use_setup_cfg)
test_setup_py = os.path.join(test_pkg, 'setup.py')
with contexts.quiet() as (stdout, stderr):
# Don't even need to install the package, just
# running the setup.py at all is sufficient
run_setup(test_setup_py, [str('--name')])

lines = stdout.readlines()
assert len(lines) > 0
assert lines[-1].strip() == 'test_pkg'

@pytest.mark.parametrize('use_setup_cfg', use_setup_cfg)
def test_setup_requires_override_nspkg(self, use_setup_cfg):
"""
Like ``test_setup_requires_overrides_version_conflict`` but where the
``setup_requires`` package is part of a namespace package that has
*already* been imported.
"""

with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
foobar_1_archive = os.path.join(temp_dir, 'foo.bar-0.1.tar.gz')
make_nspkg_sdist(foobar_1_archive, 'foo.bar', '0.1')
# Now actually go ahead an extract to the temp dir and add the
# extracted path to sys.path so foo.bar v0.1 is importable
foobar_1_dir = os.path.join(temp_dir, 'foo.bar-0.1')
os.mkdir(foobar_1_dir)
with tarfile.open(foobar_1_archive) as tf:
tf.extractall(foobar_1_dir)
sys.path.insert(1, foobar_1_dir)

dist = PRDistribution(foobar_1_dir, project_name='foo.bar',
version='0.1')
working_set.add(dist)

template = DALS("""\
import foo # Even with foo imported first the
# setup_requires package should override
import setuptools
setuptools.setup(**%r)
if not (hasattr(foo, '__path__') and
len(foo.__path__) == 2):
print('FAIL')
if 'foo.bar-0.2' not in foo.__path__[0]:
print('FAIL')
""")

test_pkg = create_setup_requires_package(
temp_dir, 'foo.bar', '0.2', make_nspkg_sdist, template,
use_setup_cfg=use_setup_cfg)

test_setup_py = os.path.join(test_pkg, 'setup.py')

with contexts.quiet() as (stdout, stderr):
try:
# Don't even need to install the package, just
# running the setup.py at all is sufficient
run_setup(test_setup_py, [str('--name')])
except pkg_resources.VersionConflict:
self.fail(
'Installing setup.py requirements '
'caused a VersionConflict')

assert 'FAIL' not in stdout.getvalue()
lines = stdout.readlines()
assert len(lines) > 0
assert lines[-1].strip() == 'test_pkg'

@pytest.mark.parametrize('use_setup_cfg', use_setup_cfg)
def test_setup_requires_with_attr_version(self, use_setup_cfg):
def make_dependency_sdist(dist_path, distname, version):
files = [(
'setup.py',
DALS("""
import setuptools
setuptools.setup(
name={name!r},
version={version!r},
py_modules=[{name!r}],
)
""".format(name=distname, version=version)),
), (
distname + '.py',
DALS("""
version = 42
"""),
)]
make_sdist(dist_path, files)
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
test_pkg = create_setup_requires_package(
temp_dir, setup_attrs=dict(version='attr: foobar.version'),
make_package=make_dependency_sdist,
use_setup_cfg=use_setup_cfg + ('version',),
)
test_setup_py = os.path.join(test_pkg, 'setup.py')
with contexts.quiet() as (stdout, stderr):
run_setup(test_setup_py, [str('--version')])
lines = stdout.readlines()
assert len(lines) > 0
assert lines[-1].strip() == '42'

def test_setup_requires_honors_pip_env(self, mock_index, monkeypatch):
monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
monkeypatch.setenv('PIP_NO_INDEX', 'false')
monkeypatch.setenv(str('PIP_INDEX_URL'), mock_index.url)
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
test_pkg = create_setup_requires_package(
temp_dir, 'python-xlib', '0.19',
setup_attrs=dict(dependency_links=[]))
test_setup_cfg = os.path.join(test_pkg, 'setup.cfg')
with open(test_setup_cfg, 'w') as fp:
fp.write(DALS(
'''
[easy_install]
index_url = https://pypi.org/legacy/
'''))
test_setup_py = os.path.join(test_pkg, 'setup.py')
with pytest.raises(distutils.errors.DistutilsError):
run_setup(test_setup_py, [str('--version')])
assert len(mock_index.requests) == 1
assert mock_index.requests[0].path == '/python-xlib/'

def test_setup_requires_with_pep508_url(self, mock_index, monkeypatch):
monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
monkeypatch.setenv(str('PIP_INDEX_URL'), mock_index.url)
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
dep_sdist = os.path.join(temp_dir, 'dep.tar.gz')
make_trivial_sdist(dep_sdist, 'dependency', '42')
dep_url = path_to_url(dep_sdist, authority='localhost')
test_pkg = create_setup_requires_package(
temp_dir,
# Ignored (overridden by setup_attrs)
'python-xlib', '0.19',
setup_attrs=dict(
setup_requires='dependency @ %s' % dep_url))
test_setup_py = os.path.join(test_pkg, 'setup.py')
run_setup(test_setup_py, [str('--version')])
assert len(mock_index.requests) == 0

def test_setup_requires_with_allow_hosts(self, mock_index):
''' The `allow-hosts` option in not supported anymore. '''
files = {
'test_pkg': {
'setup.py': DALS('''
from setuptools import setup
setup(setup_requires='python-xlib')
'''),
'setup.cfg': DALS('''
[easy_install]
allow_hosts = *
'''),
}
}
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
path.build(files, prefix=temp_dir)
setup_py = str(pathlib.Path(temp_dir, 'test_pkg', 'setup.py'))
with pytest.raises(distutils.errors.DistutilsError):
run_setup(setup_py, [str('--version')])
assert len(mock_index.requests) == 0

def test_setup_requires_with_python_requires(self, monkeypatch, tmpdir):
''' Check `python_requires` is honored. '''
monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
monkeypatch.setenv(str('PIP_NO_INDEX'), str('1'))
monkeypatch.setenv(str('PIP_VERBOSE'), str('1'))
dep_1_0_sdist = 'dep-1.0.tar.gz'
dep_1_0_url = path_to_url(str(tmpdir / dep_1_0_sdist))
dep_1_0_python_requires = '>=2.7'
make_python_requires_sdist(
str(tmpdir / dep_1_0_sdist), 'dep', '1.0', dep_1_0_python_requires)
dep_2_0_sdist = 'dep-2.0.tar.gz'
dep_2_0_url = path_to_url(str(tmpdir / dep_2_0_sdist))
dep_2_0_python_requires = '!=' + '.'.join(
map(str, sys.version_info[:2])) + '.*'
make_python_requires_sdist(
str(tmpdir / dep_2_0_sdist), 'dep', '2.0', dep_2_0_python_requires)
index = tmpdir / 'index.html'
index.write_text(DALS(
'''
<!DOCTYPE html>
<html><head><title>Links for dep</title></head>
<body>
<h1>Links for dep</h1>
<a href="{dep_1_0_url}" data-requires-python="{dep_1_0_python_requires}">{dep_1_0_sdist}</a><br/>
<a href="{dep_2_0_url}" data-requires-python="{dep_2_0_python_requires}">{dep_2_0_sdist}</a><br/>
</body>
</html>
''').format( # noqa
dep_1_0_url=dep_1_0_url,
dep_1_0_sdist=dep_1_0_sdist,
dep_1_0_python_requires=dep_1_0_python_requires,
dep_2_0_url=dep_2_0_url,
dep_2_0_sdist=dep_2_0_sdist,
dep_2_0_python_requires=dep_2_0_python_requires,
), 'utf-8')
index_url = path_to_url(str(index))
with contexts.save_pkg_resources_state():
test_pkg = create_setup_requires_package(
str(tmpdir),
'python-xlib', '0.19', # Ignored (overridden by setup_attrs).
setup_attrs=dict(
setup_requires='dep', dependency_links=[index_url]))
test_setup_py = os.path.join(test_pkg, 'setup.py')
run_setup(test_setup_py, [str('--version')])
eggs = list(map(str, pkg_resources.find_distributions(
os.path.join(test_pkg, '.eggs'))))
assert eggs == ['dep 1.0']

@pytest.mark.parametrize(
'with_dependency_links_in_setup_py',
(False, True))
def test_setup_requires_with_find_links_in_setup_cfg(
self, monkeypatch,
with_dependency_links_in_setup_py):
monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
make_trivial_sdist(
os.path.join(temp_dir, 'python-xlib-42.tar.gz'),
'python-xlib',
'42')
test_pkg = os.path.join(temp_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)
with open(test_setup_py, 'w') as fp:
if with_dependency_links_in_setup_py:
dependency_links = [os.path.join(temp_dir, 'links')]
else:
dependency_links = []
fp.write(DALS(
'''
from setuptools import installer, setup
setup(setup_requires='python-xlib==42',
dependency_links={dependency_links!r})
''').format(
dependency_links=dependency_links))
with open(test_setup_cfg, 'w') as fp:
fp.write(DALS(
'''
[easy_install]
index_url = {index_url}
find_links = {find_links}
''').format(index_url=os.path.join(temp_dir, 'index'),
find_links=temp_dir))
run_setup(test_setup_py, [str('--version')])

def test_setup_requires_with_transitive_extra_dependency(
self, monkeypatch):
# Use case: installing a package with a build dependency on
# an already installed `dep[extra]`, which in turn depends
# on `extra_dep` (whose is not already installed).
with contexts.save_pkg_resources_state():
with contexts.tempdir() as temp_dir:
# Create source distribution for `extra_dep`.
make_trivial_sdist(
os.path.join(temp_dir, 'extra_dep-1.0.tar.gz'),
'extra_dep', '1.0')
# Create source tree for `dep`.
dep_pkg = os.path.join(temp_dir, 'dep')
os.mkdir(dep_pkg)
path.build({
'setup.py':
DALS("""
import setuptools
setuptools.setup(
name='dep', version='2.0',
extras_require={'extra': ['extra_dep']},
)
"""),
'setup.cfg': '',
}, prefix=dep_pkg)
# "Install" dep.
run_setup(
os.path.join(dep_pkg, 'setup.py'), [str('dist_info')])
working_set.add_entry(dep_pkg)
# Create source tree for test package.
test_pkg = os.path.join(temp_dir, 'test_pkg')
test_setup_py = os.path.join(test_pkg, 'setup.py')
os.mkdir(test_pkg)
with open(test_setup_py, 'w') as fp:
fp.write(DALS(
'''
from setuptools import installer, setup
setup(setup_requires='dep[extra]')
'''))
# Check...
monkeypatch.setenv(str('PIP_FIND_LINKS'), str(temp_dir))
monkeypatch.setenv(str('PIP_NO_INDEX'), str('1'))
monkeypatch.setenv(str('PIP_RETRIES'), str('0'))
monkeypatch.setenv(str('PIP_TIMEOUT'), str('0'))
run_setup(test_setup_py, [str('--version')])


def make_trivial_sdist(dist_path, distname, version):
"""
Create a simple sdist tarball at dist_path, containing just a simple
Expand Down

0 comments on commit 1da50ce

Please sign in to comment.