| 1 |
import configparser |
| 2 |
import csv |
| 3 |
import io |
| 4 |
import os |
| 5 |
import stat |
| 6 |
from pathlib import Path |
| 7 |
import tempfile |
| 8 |
from unittest import skipIf |
| 9 |
import zipfile |
| 10 |
|
| 11 |
import pytest |
| 12 |
from testpath import assert_isfile, assert_isdir, assert_not_path_exists |
| 13 |
|
| 14 |
from flit.wheel import WheelBuilder, make_wheel_in |
| 15 |
from flit.config import ConfigError |
| 16 |
|
| 17 |
samples_dir = Path(__file__).parent / 'samples' |
| 18 |
|
| 19 |
|
| 20 |
def unpack(path): |
| 21 |
z = zipfile.ZipFile(str(path)) |
| 22 |
t = tempfile.TemporaryDirectory() |
| 23 |
z.extractall(t.name) |
| 24 |
return t |
| 25 |
|
| 26 |
def test_wheel_module(copy_sample): |
| 27 |
td = copy_sample('module1_toml') |
| 28 |
make_wheel_in(td / 'pyproject.toml', td) |
| 29 |
assert_isfile(td / 'module1-0.1-py2.py3-none-any.whl') |
| 30 |
|
| 31 |
def test_editable_wheel_module(copy_sample): |
| 32 |
td = copy_sample('module1_toml') |
| 33 |
make_wheel_in(td / 'pyproject.toml', td, editable=True) |
| 34 |
whl_file = td / 'module1-0.1-py2.py3-none-any.whl' |
| 35 |
assert_isfile(whl_file) |
| 36 |
with unpack(whl_file) as unpacked: |
| 37 |
pth_path = Path(unpacked, 'module1.pth') |
| 38 |
assert_isfile(pth_path) |
| 39 |
assert pth_path.read_text() == str(td) |
| 40 |
assert_isdir(Path(unpacked, 'module1-0.1.dist-info')) |
| 41 |
|
| 42 |
def test_editable_wheel_has_absolute_pth(copy_sample): |
| 43 |
td = copy_sample('module1_toml') |
| 44 |
oldcwd = os.getcwd() |
| 45 |
os.chdir(str(td)) |
| 46 |
try: |
| 47 |
make_wheel_in(Path('pyproject.toml'), Path('.'), editable=True) |
| 48 |
whl_file = 'module1-0.1-py2.py3-none-any.whl' |
| 49 |
assert_isfile(whl_file) |
| 50 |
with unpack(whl_file) as unpacked: |
| 51 |
pth_path = Path(unpacked, 'module1.pth') |
| 52 |
assert_isfile(pth_path) |
| 53 |
assert Path(pth_path.read_text()).is_absolute() |
| 54 |
assert pth_path.read_text() == str(td.resolve()) |
| 55 |
assert_isdir(Path(unpacked, 'module1-0.1.dist-info')) |
| 56 |
finally: |
| 57 |
os.chdir(oldcwd) |
| 58 |
|
| 59 |
def test_wheel_package(copy_sample): |
| 60 |
td = copy_sample('package1') |
| 61 |
make_wheel_in(td / 'pyproject.toml', td) |
| 62 |
assert_isfile(td / 'package1-0.1-py2.py3-none-any.whl') |
| 63 |
|
| 64 |
def test_editable_wheel_package(copy_sample): |
| 65 |
td = copy_sample('package1') |
| 66 |
make_wheel_in(td / 'pyproject.toml', td, editable=True) |
| 67 |
whl_file = td / 'package1-0.1-py2.py3-none-any.whl' |
| 68 |
assert_isfile(whl_file) |
| 69 |
with unpack(whl_file) as unpacked: |
| 70 |
pth_path = Path(unpacked, 'package1.pth') |
| 71 |
assert_isfile(pth_path) |
| 72 |
assert pth_path.read_text() == str(td) |
| 73 |
assert_isdir(Path(unpacked, 'package1-0.1.dist-info')) |
| 74 |
|
| 75 |
def test_editable_wheel_namespace_package(copy_sample): |
| 76 |
td = copy_sample('ns1-pkg') |
| 77 |
make_wheel_in(td / 'pyproject.toml', td, editable=True) |
| 78 |
whl_file = td / 'ns1_pkg-0.1-py2.py3-none-any.whl' |
| 79 |
assert_isfile(whl_file) |
| 80 |
with unpack(whl_file) as unpacked: |
| 81 |
pth_path = Path(unpacked, 'ns1.pkg.pth') |
| 82 |
assert_isfile(pth_path) |
| 83 |
assert pth_path.read_text() == str(td) |
| 84 |
assert_isdir(Path(unpacked, 'ns1_pkg-0.1.dist-info')) |
| 85 |
|
| 86 |
def test_wheel_src_module(copy_sample): |
| 87 |
td = copy_sample('module3') |
| 88 |
make_wheel_in(td / 'pyproject.toml', td) |
| 89 |
|
| 90 |
whl_file = td / 'module3-0.1-py2.py3-none-any.whl' |
| 91 |
assert_isfile(whl_file) |
| 92 |
with unpack(whl_file) as unpacked: |
| 93 |
assert_isfile(Path(unpacked, 'module3.py')) |
| 94 |
assert_isdir(Path(unpacked, 'module3-0.1.dist-info')) |
| 95 |
assert_isfile(Path(unpacked, 'module3-0.1.dist-info', 'licenses', 'LICENSE')) |
| 96 |
|
| 97 |
def test_editable_wheel_src_module(copy_sample): |
| 98 |
td = copy_sample('module3') |
| 99 |
make_wheel_in(td / 'pyproject.toml', td, editable=True) |
| 100 |
whl_file = td / 'module3-0.1-py2.py3-none-any.whl' |
| 101 |
assert_isfile(whl_file) |
| 102 |
with unpack(whl_file) as unpacked: |
| 103 |
pth_path = Path(unpacked, 'module3.pth') |
| 104 |
assert_isfile(pth_path) |
| 105 |
assert pth_path.read_text() == str(td / "src") |
| 106 |
assert_isdir(Path(unpacked, 'module3-0.1.dist-info')) |
| 107 |
|
| 108 |
def test_wheel_src_package(copy_sample): |
| 109 |
td = copy_sample('package2') |
| 110 |
make_wheel_in(td / 'pyproject.toml', td) |
| 111 |
|
| 112 |
whl_file = td / 'package2-0.1-py2.py3-none-any.whl' |
| 113 |
assert_isfile(whl_file) |
| 114 |
with unpack(whl_file) as unpacked: |
| 115 |
print(os.listdir(unpacked)) |
| 116 |
assert_isfile(Path(unpacked, 'package2', '__init__.py')) |
| 117 |
|
| 118 |
def test_editable_wheel_src_package(copy_sample): |
| 119 |
td = copy_sample('package2') |
| 120 |
make_wheel_in(td / 'pyproject.toml', td, editable=True) |
| 121 |
whl_file = td / 'package2-0.1-py2.py3-none-any.whl' |
| 122 |
assert_isfile(whl_file) |
| 123 |
with unpack(whl_file) as unpacked: |
| 124 |
pth_path = Path(unpacked, 'package2.pth') |
| 125 |
assert_isfile(pth_path) |
| 126 |
assert pth_path.read_text() == str(td / "src") |
| 127 |
assert_isdir(Path(unpacked, 'package2-0.1.dist-info')) |
| 128 |
|
| 129 |
|
| 130 |
def test_wheel_ns_package(copy_sample): |
| 131 |
td = copy_sample('ns1-pkg') |
| 132 |
res = make_wheel_in(td / 'pyproject.toml', td) |
| 133 |
assert res.file == td / 'ns1_pkg-0.1-py2.py3-none-any.whl' |
| 134 |
assert_isfile(res.file) |
| 135 |
with unpack(res.file) as td_unpack: |
| 136 |
assert_isdir(Path(td_unpack, 'ns1_pkg-0.1.dist-info')) |
| 137 |
assert_isfile(Path(td_unpack, 'ns1', 'pkg', '__init__.py')) |
| 138 |
assert_not_path_exists(Path(td_unpack, 'ns1', '__init__.py')) |
| 139 |
|
| 140 |
def test_dist_name(copy_sample): |
| 141 |
td = copy_sample('altdistname') |
| 142 |
make_wheel_in(td / 'pyproject.toml', td) |
| 143 |
res = td / 'package_dist1-0.1-py2.py3-none-any.whl' |
| 144 |
assert_isfile(res) |
| 145 |
with unpack(res) as td_unpack: |
| 146 |
assert_isdir(Path(td_unpack, 'package_dist1-0.1.dist-info')) |
| 147 |
|
| 148 |
def test_entry_points(copy_sample): |
| 149 |
td = copy_sample('entrypoints_valid') |
| 150 |
make_wheel_in(td / 'pyproject.toml', td) |
| 151 |
assert_isfile(td / 'package1-0.1-py2.py3-none-any.whl') |
| 152 |
with unpack(td / 'package1-0.1-py2.py3-none-any.whl') as td_unpack: |
| 153 |
entry_points = Path(td_unpack, 'package1-0.1.dist-info', 'entry_points.txt') |
| 154 |
assert_isfile(entry_points) |
| 155 |
cp = configparser.ConfigParser() |
| 156 |
cp.read(str(entry_points)) |
| 157 |
assert 'console_scripts' in cp.sections() |
| 158 |
assert 'myplugins' in cp.sections() |
| 159 |
|
| 160 |
def test_entry_points_conflict(copy_sample): |
| 161 |
td = copy_sample('entrypoints_conflict') |
| 162 |
with pytest.raises(ConfigError): |
| 163 |
make_wheel_in(td / 'pyproject.toml', td) |
| 164 |
|
| 165 |
def test_wheel_builder(): |
| 166 |
# Slightly lower level interface |
| 167 |
with tempfile.TemporaryDirectory() as td: |
| 168 |
target = Path(td, 'sample.whl') |
| 169 |
with target.open('wb') as f: |
| 170 |
wb = WheelBuilder.from_ini_path(samples_dir / 'package1' / 'pyproject.toml', f) |
| 171 |
wb.build() |
| 172 |
|
| 173 |
assert zipfile.is_zipfile(str(target)) |
| 174 |
assert wb.wheel_filename == 'package1-0.1-py2.py3-none-any.whl' |
| 175 |
|
| 176 |
def test_stub_wheel_builder(): |
| 177 |
# Slightly lower level interface |
| 178 |
with tempfile.TemporaryDirectory() as td: |
| 179 |
target = Path(td, 'sample.whl') |
| 180 |
with target.open('wb') as f: |
| 181 |
wb = WheelBuilder.from_ini_path(samples_dir / 'sample-stubs' / 'pyproject.toml', f) |
| 182 |
wb.build() |
| 183 |
|
| 184 |
assert zipfile.is_zipfile(str(target)) |
| 185 |
assert wb.wheel_filename == 'wcwidth_stubs-0.2.13.1-py3-none-any.whl' |
| 186 |
|
| 187 |
@skipIf(os.name == 'nt', 'Windows does not preserve necessary permissions') |
| 188 |
def test_permissions_normed(copy_sample): |
| 189 |
td = copy_sample('module1_toml') |
| 190 |
|
| 191 |
(td / 'module1.py').chmod(0o620) |
| 192 |
make_wheel_in(td / 'pyproject.toml', td) |
| 193 |
|
| 194 |
whl = td / 'module1-0.1-py2.py3-none-any.whl' |
| 195 |
assert_isfile(whl) |
| 196 |
with zipfile.ZipFile(str(whl)) as zf: |
| 197 |
info = zf.getinfo('module1.py') |
| 198 |
perms = (info.external_attr >> 16) & 0o777 |
| 199 |
assert perms == 0o644, oct(perms) |
| 200 |
whl.unlink() |
| 201 |
|
| 202 |
# This time with executable bit set |
| 203 |
(td / 'module1.py').chmod(0o720) |
| 204 |
make_wheel_in(td / 'pyproject.toml', td) |
| 205 |
|
| 206 |
assert_isfile(whl) |
| 207 |
with zipfile.ZipFile(str(whl)) as zf: |
| 208 |
info = zf.getinfo('module1.py') |
| 209 |
perms = (info.external_attr >> 16) & 0o777 |
| 210 |
assert perms == 0o755, oct(perms) |
| 211 |
|
| 212 |
info = zf.getinfo('module1-0.1.dist-info/METADATA') |
| 213 |
perms = (info.external_attr >> 16) & 0o777 |
| 214 |
assert perms == 0o644, oct(perms) |
| 215 |
|
| 216 |
info = zf.getinfo('module1-0.1.dist-info/RECORD') |
| 217 |
perms = (info.external_attr >> 16) & stat.S_IFREG |
| 218 |
assert perms |
| 219 |
|
| 220 |
def test_compression(tmp_path): |
| 221 |
info = make_wheel_in(samples_dir / 'module1_toml' / 'pyproject.toml', tmp_path) |
| 222 |
assert_isfile(info.file) |
| 223 |
with zipfile.ZipFile(str(info.file)) as zf: |
| 224 |
for name in [ |
| 225 |
'module1.py', |
| 226 |
'module1-0.1.dist-info/METADATA', |
| 227 |
]: |
| 228 |
assert zf.getinfo(name).compress_type == zipfile.ZIP_DEFLATED |
| 229 |
|
| 230 |
def test_wheel_module_local_version(copy_sample): |
| 231 |
"""Test if a local version specifier is preserved in wheel filename and dist-info dir name""" |
| 232 |
td = copy_sample('modulewithlocalversion') |
| 233 |
make_wheel_in(td / 'pyproject.toml', td) |
| 234 |
|
| 235 |
whl_file = td / 'modulewithlocalversion-0.1.dev0+test-py2.py3-none-any.whl' |
| 236 |
assert_isfile(whl_file) |
| 237 |
with unpack(whl_file) as unpacked: |
| 238 |
assert_isfile(Path(unpacked, 'modulewithlocalversion.py')) |
| 239 |
assert_isdir(Path(unpacked, 'modulewithlocalversion-0.1.dev0+test.dist-info')) |
| 240 |
|
| 241 |
def test_record_csv_escaping(copy_sample): |
| 242 |
td = copy_sample('package1') |
| 243 |
|
| 244 |
(td / 'package1/a,b,c.bin').write_bytes(b'') |
| 245 |
|
| 246 |
info = make_wheel_in(td / 'pyproject.toml', td) |
| 247 |
|
| 248 |
with zipfile.ZipFile(info.file) as zf: |
| 249 |
with zf.open('package1-0.1.dist-info/RECORD') as record_file: |
| 250 |
with io.TextIOWrapper(record_file, newline='') as record_text_file: |
| 251 |
reader = csv.reader(record_text_file) |
| 252 |
row = next(r for r in reader if r[0] == 'package1/a,b,c.bin') |
| 253 |
assert row[1] == 'sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU' |
| 254 |
assert row[2] == '0' |