|
4 | 4 |
|
5 | 5 | from hashlib import sha256
|
6 | 6 |
|
| 7 | +import pytest |
| 8 | + |
7 | 9 | from poetry.core.packages.utils.link import Link
|
8 | 10 |
|
9 | 11 |
|
10 |
| -def make_url(ext: str) -> Link: |
11 |
| - checksum = sha256(str(uuid.uuid4()).encode()) |
| 12 | +def make_checksum() -> str: |
| 13 | + return sha256(str(uuid.uuid4()).encode()).hexdigest() |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture() |
| 17 | +def file_checksum() -> str: |
| 18 | + return make_checksum() |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture() |
| 22 | +def metadata_checksum() -> str: |
| 23 | + return make_checksum() |
| 24 | + |
| 25 | + |
| 26 | +def make_url( |
| 27 | + ext: str, file_checksum: str = None, metadata_checksum: str = None |
| 28 | +) -> Link: |
| 29 | + file_checksum = file_checksum or make_checksum() |
12 | 30 | return Link(
|
13 | 31 | "https://files.pythonhosted.org/packages/16/52/dead/"
|
14 |
| - f"demo-1.0.0.{ext}#sha256={checksum}" |
| 32 | + f"demo-1.0.0.{ext}#sha256={file_checksum}", |
| 33 | + metadata=f"sha256={metadata_checksum}" if metadata_checksum else None, |
15 | 34 | )
|
16 | 35 |
|
17 | 36 |
|
18 |
| -def test_package_link_is_checks(): |
19 |
| - assert make_url("egg").is_egg |
20 |
| - assert make_url("tar.gz").is_sdist |
21 |
| - assert make_url("zip").is_sdist |
22 |
| - assert make_url("exe").is_wininst |
23 |
| - assert make_url("cp36-cp36m-manylinux1_x86_64.whl").is_wheel |
| 37 | +def test_package_link_hash(file_checksum: str) -> None: |
| 38 | + link = make_url(ext="whl", file_checksum=file_checksum) |
| 39 | + assert link.hash_name == "sha256" |
| 40 | + assert link.hash == file_checksum |
| 41 | + assert link.show_url == "demo-1.0.0.whl" |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize( |
| 45 | + ("ext", "check"), |
| 46 | + [ |
| 47 | + ("whl", "wheel"), |
| 48 | + ("egg", "egg"), |
| 49 | + ("tar.gz", "sdist"), |
| 50 | + ("zip", "sdist"), |
| 51 | + ("cp36-cp36m-manylinux1_x86_64.whl", "wheel"), |
| 52 | + ], |
| 53 | +) |
| 54 | +def test_package_link_is_checks(ext: str, check: str) -> None: |
| 55 | + link = make_url(ext=ext) |
| 56 | + assert getattr(link, f"is_{check}") |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + ("ext", "has_metadata"), |
| 61 | + [("whl", True), ("egg", False), ("tar.gz", True), ("zip", True)], |
| 62 | +) |
| 63 | +def test_package_link_pep658( |
| 64 | + ext: str, has_metadata: bool, metadata_checksum: str |
| 65 | +) -> None: |
| 66 | + link = make_url(ext=ext, metadata_checksum=metadata_checksum) |
| 67 | + |
| 68 | + if has_metadata: |
| 69 | + assert link.has_metadata |
| 70 | + assert link.metadata == f"{link.url_without_fragment}.metadata" |
| 71 | + assert link.metadata_hash == metadata_checksum |
| 72 | + assert link.metadata_hash_name == "sha256" |
| 73 | + else: |
| 74 | + assert not link.has_metadata |
| 75 | + assert not link.metadata |
| 76 | + assert not link.metadata_hash |
| 77 | + assert not link.metadata_hash_name |
| 78 | + |
| 79 | + |
| 80 | +def test_package_link_pep658_no_default_metadata() -> None: |
| 81 | + link = make_url(ext="whl") |
| 82 | + |
| 83 | + assert not link.has_metadata |
| 84 | + assert not link.metadata |
| 85 | + assert not link.metadata_hash |
| 86 | + assert not link.metadata_hash_name |
0 commit comments