-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_builder.py
318 lines (254 loc) · 8.65 KB
/
test_builder.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from pathlib import Path
from subprocess import CalledProcessError
import json
import os
import sys
import pytest
from pyproject_installer.build_cmd import (
build_wheel,
build_sdist,
WHEEL_TRACKER,
)
from pyproject_installer.build_cmd._build import (
BACKEND_CALLER,
SUPPORTED_BUILD_HOOKS,
build,
)
def test_srcdir_nonexistent(wheeldir):
with pytest.raises(
ValueError,
match="Unable to resolve path for source directory",
):
build_wheel(Path("/nonexistent/path"), outdir=wheeldir)
def test_srcdir_nondir(tmpdir, wheeldir):
file_src = tmpdir / "file_src"
file_src.touch()
with pytest.raises(ValueError, match="Source path should be a directory"):
build_wheel(file_src, outdir=wheeldir)
def test_srcdir_nonpython_project(tmpdir, wheeldir):
with pytest.raises(
ValueError, match="Required either pyproject.toml or setup.py"
):
build_wheel(tmpdir, outdir=wheeldir)
def test_missing_pyproject_config(mock_build, tmpdir):
"""If pyproject.toml is missing then the default backend should be used"""
wheeldir = tmpdir / "dist"
setuppy = tmpdir / "setup.py"
setuppy.touch()
build_wheel(tmpdir, outdir=wheeldir)
b_kwargs = {
"args": [
sys.executable,
BACKEND_CALLER,
"--result-fd",
"4",
"setuptools.build_meta:__legacy__",
"build_wheel",
"--hook-args",
json.dumps([[str(wheeldir)], {"config_settings": None}]),
],
"stdin": None,
"capture_output": True,
"cwd": tmpdir,
"check": True,
"pass_fds": (4,),
}
mock_build.assert_called_once_with(**b_kwargs)
@pytest.mark.parametrize(
"build_args",
({}, {"verbose": False}, {"verbose": True}),
ids=["default", "quiet", "verbose"],
)
def test_verbosity(mock_build, pyproject, build_args):
"""Check verbosity"""
pyproject_path = pyproject()
wheeldir = pyproject_path / "dist"
verbose = build_args.get("verbose", False)
capture = not verbose
# emulate build error to see captured out/err
def _raise_process_error(*args, **kwargs):
stdout, stderr = (b"stdout", b"stderr") if capture else (None, None)
raise CalledProcessError(
1, ["command args"], output=stdout, stderr=stderr
)
mock_build.side_effect = _raise_process_error
expected_err_msg = "build_wheel failed"
if not verbose:
expected_err_msg += (
"\n\nCaptured stdout:\n\nstdout\n\nCaptured stderr:\n\nstderr"
)
with pytest.raises(RuntimeError) as e:
build_wheel(pyproject_path, outdir=wheeldir, **build_args)
assert str(e.value) == expected_err_msg
b_kwargs = {
"args": [
sys.executable,
BACKEND_CALLER,
"--result-fd",
"4",
]
+ [x for x in ("--verbose",) if verbose]
+ [
"be",
"build_wheel",
"--hook-args",
json.dumps([[str(wheeldir)], {"config_settings": None}]),
],
"stdin": None,
"capture_output": not verbose,
"cwd": pyproject_path,
"check": True,
"pass_fds": (4,),
}
mock_build.assert_called_once_with(**b_kwargs)
def test_paths_resolved(mock_build, pyproject, monkeypatch):
"""Check if srcdir and wheeldir are resolved for backend"""
pyproject_path = pyproject()
cwd = pyproject_path.parent
monkeypatch.chdir(cwd)
wheeldir = cwd / "dist"
build_wheel(
pyproject_path.relative_to(cwd), outdir=wheeldir.relative_to(cwd)
)
b_kwargs = {
"args": [
sys.executable,
BACKEND_CALLER,
"--result-fd",
"4",
"be",
"build_wheel",
"--hook-args",
json.dumps([[str(wheeldir)], {"config_settings": None}]),
],
"stdin": None,
"capture_output": True,
"cwd": pyproject_path,
"check": True,
"pass_fds": (4,),
}
mock_build.assert_called_once_with(**b_kwargs)
def test_build_backend_config_settings(mock_build, pyproject):
"""Check build-backend"""
pyproject_path = pyproject()
wheeldir = pyproject_path / "dist"
config = {"key": "value"}
build_wheel(pyproject_path, outdir=wheeldir, config=config)
b_kwargs = {
"args": [
sys.executable,
BACKEND_CALLER,
"--result-fd",
"4",
"be",
"build_wheel",
"--hook-args",
json.dumps([[str(wheeldir)], {"config_settings": config}]),
],
"stdin": None,
"capture_output": True,
"cwd": pyproject_path,
"check": True,
"pass_fds": (4,),
}
mock_build.assert_called_once_with(**b_kwargs)
def test_nonexistent_outdir(mock_build, pyproject):
"""Check if outdir is created when it's missing"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
assert not outdir.exists()
build_wheel(pyproject_path, outdir=outdir)
assert outdir.exists()
def test_existent_outdir(mock_build, pyproject):
"""Check if build_wheel doesn't fail on existent outdir"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
outdir.mkdir()
build_wheel(pyproject_path, outdir=outdir)
@pytest.mark.skipif(os.geteuid() == 0, reason="Requires unprivileged user")
def test_uncreatable_outdir(mock_build, pyproject):
"""Check error if outdir is uncreatable(e.g. not enough permissions)"""
pyproject_path = pyproject()
outdir = Path("/uncreatable_dir")
with pytest.raises(
ValueError,
match="Unable to create path for outdir: /uncreatable_dir",
):
build_wheel(pyproject_path, outdir=outdir)
def test_wheeltracker(mock_build, pyproject):
"""Check if .wheeltracker is written on build"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
tracker = outdir / WHEEL_TRACKER
assert not tracker.exists()
build_wheel(pyproject_path, outdir=outdir)
assert tracker.is_file()
assert tracker.read_text().rstrip() == "foo.whl"
def test_sdist_no_wheeltracker(mock_build, pyproject):
"""Check if .wheeltracker is not created on sdist build"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
tracker = outdir / WHEEL_TRACKER
assert not tracker.exists()
build_sdist(pyproject_path, outdir=outdir)
assert not tracker.exists()
def test_build_invalid_hook(pyproject):
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
invalid_hook = "invalid_hook"
with pytest.raises(
ValueError,
match=(
f"Unknown build hook: {invalid_hook}, "
f"supported: {', '.join(SUPPORTED_BUILD_HOOKS)}"
),
):
build(pyproject_path, outdir=outdir, hook=invalid_hook)
@pytest.mark.parametrize("hook", SUPPORTED_BUILD_HOOKS)
def test_supported_build_hooks(hook, mock_build, pyproject):
"""Check build hook"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
build(pyproject_path, outdir=outdir, hook=hook)
b_kwargs = {
"args": [
sys.executable,
BACKEND_CALLER,
"--result-fd",
"4",
"be",
hook,
"--hook-args",
json.dumps([[str(outdir)], {"config_settings": None}]),
],
"stdin": None,
"capture_output": True,
"cwd": pyproject_path,
"check": True,
"pass_fds": (4,),
}
mock_build.assert_called_once_with(**b_kwargs)
def test_raisable_thread(mock_build, pyproject, mocker):
"""Check if build fails on raised thread"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
# override mock_build's mock for os.read
mock_os_read = mocker.patch("pyproject_installer.build_cmd._build.os.read")
# emulate os.read error to raise thread
def _raise_os_read(*args, **kwargs):
raise OSError("oops")
mock_os_read.side_effect = _raise_os_read
with pytest.raises(RuntimeError, match="oops"):
build_wheel(pyproject_path, outdir=outdir)
def test_received_invalid_data(mock_build, pyproject, mocker):
"""Check if build fails on invalid data"""
pyproject_path = pyproject()
outdir = pyproject_path / "dist"
# override mock_build's mock for os.read
mock_os_read = mocker.patch("pyproject_installer.build_cmd._build.os.read")
mock_os_read.side_effect = [b"invalid_json", b""]
with pytest.raises(
RuntimeError,
match="Received invalid JSON data from backend helper: 'invalid_json'",
):
build_wheel(pyproject_path, outdir=outdir)