forked from python-poetry/poetry-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_complete.py
629 lines (494 loc) · 19.1 KB
/
test_complete.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
from __future__ import annotations
import ast
import os
import platform
import re
import shutil
import sys
import tarfile
import tempfile
import zipfile
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Iterator
import pytest
from poetry.core import __version__
from poetry.core.factory import Factory
from poetry.core.masonry.builder import Builder
if TYPE_CHECKING:
from pytest_mock import MockerFixture
fixtures_dir = Path(__file__).parent / "fixtures"
@pytest.fixture(autouse=True)
def setup() -> Iterator[None]:
clear_samples_dist()
yield
clear_samples_dist()
def clear_samples_dist() -> None:
for dist in fixtures_dir.glob("**/dist"):
if dist.is_dir():
shutil.rmtree(str(dist))
@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_wheel_c_extension() -> None:
module_path = fixtures_dir / "extended"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = fixtures_dir / "extended" / "dist" / "extended-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "extended-0.1/build.py" in tar.getnames()
assert "extended-0.1/extended/extended.c" in tar.getnames()
whl = list((module_path / "dist").glob("extended-0.1-cp*-cp*-*.whl"))[0]
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
has_compiled_extension = False
for name in zip.namelist():
if name.startswith("extended/extended") and name.endswith((".so", ".pyd")):
has_compiled_extension = True
assert has_compiled_extension
try:
wheel_data = zip.read("extended-0.1.dist-info/WHEEL").decode()
assert (
re.match(
f"""(?m)^\
Wheel-Version: 1.0
Generator: poetry-core {__version__}
Root-Is-Purelib: false
Tag: cp[23]_?\\d+-cp[23]_?\\d+m?u?-.+
$""",
wheel_data,
)
is not None
)
records = zip.read("extended-0.1.dist-info/RECORD").decode()
assert re.search(r"\s+extended/extended.*\.(so|pyd)", records) is not None
finally:
zip.close()
@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_wheel_c_extension_with_no_setup() -> None:
module_path = fixtures_dir / "extended_with_no_setup"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = fixtures_dir / "extended_with_no_setup" / "dist" / "extended-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "extended-0.1/build.py" in tar.getnames()
assert "extended-0.1/extended/extended.c" in tar.getnames()
whl = list((module_path / "dist").glob("extended-0.1-cp*-cp*-*.whl"))[0]
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
has_compiled_extension = False
for name in zip.namelist():
if name.startswith("extended/extended") and name.endswith((".so", ".pyd")):
has_compiled_extension = True
assert has_compiled_extension
try:
wheel_data = zip.read("extended-0.1.dist-info/WHEEL").decode()
assert (
re.match(
f"""(?m)^\
Wheel-Version: 1.0
Generator: poetry-core {__version__}
Root-Is-Purelib: false
Tag: cp[23]_?\\d+-cp[23]_?\\d+m?u?-.+
$""",
wheel_data,
)
is not None
)
records = zip.read("extended-0.1.dist-info/RECORD").decode()
assert re.search(r"\s+extended/extended.*\.(so|pyd)", records) is not None
finally:
zip.close()
@pytest.mark.skipif(
sys.platform == "win32"
and sys.version_info <= (3, 6)
or platform.python_implementation().lower() == "pypy",
reason="Disable test on Windows for Python <=3.6 and for PyPy",
)
def test_wheel_c_extension_src_layout() -> None:
module_path = fixtures_dir / "src_extended"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = fixtures_dir / "src_extended" / "dist" / "extended-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "extended-0.1/build.py" in tar.getnames()
assert "extended-0.1/src/extended/extended.c" in tar.getnames()
whl = list((module_path / "dist").glob("extended-0.1-cp*-cp*-*.whl"))[0]
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
has_compiled_extension = False
for name in zip.namelist():
if name.startswith("extended/extended") and name.endswith((".so", ".pyd")):
has_compiled_extension = True
assert has_compiled_extension
try:
wheel_data = zip.read("extended-0.1.dist-info/WHEEL").decode()
assert (
re.match(
f"""(?m)^\
Wheel-Version: 1.0
Generator: poetry-core {__version__}
Root-Is-Purelib: false
Tag: cp[23]_?\\d+-cp[23]_?\\d+m?u?-.+
$""",
wheel_data,
)
is not None
)
records = zip.read("extended-0.1.dist-info/RECORD").decode()
assert re.search(r"\s+extended/extended.*\.(so|pyd)", records) is not None
finally:
zip.close()
def test_complete() -> None:
module_path = fixtures_dir / "complete"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
whl = module_path / "dist" / "my_package-1.2.3-py3-none-any.whl"
assert whl.exists()
if sys.platform != "win32":
assert (os.stat(str(whl)).st_mode & 0o777) == 0o644
zip = zipfile.ZipFile(str(whl))
try:
assert "my_package/sub_pgk1/extra_file.xml" not in zip.namelist()
assert "my-package-1.2.3.data/scripts/script.sh" in zip.namelist()
assert (
"Hello World"
in zip.read("my-package-1.2.3.data/scripts/script.sh").decode()
)
entry_points = zip.read("my_package-1.2.3.dist-info/entry_points.txt")
assert (
entry_points.decode()
== """\
[console_scripts]
extra-script=my_package.extra:main[time]
my-2nd-script=my_package:main2
my-script=my_package:main
"""
)
wheel_data = zip.read("my_package-1.2.3.dist-info/WHEEL").decode()
assert (
wheel_data
== f"""\
Wheel-Version: 1.0
Generator: poetry-core {__version__}
Root-Is-Purelib: true
Tag: py3-none-any
"""
)
wheel_data = zip.read("my_package-1.2.3.dist-info/METADATA").decode()
assert (
wheel_data
== """\
Metadata-Version: 2.1
Name: my-package
Version: 1.2.3
Summary: Some description.
Home-page: https://python-poetry.org/
License: MIT
Keywords: packaging,dependency,poetry
Author: Sébastien Eustace
Author-email: sebastien@eustace.io
Maintainer: People Everywhere
Maintainer-email: people@everywhere.com
Requires-Python: >=3.6,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Provides-Extra: time
Requires-Dist: cachy[msgpack] (>=0.2.0,<0.3.0)
Requires-Dist: cleo (>=0.6,<0.7)
Requires-Dist: pendulum (>=1.4,<2.0) ; (python_version ~= "2.7" and sys_platform == "win32" or python_version in "3.4 3.5") and (extra == "time")
Project-URL: Documentation, https://python-poetry.org/docs
Project-URL: Issue Tracker, https://github.com/python-poetry/poetry/issues
Project-URL: Repository, https://github.com/python-poetry/poetry
Description-Content-Type: text/x-rst
My Package
==========
"""
)
actual_records = zip.read("my_package-1.2.3.dist-info/RECORD").decode()
# For some reason, the ordering of the files and the SHA hashes
# vary per operating systems and Python versions.
# So instead of 1:1 assertion, let's do a bit clunkier one:
expected_records = [
"my_package/__init__.py",
"my_package/data1/test.json",
"my_package/sub_pkg1/__init__.py",
"my_package/sub_pkg2/__init__.py",
"my_package/sub_pkg2/data2/data.json",
"my_package-1.2.3.dist-info/entry_points.txt",
"my_package-1.2.3.dist-info/LICENSE",
"my_package-1.2.3.dist-info/WHEEL",
"my_package-1.2.3.dist-info/METADATA",
]
for expected_record in expected_records:
assert expected_record in actual_records
finally:
zip.close()
def test_complete_no_vcs() -> None:
# Copy the complete fixtures dir to a temporary directory
module_path = fixtures_dir / "complete"
temporary_dir = Path(tempfile.mkdtemp()) / "complete"
shutil.copytree(module_path.as_posix(), temporary_dir.as_posix())
builder = Builder(Factory().create_poetry(temporary_dir))
builder.build(fmt="all")
whl = temporary_dir / "dist" / "my_package-1.2.3-py3-none-any.whl"
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
# Check the zipped file to be sure that included and excluded files are
# correctly taken account of without vcs
expected_name_list = [
"my_package/__init__.py",
"my_package/data1/test.json",
"my_package/sub_pkg1/__init__.py",
"my_package/sub_pkg2/__init__.py",
"my_package/sub_pkg2/data2/data.json",
"my-package-1.2.3.data/scripts/script.sh",
"my_package/sub_pkg3/foo.py",
"my_package-1.2.3.dist-info/entry_points.txt",
"my_package-1.2.3.dist-info/LICENSE",
"my_package-1.2.3.dist-info/WHEEL",
"my_package-1.2.3.dist-info/METADATA",
"my_package-1.2.3.dist-info/RECORD",
]
assert sorted(zip.namelist()) == sorted(expected_name_list)
try:
entry_points = zip.read("my_package-1.2.3.dist-info/entry_points.txt")
assert (
entry_points.decode()
== """\
[console_scripts]
extra-script=my_package.extra:main[time]
my-2nd-script=my_package:main2
my-script=my_package:main
"""
)
wheel_data = zip.read("my_package-1.2.3.dist-info/WHEEL").decode()
assert (
wheel_data
== f"""\
Wheel-Version: 1.0
Generator: poetry-core {__version__}
Root-Is-Purelib: true
Tag: py3-none-any
"""
)
wheel_data = zip.read("my_package-1.2.3.dist-info/METADATA").decode()
assert (
wheel_data
== """\
Metadata-Version: 2.1
Name: my-package
Version: 1.2.3
Summary: Some description.
Home-page: https://python-poetry.org/
License: MIT
Keywords: packaging,dependency,poetry
Author: Sébastien Eustace
Author-email: sebastien@eustace.io
Maintainer: People Everywhere
Maintainer-email: people@everywhere.com
Requires-Python: >=3.6,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Provides-Extra: time
Requires-Dist: cachy[msgpack] (>=0.2.0,<0.3.0)
Requires-Dist: cleo (>=0.6,<0.7)
Requires-Dist: pendulum (>=1.4,<2.0) ; (python_version ~= "2.7" and sys_platform == "win32" or python_version in "3.4 3.5") and (extra == "time")
Project-URL: Documentation, https://python-poetry.org/docs
Project-URL: Issue Tracker, https://github.com/python-poetry/poetry/issues
Project-URL: Repository, https://github.com/python-poetry/poetry
Description-Content-Type: text/x-rst
My Package
==========
"""
)
finally:
zip.close()
def test_module_src() -> None:
module_path = fixtures_dir / "source_file"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = module_path / "dist" / "module_src-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "module_src-0.1/src/module_src.py" in tar.getnames()
whl = module_path / "dist" / "module_src-0.1-py2.py3-none-any.whl"
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
try:
assert "module_src.py" in zip.namelist()
finally:
zip.close()
def test_package_src() -> None:
module_path = fixtures_dir / "source_package"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = module_path / "dist" / "package_src-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "package_src-0.1/src/package_src/module.py" in tar.getnames()
whl = module_path / "dist" / "package_src-0.1-py2.py3-none-any.whl"
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
try:
assert "package_src/__init__.py" in zip.namelist()
assert "package_src/module.py" in zip.namelist()
finally:
zip.close()
def test_split_source() -> None:
module_path = fixtures_dir / "split_source"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = module_path / "dist" / "split_source-0.1.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
assert "split_source-0.1/lib_a/module_a/__init__.py" in tar.getnames()
assert "split_source-0.1/lib_b/module_b/__init__.py" in tar.getnames()
whl = module_path / "dist" / "split_source-0.1-py3-none-any.whl"
assert whl.exists()
zip = zipfile.ZipFile(str(whl))
try:
assert "module_a/__init__.py" in zip.namelist()
assert "module_b/__init__.py" in zip.namelist()
finally:
zip.close()
def test_package_with_include(mocker: MockerFixture) -> None:
module_path = fixtures_dir / "with-include"
# Patch git module to return specific excluded files
p = mocker.patch("poetry.core.vcs.git.Git.get_ignored_files")
p.return_value = [
str(
Path(__file__).parent
/ "fixtures"
/ "with-include"
/ "extra_dir"
/ "vcs_excluded.txt"
),
str(
Path(__file__).parent
/ "fixtures"
/ "with-include"
/ "extra_dir"
/ "sub_pkg"
/ "vcs_excluded.txt"
),
]
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = fixtures_dir / "with-include" / "dist" / "with_include-1.2.3.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
names = tar.getnames()
assert len(names) == len(set(names))
assert "with_include-1.2.3/LICENSE" in names
assert "with_include-1.2.3/README.rst" in names
assert "with_include-1.2.3/extra_dir/__init__.py" in names
assert "with_include-1.2.3/extra_dir/vcs_excluded.txt" in names
assert "with_include-1.2.3/extra_dir/sub_pkg/__init__.py" in names
assert "with_include-1.2.3/extra_dir/sub_pkg/vcs_excluded.txt" not in names
assert "with_include-1.2.3/my_module.py" in names
assert "with_include-1.2.3/notes.txt" in names
assert "with_include-1.2.3/package_with_include/__init__.py" in names
assert "with_include-1.2.3/tests/__init__.py" in names
assert "with_include-1.2.3/pyproject.toml" in names
assert "with_include-1.2.3/setup.py" in names
assert "with_include-1.2.3/PKG-INFO" in names
assert "with_include-1.2.3/for_wheel_only/__init__.py" not in names
assert "with_include-1.2.3/src/src_package/__init__.py" in names
file = tar.extractfile("with_include-1.2.3/setup.py")
assert file
setup = file.read()
setup_ast = ast.parse(setup)
setup_ast.body = [n for n in setup_ast.body if isinstance(n, ast.Assign)]
ns: dict[str, Any] = {}
exec(compile(setup_ast, filename="setup.py", mode="exec"), ns)
assert ns["package_dir"] == {"": "src"}
assert ns["packages"] == [
"extra_dir",
"extra_dir.sub_pkg",
"package_with_include",
"src_package",
"tests",
]
assert ns["package_data"] == {"": ["*"]}
assert ns["modules"] == ["my_module"]
whl = module_path / "dist" / "with_include-1.2.3-py3-none-any.whl"
assert whl.exists()
with zipfile.ZipFile(str(whl)) as z:
names = z.namelist()
assert len(names) == len(set(names))
assert "with_include-1.2.3.dist-info/LICENSE" in names
assert "extra_dir/__init__.py" in names
assert "extra_dir/vcs_excluded.txt" in names
assert "extra_dir/sub_pkg/__init__.py" in names
assert "extra_dir/sub_pkg/vcs_excluded.txt" not in names
assert "for_wheel_only/__init__.py" in names
assert "my_module.py" in names
assert "notes.txt" in names
assert "package_with_include/__init__.py" in names
assert "tests/__init__.py" not in names
assert "src_package/__init__.py" in names
def test_respect_format_for_explicit_included_files() -> None:
module_path = fixtures_dir / "exclude-whl-include-sdist"
builder = Builder(Factory().create_poetry(module_path))
builder.build(fmt="all")
sdist = module_path / "dist" / "exclude_whl_include_sdist-0.1.0.tar.gz"
assert sdist.exists()
with tarfile.open(str(sdist), "r") as tar:
names = tar.getnames()
assert (
"exclude_whl_include_sdist-0.1.0/exclude_whl_include_sdist/__init__.py"
in names
)
assert (
"exclude_whl_include_sdist-0.1.0/exclude_whl_include_sdist/compiled/source.c"
in names
)
assert (
"exclude_whl_include_sdist-0.1.0/exclude_whl_include_sdist/compiled/source.h"
in names
)
assert (
"exclude_whl_include_sdist-0.1.0/exclude_whl_include_sdist/cython_code.pyx"
in names
)
assert "exclude_whl_include_sdist-0.1.0/pyproject.toml" in names
assert "exclude_whl_include_sdist-0.1.0/setup.py" in names
assert "exclude_whl_include_sdist-0.1.0/PKG-INFO" in names
whl = module_path / "dist" / "exclude_whl_include_sdist-0.1.0-py3-none-any.whl"
assert whl.exists()
with zipfile.ZipFile(str(whl)) as z:
names = z.namelist()
assert "exclude_whl_include_sdist/__init__.py" in names
assert "exclude_whl_include_sdist/compiled/source.c" not in names
assert "exclude_whl_include_sdist/compiled/source.h" not in names
assert "exclude_whl_include_sdist/cython_code.pyx" not in names
pass