File size: 5,197 Bytes
d1ceb73 |
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 |
import os
import contextlib
import sys
import subprocess
from pathlib import Path
import pytest
import path
from . import contexts, environment
@pytest.fixture
def user_override(monkeypatch):
"""
Override site.USER_BASE and site.USER_SITE with temporary directories in
a context.
"""
with contexts.tempdir() as user_base:
monkeypatch.setattr('site.USER_BASE', user_base)
with contexts.tempdir() as user_site:
monkeypatch.setattr('site.USER_SITE', user_site)
with contexts.save_user_site_setting():
yield
@pytest.fixture
def tmpdir_cwd(tmpdir):
with tmpdir.as_cwd() as orig:
yield orig
@pytest.fixture(autouse=True, scope="session")
def workaround_xdist_376(request):
"""
Workaround pytest-dev/pytest-xdist#376
``pytest-xdist`` tends to inject '' into ``sys.path``,
which may break certain isolation expectations.
Remove the entry so the import
machinery behaves the same irrespective of xdist.
"""
if not request.config.pluginmanager.has_plugin('xdist'):
return
with contextlib.suppress(ValueError):
sys.path.remove('')
@pytest.fixture
def sample_project(tmp_path):
"""
Clone the 'sampleproject' and return a path to it.
"""
cmd = ['git', 'clone', 'https://github.com/pypa/sampleproject']
try:
subprocess.check_call(cmd, cwd=str(tmp_path))
except Exception:
pytest.skip("Unable to clone sampleproject")
return tmp_path / 'sampleproject'
# sdist and wheel artifacts should be stable across a round of tests
# so we can build them once per session and use the files as "readonly"
# In the case of setuptools, building the wheel without sdist may cause
# it to contain the `build` directory, and therefore create situations with
# `setuptools/build/lib/build/lib/...`. To avoid that, build both artifacts at once.
def _build_distributions(tmp_path_factory, request):
with contexts.session_locked_tmp_dir(
request, tmp_path_factory, "dist_build"
) as tmp: # pragma: no cover
sdist = next(tmp.glob("*.tar.gz"), None)
wheel = next(tmp.glob("*.whl"), None)
if sdist and wheel:
return (sdist, wheel)
# Sanity check: should not create recursive setuptools/build/lib/build/lib/...
assert not Path(request.config.rootdir, "build/lib/build").exists()
subprocess.check_output([
sys.executable,
"-m",
"build",
"--outdir",
str(tmp),
str(request.config.rootdir),
])
# Sanity check: should not create recursive setuptools/build/lib/build/lib/...
assert not Path(request.config.rootdir, "build/lib/build").exists()
return next(tmp.glob("*.tar.gz")), next(tmp.glob("*.whl"))
@pytest.fixture(scope="session")
def setuptools_sdist(tmp_path_factory, request):
prebuilt = os.getenv("PRE_BUILT_SETUPTOOLS_SDIST")
if prebuilt and os.path.exists(prebuilt): # pragma: no cover
return Path(prebuilt).resolve()
sdist, _ = _build_distributions(tmp_path_factory, request)
return sdist
@pytest.fixture(scope="session")
def setuptools_wheel(tmp_path_factory, request):
prebuilt = os.getenv("PRE_BUILT_SETUPTOOLS_WHEEL")
if prebuilt and os.path.exists(prebuilt): # pragma: no cover
return Path(prebuilt).resolve()
_, wheel = _build_distributions(tmp_path_factory, request)
return wheel
@pytest.fixture
def venv(tmp_path, setuptools_wheel):
"""Virtual env with the version of setuptools under test installed"""
env = environment.VirtualEnv()
env.root = path.Path(tmp_path / 'venv')
env.create_opts = ['--no-setuptools', '--wheel=bundle']
# TODO: Use `--no-wheel` when setuptools implements its own bdist_wheel
env.req = str(setuptools_wheel)
# In some environments (eg. downstream distro packaging),
# where tox isn't used to run tests and PYTHONPATH is set to point to
# a specific setuptools codebase, PYTHONPATH will leak into the spawned
# processes.
# env.create() should install the just created setuptools
# wheel, but it doesn't if it finds another existing matching setuptools
# installation present on PYTHONPATH:
# `setuptools is already installed with the same version as the provided
# wheel. Use --force-reinstall to force an installation of the wheel.`
# This prevents leaking PYTHONPATH to the created environment.
with contexts.environment(PYTHONPATH=None):
return env.create()
@pytest.fixture
def venv_without_setuptools(tmp_path):
"""Virtual env without any version of setuptools installed"""
env = environment.VirtualEnv()
env.root = path.Path(tmp_path / 'venv_without_setuptools')
env.create_opts = ['--no-setuptools', '--no-wheel']
env.ensure_env()
return env
@pytest.fixture
def bare_venv(tmp_path):
"""Virtual env without any common packages installed"""
env = environment.VirtualEnv()
env.root = path.Path(tmp_path / 'bare_venv')
env.create_opts = ['--no-setuptools', '--no-pip', '--no-wheel', '--no-seed']
env.ensure_env()
return env
|