forked from mesonbuild/meson-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
177 lines (146 loc) · 4.99 KB
/
conftest.py
File metadata and controls
177 lines (146 loc) · 4.99 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
# SPDX-License-Identifier: MIT
import contextlib
import os
import os.path
import pathlib
import re
import shutil
import subprocess
import sys
import tempfile
from venv import EnvBuilder
import pytest
import mesonpy
def adjust_packaging_platform_tag(platform: str) -> str:
# The packaging module generates overly specific platforms tags on
# Linux. The platforms tags on Linux evolved over time.
# meson-python uses more relaxed platform tags to maintain
# compatibility with old wheel installation tools. The relaxed
# platform tags match the ones generated by the wheel package.
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
return re.sub(r'^(many|musl)linux(1|2010|2014|_\d+_\d+)_(.*)$', r'linux_\3', platform)
package_dir = pathlib.Path(__file__).parent / 'packages'
@contextlib.contextmanager
def cd_package(package):
cur_dir = os.getcwd()
package_path = package_dir / package
os.chdir(package_path)
try:
yield package_path
finally:
os.chdir(cur_dir)
@contextlib.contextmanager
def in_git_repo_context(path=os.path.curdir):
# Resist the tentation of using pathlib.Path here: it is not
# supporded by subprocess in Python 3.7.
path = os.path.abspath(path)
shutil.rmtree(os.path.join(path, '.git'), ignore_errors=True)
try:
subprocess.check_call(['git', 'init', '-b', 'main', path])
subprocess.check_call(['git', 'config', 'user.email', 'author@example.com'], cwd=path)
subprocess.check_call(['git', 'config', 'user.name', 'A U Thor'], cwd=path)
subprocess.check_call(['git', 'add', '*'], cwd=path)
subprocess.check_call(['git', 'commit', '-q', '-m', 'Test'], cwd=path)
yield
finally:
# PermissionError raised on Windows.
with contextlib.suppress(PermissionError):
shutil.rmtree(os.path.join(path, '.git'))
@pytest.fixture(scope='session')
def tmp_dir_session(tmpdir_factory):
return pathlib.Path(tempfile.mkdtemp(
prefix='mesonpy-test-',
dir=tmpdir_factory.mktemp('test'),
))
class VEnv(EnvBuilder):
def __init__(self, env_dir):
super().__init__(with_pip=True)
self.create(env_dir)
def ensure_directories(self, env_dir):
context = super().ensure_directories(env_dir)
# Store the path to the venv Python interpreter. There does
# not seem to be a way to do this without subclassing.
self.executable = context.env_exe
return context
@pytest.fixture()
def venv():
path = pathlib.Path(tempfile.mkdtemp(prefix='mesonpy-test-venv-'))
venv = VEnv(path)
try:
yield venv
finally:
shutil.rmtree(path)
def generate_package_fixture(package):
@pytest.fixture
def fixture():
with cd_package(package) as new_path:
yield new_path
return fixture
def generate_sdist_fixture(package):
@pytest.fixture(scope='session')
def fixture(tmp_dir_session):
with cd_package(package), in_git_repo_context():
return tmp_dir_session / mesonpy.build_sdist(tmp_dir_session)
return fixture
def generate_wheel_fixture(package):
@pytest.fixture(scope='session')
def fixture(tmp_dir_session):
with cd_package(package), in_git_repo_context():
return tmp_dir_session / mesonpy.build_wheel(tmp_dir_session)
return fixture
# inject {package,sdist,wheel}_* fixtures (https://github.com/pytest-dev/pytest/issues/2424)
for package in os.listdir(package_dir):
normalized = package.replace('-', '_')
globals()[f'package_{normalized}'] = generate_package_fixture(package)
globals()[f'sdist_{normalized}'] = generate_sdist_fixture(package)
globals()[f'wheel_{normalized}'] = generate_wheel_fixture(package)
@pytest.fixture(scope='session')
def pep518_wheelhouse(tmpdir_factory):
wheelhouse = tmpdir_factory.mktemp('wheelhouse')
dist = tmpdir_factory.mktemp('dist')
subprocess.run(
[sys.executable, '-m', 'build', '--wheel', '--outdir', str(dist)],
cwd=str(package_dir.parent.parent),
check=True,
)
(wheel_path,) = dist.visit('*.whl')
subprocess.run(
[
sys.executable,
'-m',
'pip',
'download',
'-q',
'-d',
str(wheelhouse),
str(wheel_path),
],
check=True,
)
subprocess.run(
[
sys.executable,
'-m',
'pip',
'download',
'-q',
'-d',
str(wheelhouse),
'build',
'colorama',
'meson',
'ninja',
'patchelf',
'pyproject-metadata',
'tomli',
'typing-extensions',
'wheel',
],
check=True,
)
return str(wheelhouse)
@pytest.fixture
def pep518(pep518_wheelhouse, monkeypatch):
monkeypatch.setenv('PIP_FIND_LINKS', pep518_wheelhouse)
monkeypatch.setenv('PIP_NO_INDEX', 'true')
return pep518_wheelhouse