1"""Run some integration tests.
2
3Try to install a few packages.
4"""
5
6import glob
7import os
8import sys
9import urllib.request
10
11import pytest
12
13from setuptools.command.easy_install import easy_install
14from setuptools.command import easy_install as easy_install_pkg
15from setuptools.dist import Distribution
16
17
18pytestmark = pytest.mark.skipif(
19    'platform.python_implementation() == "PyPy" and '
20    'platform.system() == "Windows"',
21    reason="pypa/setuptools#2496",
22)
23
24
25def setup_module(module):
26    packages = 'stevedore', 'virtualenvwrapper', 'pbr', 'novaclient'
27    for pkg in packages:
28        try:
29            __import__(pkg)
30            tmpl = "Integration tests cannot run when {pkg} is installed"
31            pytest.skip(tmpl.format(**locals()))
32        except ImportError:
33            pass
34
35    try:
36        urllib.request.urlopen('https://pypi.python.org/pypi')
37    except Exception as exc:
38        pytest.skip(str(exc))
39
40
41@pytest.fixture
42def install_context(request, tmpdir, monkeypatch):
43    """Fixture to set up temporary installation directory.
44    """
45    # Save old values so we can restore them.
46    new_cwd = tmpdir.mkdir('cwd')
47    user_base = tmpdir.mkdir('user_base')
48    user_site = tmpdir.mkdir('user_site')
49    install_dir = tmpdir.mkdir('install_dir')
50
51    def fin():
52        # undo the monkeypatch, particularly needed under
53        # windows because of kept handle on cwd
54        monkeypatch.undo()
55        new_cwd.remove()
56        user_base.remove()
57        user_site.remove()
58        install_dir.remove()
59
60    request.addfinalizer(fin)
61
62    # Change the environment and site settings to control where the
63    # files are installed and ensure we do not overwrite anything.
64    monkeypatch.chdir(new_cwd)
65    monkeypatch.setattr(easy_install_pkg, '__file__', user_site.strpath)
66    monkeypatch.setattr('site.USER_BASE', user_base.strpath)
67    monkeypatch.setattr('site.USER_SITE', user_site.strpath)
68    monkeypatch.setattr('sys.path', sys.path + [install_dir.strpath])
69    monkeypatch.setenv(str('PYTHONPATH'), str(os.path.pathsep.join(sys.path)))
70
71    # Set up the command for performing the installation.
72    dist = Distribution()
73    cmd = easy_install(dist)
74    cmd.install_dir = install_dir.strpath
75    return cmd
76
77
78def _install_one(requirement, cmd, pkgname, modulename):
79    cmd.args = [requirement]
80    cmd.ensure_finalized()
81    cmd.run()
82    target = cmd.install_dir
83    dest_path = glob.glob(os.path.join(target, pkgname + '*.egg'))
84    assert dest_path
85    assert os.path.exists(os.path.join(dest_path[0], pkgname, modulename))
86
87
88def test_stevedore(install_context):
89    _install_one('stevedore', install_context,
90                 'stevedore', 'extension.py')
91
92
93@pytest.mark.xfail
94def test_virtualenvwrapper(install_context):
95    _install_one('virtualenvwrapper', install_context,
96                 'virtualenvwrapper', 'hook_loader.py')
97
98
99def test_pbr(install_context):
100    _install_one('pbr', install_context,
101                 'pbr', 'core.py')
102
103
104@pytest.mark.xfail
105def test_python_novaclient(install_context):
106    _install_one('python-novaclient', install_context,
107                 'novaclient', 'base.py')
108
109
110def test_pyuri(install_context):
111    """
112    Install the pyuri package (version 0.3.1 at the time of writing).
113
114    This is also a regression test for issue #1016.
115    """
116    _install_one('pyuri', install_context, 'pyuri', 'uri.py')
117
118    pyuri = install_context.installed_projects['pyuri']
119
120    # The package data should be installed.
121    assert os.path.exists(os.path.join(pyuri.location, 'pyuri', 'uri.regex'))
122