| 1 |
# SPDX-FileCopyrightText: 2021-present Ofek Lev <[email protected]> |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
import os |
| 5 |
import zipfile |
| 6 |
|
| 7 |
from packaging.tags import sys_tags |
| 8 |
|
| 9 |
from .utils import build_project |
| 10 |
|
| 11 |
best_matching_tag = next(iter( |
| 12 |
t for t in sys_tags() if 'manylinux' not in t.platform and 'musllinux' not in t.platform |
| 13 |
)) |
| 14 |
|
| 15 |
|
| 16 |
def test_target_not_wheel(new_project): |
| 17 |
project_file = new_project / 'pyproject.toml' |
| 18 |
contents = project_file.read_text(encoding='utf-8') |
| 19 |
contents = contents.replace('[tool.hatch.build.targets.wheel.hooks.mypyc]', '[tool.hatch.build.hooks.mypyc]') |
| 20 |
project_file.write_text(contents, encoding='utf-8') |
| 21 |
|
| 22 |
build_project('-s') |
| 23 |
|
| 24 |
build_dir = new_project / 'dist' |
| 25 |
assert build_dir.is_dir() |
| 26 |
|
| 27 |
artifacts = list(build_dir.iterdir()) |
| 28 |
assert len(artifacts) == 1 |
| 29 |
|
| 30 |
assert artifacts[0].name.endswith('.tar.gz') |
| 31 |
|
| 32 |
|
| 33 |
def test_no_exclusion(new_project, compiled_extension): |
| 34 |
build_project() |
| 35 |
|
| 36 |
build_dir = new_project / 'dist' |
| 37 |
assert build_dir.is_dir() |
| 38 |
|
| 39 |
artifacts = list(build_dir.iterdir()) |
| 40 |
assert len(artifacts) == 1 |
| 41 |
wheel_file = artifacts[0] |
| 42 |
|
| 43 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 44 |
|
| 45 |
extraction_directory = new_project.parent / '_archive' |
| 46 |
extraction_directory.mkdir() |
| 47 |
|
| 48 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 49 |
zip_archive.extractall(str(extraction_directory)) |
| 50 |
|
| 51 |
root_paths = list(extraction_directory.iterdir()) |
| 52 |
assert len(root_paths) == 3 |
| 53 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 54 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 55 |
|
| 56 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 57 |
assert metadata_directory.is_dir() |
| 58 |
|
| 59 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 60 |
assert wheel_metadata_file.is_file() |
| 61 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 62 |
|
| 63 |
extracted_package_dir = extraction_directory / 'my_app' |
| 64 |
assert extracted_package_dir.is_dir() |
| 65 |
|
| 66 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 67 |
assert len(distributed_files) == 5 |
| 68 |
|
| 69 |
root_files = 0 |
| 70 |
fibonacci_files = 0 |
| 71 |
for distributed_file in distributed_files: |
| 72 |
if distributed_file.name.startswith('__init__'): |
| 73 |
root_files += 1 |
| 74 |
elif distributed_file.name.startswith('fib'): |
| 75 |
fibonacci_files += 1 |
| 76 |
|
| 77 |
assert root_files == 2 |
| 78 |
assert fibonacci_files == 2 |
| 79 |
|
| 80 |
|
| 81 |
def test_exclusion(new_project, compiled_extension): |
| 82 |
project_file = new_project / 'pyproject.toml' |
| 83 |
contents = project_file.read_text(encoding='utf-8') |
| 84 |
contents += '\nexclude = ["__main__.py"]' |
| 85 |
project_file.write_text(contents, encoding='utf-8') |
| 86 |
|
| 87 |
package_main = new_project / 'my_app' / '__main__.py' |
| 88 |
package_main.write_text( |
| 89 |
"""\ |
| 90 |
if __name__ == '__main__': |
| 91 |
from .fib import fib |
| 92 |
print(fib()) |
| 93 |
""", |
| 94 |
encoding='utf-8', |
| 95 |
) |
| 96 |
|
| 97 |
build_project() |
| 98 |
|
| 99 |
build_dir = new_project / 'dist' |
| 100 |
assert build_dir.is_dir() |
| 101 |
|
| 102 |
artifacts = list(build_dir.iterdir()) |
| 103 |
assert len(artifacts) == 1 |
| 104 |
wheel_file = artifacts[0] |
| 105 |
|
| 106 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 107 |
|
| 108 |
extraction_directory = new_project.parent / '_archive' |
| 109 |
extraction_directory.mkdir() |
| 110 |
|
| 111 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 112 |
zip_archive.extractall(str(extraction_directory)) |
| 113 |
|
| 114 |
root_paths = list(extraction_directory.iterdir()) |
| 115 |
assert len(root_paths) == 3 |
| 116 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 117 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 118 |
|
| 119 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 120 |
assert metadata_directory.is_dir() |
| 121 |
|
| 122 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 123 |
assert wheel_metadata_file.is_file() |
| 124 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 125 |
|
| 126 |
extracted_package_dir = extraction_directory / 'my_app' |
| 127 |
assert extracted_package_dir.is_dir() |
| 128 |
|
| 129 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 130 |
assert len(distributed_files) == 6 |
| 131 |
|
| 132 |
root_files = 0 |
| 133 |
fibonacci_files = 0 |
| 134 |
for distributed_file in distributed_files: |
| 135 |
if distributed_file.name.startswith('__init__'): |
| 136 |
root_files += 1 |
| 137 |
elif distributed_file.name.startswith('fib'): |
| 138 |
fibonacci_files += 1 |
| 139 |
|
| 140 |
assert root_files == 2 |
| 141 |
assert fibonacci_files == 2 |
| 142 |
|
| 143 |
|
| 144 |
def test_separation(new_project): |
| 145 |
project_file = new_project / 'pyproject.toml' |
| 146 |
contents = project_file.read_text(encoding='utf-8') |
| 147 |
contents += '\noptions = { separate = true }' |
| 148 |
project_file.write_text(contents, encoding='utf-8') |
| 149 |
|
| 150 |
build_project() |
| 151 |
|
| 152 |
build_dir = new_project / 'dist' |
| 153 |
assert build_dir.is_dir() |
| 154 |
|
| 155 |
artifacts = list(build_dir.iterdir()) |
| 156 |
assert len(artifacts) == 1 |
| 157 |
wheel_file = artifacts[0] |
| 158 |
|
| 159 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 160 |
|
| 161 |
extraction_directory = new_project.parent / '_archive' |
| 162 |
extraction_directory.mkdir() |
| 163 |
|
| 164 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 165 |
zip_archive.extractall(str(extraction_directory)) |
| 166 |
|
| 167 |
root_paths = list(extraction_directory.iterdir()) |
| 168 |
assert len(root_paths) == 2 |
| 169 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 170 |
|
| 171 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 172 |
assert metadata_directory.is_dir() |
| 173 |
|
| 174 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 175 |
assert wheel_metadata_file.is_file() |
| 176 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 177 |
|
| 178 |
extracted_package_dir = extraction_directory / 'my_app' |
| 179 |
assert extracted_package_dir.is_dir() |
| 180 |
|
| 181 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 182 |
assert len(distributed_files) == 5 |
| 183 |
|
| 184 |
root_files = 0 |
| 185 |
fibonacci_files = 0 |
| 186 |
for distributed_file in distributed_files: |
| 187 |
if distributed_file.name.startswith('__init__'): |
| 188 |
root_files += 1 |
| 189 |
elif distributed_file.name.startswith('fib'): |
| 190 |
fibonacci_files += 1 |
| 191 |
|
| 192 |
assert root_files == 1 |
| 193 |
assert fibonacci_files == 3 |
| 194 |
|
| 195 |
|
| 196 |
def test_src_layout(new_project, compiled_extension): |
| 197 |
project_file = new_project / 'pyproject.toml' |
| 198 |
contents = project_file.read_text(encoding='utf-8') |
| 199 |
contents = contents.replace('my_app', 'src/my_app') |
| 200 |
project_file.write_text(contents, encoding='utf-8') |
| 201 |
package_dir = new_project / 'my_app' |
| 202 |
src_dir = new_project / 'src' |
| 203 |
src_dir.mkdir() |
| 204 |
package_dir.replace(src_dir / 'my_app') |
| 205 |
|
| 206 |
build_project() |
| 207 |
|
| 208 |
build_dir = new_project / 'dist' |
| 209 |
assert build_dir.is_dir() |
| 210 |
|
| 211 |
artifacts = list(build_dir.iterdir()) |
| 212 |
assert len(artifacts) == 1 |
| 213 |
wheel_file = artifacts[0] |
| 214 |
|
| 215 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 216 |
|
| 217 |
extraction_directory = new_project.parent / '_archive' |
| 218 |
extraction_directory.mkdir() |
| 219 |
|
| 220 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 221 |
zip_archive.extractall(str(extraction_directory)) |
| 222 |
|
| 223 |
root_paths = list(extraction_directory.iterdir()) |
| 224 |
assert len(root_paths) == 3 |
| 225 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 226 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 227 |
|
| 228 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 229 |
assert metadata_directory.is_dir() |
| 230 |
|
| 231 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 232 |
assert wheel_metadata_file.is_file() |
| 233 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 234 |
|
| 235 |
extracted_package_dir = extraction_directory / 'my_app' |
| 236 |
assert extracted_package_dir.is_dir() |
| 237 |
|
| 238 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 239 |
assert len(distributed_files) == 5 |
| 240 |
|
| 241 |
root_files = 0 |
| 242 |
fibonacci_files = 0 |
| 243 |
for distributed_file in distributed_files: |
| 244 |
if distributed_file.name.startswith('__init__'): |
| 245 |
root_files += 1 |
| 246 |
elif distributed_file.name.startswith('fib'): |
| 247 |
fibonacci_files += 1 |
| 248 |
|
| 249 |
assert root_files == 2 |
| 250 |
assert fibonacci_files == 2 |
| 251 |
|
| 252 |
|
| 253 |
def test_dependency(new_project, compiled_extension): |
| 254 |
# Has py.typed and no transitive dependencies |
| 255 |
dependency = 'charset_normalizer' |
| 256 |
|
| 257 |
project_file = new_project / 'pyproject.toml' |
| 258 |
contents = project_file.read_text(encoding='utf-8') |
| 259 |
contents = contents.replace('dependencies = []', f'dependencies = ["{dependency}"]') |
| 260 |
contents += '\nrequire-runtime-dependencies = true' |
| 261 |
project_file.write_text(contents, encoding='utf-8') |
| 262 |
|
| 263 |
core_logic_file = new_project / 'my_app' / 'fib.py' |
| 264 |
core_logic_file.write_text( |
| 265 |
f"""\ |
| 266 |
import {dependency} |
| 267 |
{core_logic_file.read_text()} |
| 268 |
""", |
| 269 |
encoding='utf-8', |
| 270 |
) |
| 271 |
|
| 272 |
build_project() |
| 273 |
|
| 274 |
build_dir = new_project / 'dist' |
| 275 |
assert build_dir.is_dir() |
| 276 |
|
| 277 |
artifacts = list(build_dir.iterdir()) |
| 278 |
assert len(artifacts) == 1 |
| 279 |
wheel_file = artifacts[0] |
| 280 |
|
| 281 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 282 |
|
| 283 |
extraction_directory = new_project.parent / '_archive' |
| 284 |
extraction_directory.mkdir() |
| 285 |
|
| 286 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 287 |
zip_archive.extractall(str(extraction_directory)) |
| 288 |
|
| 289 |
root_paths = list(extraction_directory.iterdir()) |
| 290 |
assert len(root_paths) == 3 |
| 291 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 292 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 293 |
|
| 294 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 295 |
assert metadata_directory.is_dir() |
| 296 |
|
| 297 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 298 |
assert wheel_metadata_file.is_file() |
| 299 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 300 |
|
| 301 |
extracted_package_dir = extraction_directory / 'my_app' |
| 302 |
assert extracted_package_dir.is_dir() |
| 303 |
|
| 304 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 305 |
assert len(distributed_files) == 5 |
| 306 |
|
| 307 |
root_files = 0 |
| 308 |
fibonacci_files = 0 |
| 309 |
for distributed_file in distributed_files: |
| 310 |
if distributed_file.name.startswith('__init__'): |
| 311 |
root_files += 1 |
| 312 |
elif distributed_file.name.startswith('fib'): |
| 313 |
fibonacci_files += 1 |
| 314 |
|
| 315 |
assert root_files == 2 |
| 316 |
assert fibonacci_files == 2 |
| 317 |
|
| 318 |
|
| 319 |
def test_build_dir(new_project, compiled_extension): |
| 320 |
build_dir = os.urandom(8).hex() |
| 321 |
|
| 322 |
project_file = new_project / 'pyproject.toml' |
| 323 |
contents = project_file.read_text(encoding='utf-8') |
| 324 |
contents += f'\nbuild-dir = "{build_dir}"' |
| 325 |
project_file.write_text(contents, encoding='utf-8') |
| 326 |
|
| 327 |
build_project() |
| 328 |
|
| 329 |
build_dir = new_project / 'dist' |
| 330 |
assert build_dir.is_dir() |
| 331 |
|
| 332 |
artifacts = list(build_dir.iterdir()) |
| 333 |
assert len(artifacts) == 1 |
| 334 |
wheel_file = artifacts[0] |
| 335 |
|
| 336 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 337 |
|
| 338 |
extraction_directory = new_project.parent / '_archive' |
| 339 |
extraction_directory.mkdir() |
| 340 |
|
| 341 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 342 |
zip_archive.extractall(str(extraction_directory)) |
| 343 |
|
| 344 |
root_paths = list(extraction_directory.iterdir()) |
| 345 |
assert len(root_paths) == 3 |
| 346 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 347 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 348 |
|
| 349 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 350 |
assert metadata_directory.is_dir() |
| 351 |
|
| 352 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 353 |
assert wheel_metadata_file.is_file() |
| 354 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 355 |
|
| 356 |
extracted_package_dir = extraction_directory / 'my_app' |
| 357 |
assert extracted_package_dir.is_dir() |
| 358 |
|
| 359 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 360 |
assert len(distributed_files) == 5 |
| 361 |
|
| 362 |
root_files = 0 |
| 363 |
fibonacci_files = 0 |
| 364 |
for distributed_file in distributed_files: |
| 365 |
if distributed_file.name.startswith('__init__'): |
| 366 |
root_files += 1 |
| 367 |
elif distributed_file.name.startswith('fib'): |
| 368 |
fibonacci_files += 1 |
| 369 |
|
| 370 |
assert root_files == 2 |
| 371 |
assert fibonacci_files == 2 |
| 372 |
|
| 373 |
intermediate_build_dir = new_project / build_dir |
| 374 |
assert intermediate_build_dir.is_dir() |