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
|
from __future__ import annotations
import pytest
from poetry.core.packages.specification import PackageSpecification
@pytest.mark.parametrize(
"spec1, spec2, expected",
[
(PackageSpecification("a"), PackageSpecification("a"), True),
(PackageSpecification("a", "type1"), PackageSpecification("a", "type1"), True),
(PackageSpecification("a", "type1"), PackageSpecification("a", "type2"), False),
(PackageSpecification("a"), PackageSpecification("a", "type1"), False),
(PackageSpecification("a", "type1"), PackageSpecification("a"), False),
],
)
def test_is_same_package_source_type(
spec1: PackageSpecification,
spec2: PackageSpecification,
expected: bool,
) -> None:
assert spec1.is_same_package_as(spec2) == expected
@pytest.mark.parametrize(
("source_type", "result"),
[
("directory", True),
("file", True),
("url", True),
("git", True),
("legacy", False),
(None, False),
],
)
def test_is_direct_origin(source_type: str | None, result: bool) -> None:
assert PackageSpecification("package", source_type).is_direct_origin() == result
@pytest.mark.parametrize(
"spec1, spec2, expected",
[
(PackageSpecification("a"), PackageSpecification("a"), True),
(PackageSpecification("a"), PackageSpecification("b"), False),
(PackageSpecification("a", features=["x"]), PackageSpecification("a"), True),
(
PackageSpecification("a", features=["x"]),
PackageSpecification("a", features=["x"]),
True,
),
(
PackageSpecification("a", features=["x"]),
PackageSpecification("b", features=["x"]),
False,
),
(
PackageSpecification("a", features=["x"]),
PackageSpecification("a", features=["y"]),
False,
),
(
PackageSpecification("a", features=["x"]),
PackageSpecification("a", features=["x", "y"]),
False,
),
(
PackageSpecification("a", features=["x", "y"]),
PackageSpecification("a", features=["x"]),
True,
),
],
)
def test_specification_provides(
spec1: PackageSpecification,
spec2: PackageSpecification,
expected: bool,
) -> None:
assert spec1.provides(spec2) == expected
@pytest.mark.parametrize(
"spec1, spec2",
[
(
# nothing except for name and features matters if no source
PackageSpecification("a", None, "url1", "ref1", "resref1", "sub1"),
PackageSpecification("a", None, "url2", "ref2", "resref2", "sub2"),
),
(
# ref does not matter if resolved ref is equal
PackageSpecification("a", "type", "url", "ref1", "resref1"),
PackageSpecification("a", "type", "url", "ref2", "resref1"),
),
(
# resolved ref does not matter if no ref
PackageSpecification("a", "type", "url", None, "resref1"),
PackageSpecification("a", "type", "url", None, "resref2"),
),
(
# resolved ref unset when ref starts with other
PackageSpecification("a", "type", "url", "ref/a", "resref1"),
PackageSpecification("a", "type", "url", "ref", None),
),
(
# resolved ref unset when ref starts with other
PackageSpecification("a", "type", "url", "ref/a", None),
PackageSpecification("a", "type", "url", "ref", "resref2"),
),
],
)
def test_equal_specifications_have_same_hash(
spec1: PackageSpecification, spec2: PackageSpecification
) -> None:
assert spec1 == spec2
assert spec2 == spec1
assert hash(spec1) == hash(spec2)
@pytest.mark.parametrize(
"source_url,normalized_url",
[
("https://github.com/demo/demo.git", "https://github.com/demo/demo.git"),
("git@github.com:demo/demo.git", "ssh://git@github.com/demo/demo.git"),
],
)
def test_specification_normalize_source_url_method(
source_url: str, normalized_url: str
) -> None:
assert (
PackageSpecification._normalize_source_url("git", source_url) == normalized_url
)
assert (
PackageSpecification._normalize_source_url("notgit", source_url) == source_url
)
@pytest.mark.parametrize(
"source_url,normalized_url",
[
("https://github.com/demo/demo.git", "https://github.com/demo/demo.git"),
("git@github.com:demo/demo.git", "ssh://git@github.com/demo/demo.git"),
],
)
def test_specification_uses_normalize_source_url_for_git(
source_url: str, normalized_url: str
) -> None:
assert (
PackageSpecification(
name="demo",
source_type="git",
source_url=source_url,
).source_url
== normalized_url
)
|