| 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 |
|
| 12 |
def test_target_not_wheel(new_project): |
| 13 |
project_file = new_project / 'pyproject.toml' |
| 14 |
contents = project_file.read_text(encoding='utf-8') |
| 15 |
contents = contents.replace('[tool.hatch.build.targets.wheel.hooks.mypyc]', '[tool.hatch.build.hooks.mypyc]') |
| 16 |
project_file.write_text(contents, encoding='utf-8') |
| 17 |
|
| 18 |
build_project('-s') |
| 19 |
|
| 20 |
build_dir = new_project / 'dist' |
| 21 |
assert build_dir.is_dir() |
| 22 |
|
| 23 |
artifacts = list(build_dir.iterdir()) |
| 24 |
assert len(artifacts) == 1 |
| 25 |
|
| 26 |
assert artifacts[0].name.endswith('.tar.gz') |
| 27 |
|
| 28 |
|
| 29 |
def test_no_exclusion(new_project, compiled_extension): |
| 30 |
build_project() |
| 31 |
|
| 32 |
build_dir = new_project / 'dist' |
| 33 |
assert build_dir.is_dir() |
| 34 |
|
| 35 |
artifacts = list(build_dir.iterdir()) |
| 36 |
assert len(artifacts) == 1 |
| 37 |
wheel_file = artifacts[0] |
| 38 |
|
| 39 |
best_matching_tag = next(sys_tags()) |
| 40 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 41 |
|
| 42 |
extraction_directory = new_project.parent / '_archive' |
| 43 |
extraction_directory.mkdir() |
| 44 |
|
| 45 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 46 |
zip_archive.extractall(str(extraction_directory)) |
| 47 |
|
| 48 |
root_paths = list(extraction_directory.iterdir()) |
| 49 |
assert len(root_paths) == 3 |
| 50 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 51 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 52 |
|
| 53 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 54 |
assert metadata_directory.is_dir() |
| 55 |
|
| 56 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 57 |
assert wheel_metadata_file.is_file() |
| 58 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 59 |
|
| 60 |
extracted_package_dir = extraction_directory / 'my_app' |
| 61 |
assert extracted_package_dir.is_dir() |
| 62 |
|
| 63 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 64 |
assert len(distributed_files) == 5 |
| 65 |
|
| 66 |
root_files = 0 |
| 67 |
fibonacci_files = 0 |
| 68 |
for distributed_file in distributed_files: |
| 69 |
if distributed_file.name.startswith('__init__'): |
| 70 |
root_files += 1 |
| 71 |
elif distributed_file.name.startswith('fib'): |
| 72 |
fibonacci_files += 1 |
| 73 |
|
| 74 |
assert root_files == 2 |
| 75 |
assert fibonacci_files == 2 |
| 76 |
|
| 77 |
|
| 78 |
def test_exclusion(new_project, compiled_extension): |
| 79 |
project_file = new_project / 'pyproject.toml' |
| 80 |
contents = project_file.read_text(encoding='utf-8') |
| 81 |
contents += '\nexclude = ["__main__.py"]' |
| 82 |
project_file.write_text(contents, encoding='utf-8') |
| 83 |
|
| 84 |
package_main = new_project / 'my_app' / '__main__.py' |
| 85 |
package_main.write_text( |
| 86 |
"""\ |
| 87 |
if __name__ == '__main__': |
| 88 |
from .fib import fib |
| 89 |
print(fib()) |
| 90 |
""", |
| 91 |
encoding='utf-8', |
| 92 |
) |
| 93 |
|
| 94 |
build_project() |
| 95 |
|
| 96 |
build_dir = new_project / 'dist' |
| 97 |
assert build_dir.is_dir() |
| 98 |
|
| 99 |
artifacts = list(build_dir.iterdir()) |
| 100 |
assert len(artifacts) == 1 |
| 101 |
wheel_file = artifacts[0] |
| 102 |
|
| 103 |
best_matching_tag = next(sys_tags()) |
| 104 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 105 |
|
| 106 |
extraction_directory = new_project.parent / '_archive' |
| 107 |
extraction_directory.mkdir() |
| 108 |
|
| 109 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 110 |
zip_archive.extractall(str(extraction_directory)) |
| 111 |
|
| 112 |
root_paths = list(extraction_directory.iterdir()) |
| 113 |
assert len(root_paths) == 3 |
| 114 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 115 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 116 |
|
| 117 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 118 |
assert metadata_directory.is_dir() |
| 119 |
|
| 120 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 121 |
assert wheel_metadata_file.is_file() |
| 122 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 123 |
|
| 124 |
extracted_package_dir = extraction_directory / 'my_app' |
| 125 |
assert extracted_package_dir.is_dir() |
| 126 |
|
| 127 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 128 |
assert len(distributed_files) == 6 |
| 129 |
|
| 130 |
root_files = 0 |
| 131 |
fibonacci_files = 0 |
| 132 |
for distributed_file in distributed_files: |
| 133 |
if distributed_file.name.startswith('__init__'): |
| 134 |
root_files += 1 |
| 135 |
elif distributed_file.name.startswith('fib'): |
| 136 |
fibonacci_files += 1 |
| 137 |
|
| 138 |
assert root_files == 2 |
| 139 |
assert fibonacci_files == 2 |
| 140 |
|
| 141 |
|
| 142 |
def test_separation(new_project): |
| 143 |
project_file = new_project / 'pyproject.toml' |
| 144 |
contents = project_file.read_text(encoding='utf-8') |
| 145 |
contents += '\noptions = { separate = true }' |
| 146 |
project_file.write_text(contents, encoding='utf-8') |
| 147 |
|
| 148 |
build_project() |
| 149 |
|
| 150 |
build_dir = new_project / 'dist' |
| 151 |
assert build_dir.is_dir() |
| 152 |
|
| 153 |
artifacts = list(build_dir.iterdir()) |
| 154 |
assert len(artifacts) == 1 |
| 155 |
wheel_file = artifacts[0] |
| 156 |
|
| 157 |
best_matching_tag = next(sys_tags()) |
| 158 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 159 |
|
| 160 |
extraction_directory = new_project.parent / '_archive' |
| 161 |
extraction_directory.mkdir() |
| 162 |
|
| 163 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 164 |
zip_archive.extractall(str(extraction_directory)) |
| 165 |
|
| 166 |
root_paths = list(extraction_directory.iterdir()) |
| 167 |
assert len(root_paths) == 2 |
| 168 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 169 |
|
| 170 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 171 |
assert metadata_directory.is_dir() |
| 172 |
|
| 173 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 174 |
assert wheel_metadata_file.is_file() |
| 175 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 176 |
|
| 177 |
extracted_package_dir = extraction_directory / 'my_app' |
| 178 |
assert extracted_package_dir.is_dir() |
| 179 |
|
| 180 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 181 |
assert len(distributed_files) == 5 |
| 182 |
|
| 183 |
root_files = 0 |
| 184 |
fibonacci_files = 0 |
| 185 |
for distributed_file in distributed_files: |
| 186 |
if distributed_file.name.startswith('__init__'): |
| 187 |
root_files += 1 |
| 188 |
elif distributed_file.name.startswith('fib'): |
| 189 |
fibonacci_files += 1 |
| 190 |
|
| 191 |
assert root_files == 1 |
| 192 |
assert fibonacci_files == 3 |
| 193 |
|
| 194 |
|
| 195 |
def test_src_layout(new_project, compiled_extension): |
| 196 |
project_file = new_project / 'pyproject.toml' |
| 197 |
contents = project_file.read_text(encoding='utf-8') |
| 198 |
contents = contents.replace('my_app', 'src/my_app') |
| 199 |
project_file.write_text(contents, encoding='utf-8') |
| 200 |
package_dir = new_project / 'my_app' |
| 201 |
src_dir = new_project / 'src' |
| 202 |
src_dir.mkdir() |
| 203 |
package_dir.replace(src_dir / 'my_app') |
| 204 |
|
| 205 |
build_project() |
| 206 |
|
| 207 |
build_dir = new_project / 'dist' |
| 208 |
assert build_dir.is_dir() |
| 209 |
|
| 210 |
artifacts = list(build_dir.iterdir()) |
| 211 |
assert len(artifacts) == 1 |
| 212 |
wheel_file = artifacts[0] |
| 213 |
|
| 214 |
best_matching_tag = next(sys_tags()) |
| 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 |
best_matching_tag = next(sys_tags()) |
| 282 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 283 |
|
| 284 |
extraction_directory = new_project.parent / '_archive' |
| 285 |
extraction_directory.mkdir() |
| 286 |
|
| 287 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 288 |
zip_archive.extractall(str(extraction_directory)) |
| 289 |
|
| 290 |
root_paths = list(extraction_directory.iterdir()) |
| 291 |
assert len(root_paths) == 3 |
| 292 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 293 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 294 |
|
| 295 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 296 |
assert metadata_directory.is_dir() |
| 297 |
|
| 298 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 299 |
assert wheel_metadata_file.is_file() |
| 300 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 301 |
|
| 302 |
extracted_package_dir = extraction_directory / 'my_app' |
| 303 |
assert extracted_package_dir.is_dir() |
| 304 |
|
| 305 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 306 |
assert len(distributed_files) == 5 |
| 307 |
|
| 308 |
root_files = 0 |
| 309 |
fibonacci_files = 0 |
| 310 |
for distributed_file in distributed_files: |
| 311 |
if distributed_file.name.startswith('__init__'): |
| 312 |
root_files += 1 |
| 313 |
elif distributed_file.name.startswith('fib'): |
| 314 |
fibonacci_files += 1 |
| 315 |
|
| 316 |
assert root_files == 2 |
| 317 |
assert fibonacci_files == 2 |
| 318 |
|
| 319 |
|
| 320 |
def test_build_dir(new_project, compiled_extension): |
| 321 |
build_dir = os.urandom(8).hex() |
| 322 |
|
| 323 |
project_file = new_project / 'pyproject.toml' |
| 324 |
contents = project_file.read_text(encoding='utf-8') |
| 325 |
contents += f'\nbuild-dir = "{build_dir}"' |
| 326 |
project_file.write_text(contents, encoding='utf-8') |
| 327 |
|
| 328 |
build_project() |
| 329 |
|
| 330 |
build_dir = new_project / 'dist' |
| 331 |
assert build_dir.is_dir() |
| 332 |
|
| 333 |
artifacts = list(build_dir.iterdir()) |
| 334 |
assert len(artifacts) == 1 |
| 335 |
wheel_file = artifacts[0] |
| 336 |
|
| 337 |
best_matching_tag = next(sys_tags()) |
| 338 |
assert wheel_file.name == f'my_app-1.2.3-{best_matching_tag}.whl' |
| 339 |
|
| 340 |
extraction_directory = new_project.parent / '_archive' |
| 341 |
extraction_directory.mkdir() |
| 342 |
|
| 343 |
with zipfile.ZipFile(str(wheel_file), 'r') as zip_archive: |
| 344 |
zip_archive.extractall(str(extraction_directory)) |
| 345 |
|
| 346 |
root_paths = list(extraction_directory.iterdir()) |
| 347 |
assert len(root_paths) == 3 |
| 348 |
assert len([root_path for root_path in root_paths if root_path.name.startswith('my_app')]) == 2 |
| 349 |
assert len([root_path for root_path in root_paths if root_path.name.endswith(compiled_extension)]) == 1 |
| 350 |
|
| 351 |
metadata_directory = extraction_directory / 'my_app-1.2.3.dist-info' |
| 352 |
assert metadata_directory.is_dir() |
| 353 |
|
| 354 |
wheel_metadata_file = metadata_directory / 'WHEEL' |
| 355 |
assert wheel_metadata_file.is_file() |
| 356 |
assert 'Root-Is-Purelib: false' in wheel_metadata_file.read_text(encoding='utf-8') |
| 357 |
|
| 358 |
extracted_package_dir = extraction_directory / 'my_app' |
| 359 |
assert extracted_package_dir.is_dir() |
| 360 |
|
| 361 |
distributed_files = list(extracted_package_dir.iterdir()) |
| 362 |
assert len(distributed_files) == 5 |
| 363 |
|
| 364 |
root_files = 0 |
| 365 |
fibonacci_files = 0 |
| 366 |
for distributed_file in distributed_files: |
| 367 |
if distributed_file.name.startswith('__init__'): |
| 368 |
root_files += 1 |
| 369 |
elif distributed_file.name.startswith('fib'): |
| 370 |
fibonacci_files += 1 |
| 371 |
|
| 372 |
assert root_files == 2 |
| 373 |
assert fibonacci_files == 2 |
| 374 |
|
| 375 |
intermediate_build_dir = new_project / build_dir |
| 376 |
assert intermediate_build_dir.is_dir() |