aboutsummaryrefslogtreecommitdiff
path: root/setuptools/tests/test_integration.py
blob: 3a9a6c50a1e45c0bc91df78c234b6bfb8e0fe21c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Run some integration tests.

Try to install a few packages.
"""

import glob
import os
import sys

from setuptools.extern.six.moves import urllib
import pytest

from setuptools.command.easy_install import easy_install
from setuptools.command import easy_install as easy_install_pkg
from setuptools.dist import Distribution


def setup_module(module):
    packages = 'stevedore', 'virtualenvwrapper', 'pbr', 'novaclient'
    for pkg in packages:
        try:
            __import__(pkg)
            tmpl = "Integration tests cannot run when {pkg} is installed"
            pytest.skip(tmpl.format(**locals()))
        except ImportError:
            pass

    try:
        urllib.request.urlopen('https://pypi.python.org/pypi')
    except Exception as exc:
        pytest.skip(str(exc))


@pytest.fixture
def install_context(request, tmpdir, monkeypatch):
    """Fixture to set up temporary installation directory.
    """
    # Save old values so we can restore them.
    new_cwd = tmpdir.mkdir('cwd')
    user_base = tmpdir.mkdir('user_base')
    user_site = tmpdir.mkdir('user_site')
    install_dir = tmpdir.mkdir('install_dir')

    def fin():
        # undo the monkeypatch, particularly needed under
        # windows because of kept handle on cwd
        monkeypatch.undo()
        new_cwd.remove()
        user_base.remove()
        user_site.remove()
        install_dir.remove()

    request.addfinalizer(fin)

    # Change the environment and site settings to control where the
    # files are installed and ensure we do not overwrite anything.
    monkeypatch.chdir(new_cwd)
    monkeypatch.setattr(easy_install_pkg, '__file__', user_site.strpath)
    monkeypatch.setattr('site.USER_BASE', user_base.strpath)
    monkeypatch.setattr('site.USER_SITE', user_site.strpath)
    monkeypatch.setattr('sys.path', sys.path + [install_dir.strpath])
    monkeypatch.setenv('PYTHONPATH', os.path.pathsep.join(sys.path))

    # Set up the command for performing the installation.
    dist = Distribution()
    cmd = easy_install(dist)
    cmd.install_dir = install_dir.strpath
    return cmd


def _install_one(requirement, cmd, pkgname, modulename):
    cmd.args = [requirement]
    cmd.ensure_finalized()
    cmd.run()
    target = cmd.install_dir
    dest_path = glob.glob(os.path.join(target, pkgname + '*.egg'))
    assert dest_path
    assert os.path.exists(os.path.join(dest_path[0], pkgname, modulename))


def test_stevedore(install_context):
    _install_one('stevedore', install_context,
                 'stevedore', 'extension.py')


@pytest.mark.xfail
def test_virtualenvwrapper(install_context):
    _install_one('virtualenvwrapper', install_context,
                 'virtualenvwrapper', 'hook_loader.py')


def test_pbr(install_context):
    _install_one('pbr', install_context,
                 'pbr', 'core.py')


@pytest.mark.xfail
def test_python_novaclient(install_context):
    _install_one('python-novaclient', install_context,
                 'novaclient', 'base.py')


def test_pyuri(install_context):
    """
    Install the pyuri package (version 0.3.1 at the time of writing).

    This is also a regression test for issue #1016.
    """
    _install_one('pyuri', install_context, 'pyuri', 'uri.py')

    pyuri = install_context.installed_projects['pyuri']

    # The package data should be installed.
    assert os.path.exists(os.path.join(pyuri.location, 'pyuri', 'uri.regex'))


import re
import subprocess
import functools
import tarfile, zipfile


build_deps = ['appdirs', 'packaging', 'pyparsing', 'six']
@pytest.mark.parametrize("build_dep", build_deps)
@pytest.mark.skipif(sys.version_info < (3, 6), reason='run only on late versions')
def test_build_deps_on_distutils(request, tmpdir_factory, build_dep):
    """
    All setuptools build dependencies must build without
    setuptools.
    """
    if 'pyparsing' in build_dep:
        pytest.xfail(reason="Project imports setuptools unconditionally")
    build_target = tmpdir_factory.mktemp('source')
    build_dir = download_and_extract(request, build_dep, build_target)
    install_target = tmpdir_factory.mktemp('target')
    output = install(build_dir, install_target)
    for line in output.splitlines():
        match = re.search('Unknown distribution option: (.*)', line)
        allowed_unknowns = [
            'test_suite',
            'tests_require',
            'install_requires',
        ]
        assert not match or match.group(1).strip('"\'') in allowed_unknowns


def install(pkg_dir, install_dir):
    with open(os.path.join(pkg_dir, 'setuptools.py'), 'w') as breaker:
        breaker.write('raise ImportError()')
    cmd = [sys.executable, 'setup.py', 'install', '--prefix', install_dir]
    env = dict(os.environ, PYTHONPATH=pkg_dir)
    output = subprocess.check_output(cmd, cwd=pkg_dir, env=env, stderr=subprocess.STDOUT)
    return output.decode('utf-8')


def download_and_extract(request, req, target):
    cmd = [sys.executable, '-m', 'pip', 'download', '--no-deps',
        '--no-binary', ':all:', req]
    output = subprocess.check_output(cmd, encoding='utf-8')
    filename = re.search('Saved (.*)', output).group(1)
    request.addfinalizer(functools.partial(os.remove, filename))
    opener = zipfile.ZipFile if filename.endswith('.zip') else tarfile.open
    with opener(filename) as archive:
        archive.extractall(target)
    return os.path.join(target, os.listdir(target)[0])