/
test_package.py
443 lines (375 loc) · 12.7 KB
/
test_package.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import os
import re
import traceback
from functools import partial
import py
import pytest
from tox.config import parseconfig
from tox.package import get_build_info, get_package
from tox.session import Reporter, Session
def test_make_sdist(initproj):
initproj(
"example123-0.5",
filedefs={
"tests": {"test_hello.py": "def test_hello(): pass"},
"tox.ini": """
""",
},
)
config = parseconfig([])
session = Session(config)
_, sdist = get_package(session)
assert sdist.check()
assert sdist.ext == ".zip"
assert sdist == config.distdir.join(sdist.basename)
_, sdist2 = get_package(session)
assert sdist2 == sdist
sdist.write("hello")
assert sdist.stat().size < 10
_, sdist_new = get_package(Session(config))
assert sdist_new == sdist
assert sdist_new.stat().size > 10
def test_make_sdist_distshare(tmpdir, initproj):
distshare = tmpdir.join("distshare")
initproj(
"example123-0.6",
filedefs={
"tests": {"test_hello.py": "def test_hello(): pass"},
"tox.ini": """
[tox]
distshare={}
""".format(
distshare
),
},
)
config = parseconfig([])
session = Session(config)
package, dist = get_package(session)
assert package.check()
assert package.ext == ".zip"
assert package == config.temp_dir.join("package", "1", package.basename)
assert dist == config.distdir.join(package.basename)
assert dist.check()
assert os.stat(str(dist)).st_ino == os.stat(str(package)).st_ino
sdist_share = config.distshare.join(package.basename)
assert sdist_share.check()
assert sdist_share.read("rb") == dist.read("rb"), (sdist_share, package)
def test_sdistonly(initproj, cmd):
initproj(
"example123",
filedefs={
"tox.ini": """
"""
},
)
result = cmd("-v", "--sdistonly")
assert not result.ret
assert re.match(r".*sdist-make.*setup.py.*", result.out, re.DOTALL)
assert "-mvirtualenv" not in result.out
def test_separate_sdist_no_sdistfile(cmd, initproj, tmpdir):
distshare = tmpdir.join("distshare")
initproj(
("pkg123-foo", "0.7"),
filedefs={
"tox.ini": """
[tox]
distshare={}
""".format(
distshare
)
},
)
result = cmd("--sdistonly")
assert not result.ret
distshare_files = distshare.listdir()
assert len(distshare_files) == 1
sdistfile = distshare_files[0]
assert "pkg123-foo-0.7.zip" in str(sdistfile)
def test_separate_sdist(cmd, initproj, tmpdir):
distshare = tmpdir.join("distshare")
initproj(
"pkg123-0.7",
filedefs={
"tox.ini": """
[tox]
distshare={}
sdistsrc={{distshare}}/pkg123-0.7.zip
""".format(
distshare
)
},
)
result = cmd("--sdistonly")
assert not result.ret
dist_share_files = distshare.listdir()
assert len(dist_share_files) == 1
assert dist_share_files[0].check()
result = cmd("-v", "--notest")
assert not result.ret, result.out
msg = "python inst: {}".format(result.session.package)
assert msg in result.out, result.out
operation = "copied" if not hasattr(os, "link") else "links"
msg = "package {} {} to {}".format(
os.sep.join(("pkg123", ".tox", ".tmp", "package", "1", "pkg123-0.7.zip")),
operation,
os.sep.join(("distshare", "pkg123-0.7.zip")),
)
assert msg in result.out, result.out
def test_sdist_latest(tmpdir, newconfig):
distshare = tmpdir.join("distshare")
config = newconfig(
[],
"""
[tox]
distshare={}
sdistsrc={{distshare}}/pkg123-*
""".format(
distshare
),
)
p = distshare.ensure("pkg123-1.4.5.zip")
distshare.ensure("pkg123-1.4.5a1.zip")
session = Session(config)
_, dist = get_package(session)
assert dist == p
def test_installpkg(tmpdir, newconfig):
p = tmpdir.ensure("pkg123-1.0.zip")
config = newconfig(["--installpkg={}".format(p)], "")
session = Session(config)
_, sdist_path = get_package(session)
assert sdist_path == p
def test_package_isolated_no_pyproject_toml(initproj, cmd):
initproj(
"package_no_toml-0.1",
filedefs={
"tox.ini": """
[tox]
isolated_build = true
"""
},
)
result = cmd("--sdistonly")
assert result.ret == 1
assert result.outlines == ["ERROR: missing {}".format(py.path.local().join("pyproject.toml"))]
def toml_file_check(initproj, version, message, toml):
initproj(
"package_toml-{}".format(version),
filedefs={
"tox.ini": """
[tox]
isolated_build = true
""",
"pyproject.toml": toml,
},
)
reporter = Reporter(None)
with pytest.raises(SystemExit, message=1):
get_build_info(py.path.local(), reporter)
toml_file = py.path.local().join("pyproject.toml")
msg = "ERROR: {} inside {}".format(message, toml_file)
assert reporter.reported_lines == [msg]
def test_package_isolated_toml_no_build_system(initproj, cmd):
toml_file_check(initproj, 1, "build-system section missing", "")
def test_package_isolated_toml_no_requires(initproj, cmd):
toml_file_check(
initproj,
2,
"missing requires key at build-system section",
"""
[build-system]
""",
)
def test_package_isolated_toml_no_backend(initproj, cmd):
toml_file_check(
initproj,
3,
"missing build-backend key at build-system section",
"""
[build-system]
requires = []
""",
)
def test_package_isolated_toml_bad_requires(initproj, cmd):
toml_file_check(
initproj,
4,
"requires key at build-system section must be a list of string",
"""
[build-system]
requires = ""
build-backend = ""
""",
)
def test_package_isolated_toml_bad_backend(initproj, cmd):
toml_file_check(
initproj,
5,
"build-backend key at build-system section must be a string",
"""
[build-system]
requires = []
build-backend = []
""",
)
def test_dist_exists_version_change(mock_venv, initproj, cmd):
base = initproj(
"package_toml-{}".format("0.1"),
filedefs={
"tox.ini": """
[tox]
isolated_build = true
""",
"pyproject.toml": """
[build-system]
requires = ["setuptools >= 35.0.2"]
build-backend = 'setuptools.build_meta'
""",
},
)
result = cmd("-e", "py")
assert result.ret == 0, result.out
new_code = base.join("setup.py").read_text("utf-8").replace("0.1", "0.2")
base.join("setup.py").write_text(new_code, "utf-8")
result = cmd("-e", "py")
assert result.ret == 0, result.out
def test_tox_parallel_build_safe(initproj, cmd, mock_venv, monkeypatch):
initproj(
"env_var_test",
filedefs={
"tox.ini": """
[tox]
envlist = py
install_cmd = python -m -c 'print("ok")' -- {opts} {packages}'
[testenv]
commands = python --version
"""
},
)
# we try to recreate the following situation
# t1 starts and performs build
# t2 starts, but is blocked from t1 build lock to build
# t1 gets unblocked, t2 can now enter
# t1 is artificially blocked to run test command until t2 finishes build
# (parallel build package present)
# t2 package build finishes both t1 and t2 can now finish and clean up their build packages
import tox.package
import threading
t1_build_started = threading.Event()
t1_build_blocker = threading.Event()
t2_build_started = threading.Event()
t2_build_finished = threading.Event()
invoke_result = {}
def invoke_tox_in_thread(thread_name):
try:
result = cmd("--parallel--safe-build", "-vv")
except Exception as exception:
result = exception, traceback.format_exc()
invoke_result[thread_name] = result
prev_build_package = tox.package.build_package
with monkeypatch.context() as m:
def build_package(config, report, session):
t1_build_started.set()
prev_run_test_env = tox.session.Session.runtestenv
def run_test_env(self, venv, redirect=False):
t2_build_finished.wait()
return prev_run_test_env(self, venv, redirect)
session.runtestenv = partial(run_test_env, session)
t1_build_blocker.wait()
return prev_build_package(config, report, session)
m.setattr(tox.package, "build_package", build_package)
t1 = threading.Thread(target=invoke_tox_in_thread, args=("t1",))
t1.start()
t1_build_started.wait()
with monkeypatch.context() as m:
def build_package(config, report, session):
t2_build_started.set()
try:
return prev_build_package(config, report, session)
finally:
t2_build_finished.set()
m.setattr(tox.package, "build_package", build_package)
t2 = threading.Thread(target=invoke_tox_in_thread, args=("t2",))
t2.start()
# t2 should get blocked by t1 build lock
t2_build_started.wait(timeout=0.1)
assert not t2_build_started.is_set()
t1_build_blocker.set() # release t1 blocker -> t1 can now finish
# t1 at this point should block at run test until t2 build finishes
t2_build_started.wait()
t1.join() # wait for both t1 and t2 to finish
t2.join()
# all threads finished without error
for val in invoke_result.values():
if isinstance(val, tuple):
assert False, "{!r}\n{}".format(val[0], val[1])
err = "\n".join(
"{}=\n{}".format(k, v.err).strip() for k, v in invoke_result.items() if v.err.strip()
)
out = "\n".join(
"{}=\n{}".format(k, v.out).strip() for k, v in invoke_result.items() if v.out.strip()
)
for val in invoke_result.values():
assert not val.ret, "{}\n{}".format(err, out)
assert not err
# when the lock is hit we notify
lock_file = py.path.local().join(".tox", ".package.lock")
msg = "lock file {} present, will block until released".format(lock_file)
assert msg in out
# intermediate packages are removed at end of build
t1_package = invoke_result["t1"].session.getvenv("py").package
t2_package = invoke_result["t1"].session.getvenv("py").package
assert t1 != t2
assert not t1_package.exists()
assert not t2_package.exists()
# the final distribution remains
dist_after = invoke_result["t1"].session.config.distdir.listdir()
assert len(dist_after) == 1
sdist = dist_after[0]
assert t1_package != sdist
def test_install_via_installpkg(mock_venv, initproj, cmd):
base = initproj(
"pkg-0.1",
filedefs={
"tox.ini": """
[tox]
install_cmd = python -m -c 'print("ok")' -- {opts} {packages}'
"""
},
)
fake_package = base.ensure(".tox", "dist", "pkg123-0.1.zip")
result = cmd("-e", "py", "--notest", "--installpkg", str(fake_package.relto(base)))
assert result.ret == 0, result.out
def test_verbose_isolated_build(initproj, mock_venv, cmd):
initproj(
"example123-0.5",
filedefs={
"tox.ini": """
[tox]
isolated_build = true
""",
"pyproject.toml": """
[build-system]
requires = ["setuptools >= 35.0.2"]
build-backend = 'setuptools.build_meta'
""",
},
)
result = cmd("--sdistonly", "-vvv")
assert "running sdist" in result.out, result.out
assert "running egg_info" in result.out, result.out
assert "Writing example123-0.5{}setup.cfg".format(os.sep) in result.out, result.out
def test_verbose_legacy_build(initproj, mock_venv, cmd):
initproj(
"example123-0.5",
filedefs={
"tox.ini": """
[tox]
isolated_build = false
"""
},
)
result = cmd("--sdistonly", "-vvv")
assert "running sdist" in result.out, result.out
assert "running egg_info" in result.out, result.out
assert "Writing example123-0.5{}setup.cfg".format(os.sep) in result.out, result.out