1import glob 2import os 3import sys 4 5import pytest 6from pytest import yield_fixture 7from pytest_fixture_config import yield_requires_config 8 9import pytest_virtualenv 10 11from .textwrap import DALS 12from .test_easy_install import make_nspkg_sdist 13 14 15@pytest.fixture(autouse=True) 16def pytest_virtualenv_works(virtualenv): 17 """ 18 pytest_virtualenv may not work. if it doesn't, skip these 19 tests. See #1284. 20 """ 21 venv_prefix = virtualenv.run( 22 'python -c "import sys; print(sys.prefix)"', 23 capture=True, 24 ).strip() 25 if venv_prefix == sys.prefix: 26 pytest.skip("virtualenv is broken (see pypa/setuptools#1284)") 27 28 29@yield_requires_config(pytest_virtualenv.CONFIG, ['virtualenv_executable']) 30@yield_fixture(scope='function') 31def bare_virtualenv(): 32 """ Bare virtualenv (no pip/setuptools/wheel). 33 """ 34 with pytest_virtualenv.VirtualEnv(args=( 35 '--no-wheel', 36 '--no-pip', 37 '--no-setuptools', 38 )) as venv: 39 yield venv 40 41 42SOURCE_DIR = os.path.join(os.path.dirname(__file__), '../..') 43 44 45def test_clean_env_install(bare_virtualenv): 46 """ 47 Check setuptools can be installed in a clean environment. 48 """ 49 bare_virtualenv.run(' && '.join(( 50 'cd {source}', 51 'python setup.py install', 52 )).format(source=SOURCE_DIR)) 53 54 55def test_pip_upgrade_from_source(virtualenv): 56 """ 57 Check pip can upgrade setuptools from source. 58 """ 59 dist_dir = virtualenv.workspace 60 if sys.version_info < (2, 7): 61 # Python 2.6 support was dropped in wheel 0.30.0. 62 virtualenv.run('pip install -U "wheel<0.30.0"') 63 # Generate source distribution / wheel. 64 virtualenv.run(' && '.join(( 65 'cd {source}', 66 'python setup.py -q sdist -d {dist}', 67 'python setup.py -q bdist_wheel -d {dist}', 68 )).format(source=SOURCE_DIR, dist=dist_dir)) 69 sdist = glob.glob(os.path.join(dist_dir, '*.zip'))[0] 70 wheel = glob.glob(os.path.join(dist_dir, '*.whl'))[0] 71 # Then update from wheel. 72 virtualenv.run('pip install ' + wheel) 73 # And finally try to upgrade from source. 74 virtualenv.run('pip install --no-cache-dir --upgrade ' + sdist) 75 76 77def test_test_command_install_requirements(bare_virtualenv, tmpdir): 78 """ 79 Check the test command will install all required dependencies. 80 """ 81 bare_virtualenv.run(' && '.join(( 82 'cd {source}', 83 'python setup.py develop', 84 )).format(source=SOURCE_DIR)) 85 86 def sdist(distname, version): 87 dist_path = tmpdir.join('%s-%s.tar.gz' % (distname, version)) 88 make_nspkg_sdist(str(dist_path), distname, version) 89 return dist_path 90 dependency_links = [ 91 str(dist_path) 92 for dist_path in ( 93 sdist('foobar', '2.4'), 94 sdist('bits', '4.2'), 95 sdist('bobs', '6.0'), 96 sdist('pieces', '0.6'), 97 ) 98 ] 99 with tmpdir.join('setup.py').open('w') as fp: 100 fp.write(DALS( 101 ''' 102 from setuptools import setup 103 104 setup( 105 dependency_links={dependency_links!r}, 106 install_requires=[ 107 'barbazquux1; sys_platform in ""', 108 'foobar==2.4', 109 ], 110 setup_requires='bits==4.2', 111 tests_require=""" 112 bobs==6.0 113 """, 114 extras_require={{ 115 'test': ['barbazquux2'], 116 ':"" in sys_platform': 'pieces==0.6', 117 ':python_version > "1"': """ 118 pieces 119 foobar 120 """, 121 }} 122 ) 123 '''.format(dependency_links=dependency_links))) 124 with tmpdir.join('test.py').open('w') as fp: 125 fp.write(DALS( 126 ''' 127 import foobar 128 import bits 129 import bobs 130 import pieces 131 132 open('success', 'w').close() 133 ''')) 134 # Run test command for test package. 135 bare_virtualenv.run(' && '.join(( 136 'cd {tmpdir}', 137 'python setup.py test -s test', 138 )).format(tmpdir=tmpdir)) 139 assert tmpdir.join('success').check() 140