File size: 10,288 Bytes
d40299b | 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | from __future__ import annotations
import pytest
from packaging.direct_url import (
ArchiveInfo,
DirectUrl,
DirectUrlValidationError,
DirInfo,
VcsInfo,
_strip_url,
)
@pytest.mark.parametrize(
"direct_url_dict",
[
{
"url": "file:///projects/myproject",
"dir_info": {},
},
{
"url": "file:///projects/myproject",
"dir_info": {"editable": True},
},
{
"url": "file:///projects/myproject",
"dir_info": {"editable": False},
},
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hashes": {"sha256": "f" * 40},
},
},
{
"url": "https://g.c/user/repo.git",
"vcs_info": {
"vcs": "git",
"commit_id": "a" * 40,
"requested_revision": "main",
},
},
],
)
def test_direct_url_round_trips(direct_url_dict: dict[str, object]) -> None:
assert DirectUrl.from_dict(direct_url_dict).to_dict() == direct_url_dict
def test_legacy_hash_populates_hashes() -> None:
direct_url = DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hash": "sha256=" + "f" * 40,
},
}
)
assert direct_url.archive_info
assert direct_url.archive_info.hashes == {"sha256": "f" * 40}
def test_to_dict_generate_legacy_hash() -> None:
direct_url = DirectUrl(
url="https://example.com/archive.zip",
archive_info=ArchiveInfo(hashes={"sha256": "f" * 40}),
)
assert "hash" not in direct_url.to_dict()["archive_info"]
assert (
direct_url.to_dict(generate_legacy_hash=True)["archive_info"]["hash"]
== "sha256=" + "f" * 40
)
def test_to_dict_generate_legacy_hash_no_hashes() -> None:
direct_url = DirectUrl(
url="https://example.com/archive.zip",
archive_info=ArchiveInfo(),
)
assert "hash" not in direct_url.to_dict(generate_legacy_hash=True)["archive_info"]
def test_to_dict_generate_legacy_hash_multiple_hashes() -> None:
direct_url = DirectUrl(
url="https://example.com/archive.zip",
archive_info=ArchiveInfo(hashes={"sha256": "f" * 40, "md5": "1" * 32}),
)
assert (
direct_url.to_dict(generate_legacy_hash=True)["archive_info"]["hash"]
== "sha256=" + "f" * 40
)
def test_validate_archive_info_hashes() -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"Hash values must be strings in 'archive_info.hashes'",
):
DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hashes": {"md5": 12345},
},
}
)
def test_validate_archive_info_hash_invalid_format() -> None:
with pytest.raises(
DirectUrlValidationError,
match=(
r"Invalid hash format \(expected '<algorithm>=<hash>'\) "
r"in 'archive_info.hash'"
),
):
DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hash": "md5:12345",
},
}
)
def test_validate_archive_info_hash_missing_in_hashes() -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"Algorithm 'md5' used in hash field is not present in hashes field",
):
DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hashes": {"sha256": "f" * 40},
"hash": "md5=12345",
},
}
)
def test_validate_archive_info_hash_different_in_hashes() -> None:
with pytest.raises(
DirectUrlValidationError,
match=(
r"Algorithm 'md5' used in hash field has different value in hashes field "
r"in 'archive_info.hash'"
),
):
DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hashes": {"md5": "123456"},
"hash": "md5=12345",
},
}
)
def test_validate_archive_info_hash_same_in_hashes() -> None:
DirectUrl.from_dict(
{
"url": "https://example.com/archive.zip",
"archive_info": {
"hashes": {"md5": "123456"},
"hash": "md5=123456",
},
}
)
@pytest.mark.parametrize(
"direct_url_dict",
[
{
"url": "file:///projects/myproject",
},
{
"url": "https://example.com/archive.zip",
"archive_info": {},
"dir_info": {},
},
{
"url": "https://g.c/user/repo.git",
"vcs_info": {
"vcs": "git",
"commit_id": "a" * 40,
},
"archive_info": {},
},
],
)
def test_one_info_field(direct_url_dict: dict[str, object]) -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"Exactly one of vcs_info, archive_info, dir_info must be present",
):
DirectUrl.from_dict(direct_url_dict)
def test_dir_info_url_scheme_file() -> None:
DirectUrl.from_dict(
{
"url": "file:///home/myproject",
"dir_info": {},
}
)
with pytest.raises(
DirectUrlValidationError,
match=r"URL scheme must be file:// when dir_info is present",
):
DirectUrl.from_dict(
{
"url": "https://example.com/projects/myproject",
"dir_info": {},
}
)
@pytest.mark.parametrize(
"url",
[
"FILE:///home/myproject",
"File:///home/myproject",
"file:/home/myproject",
"file://localhost/home/myproject",
],
)
def test_dir_info_url_file_absolute_case_insensitive(url: str) -> None:
"""Per RFC 3986 §3.1, URL schemes are case-insensitive; all variants of
absolute file URLs must be accepted when dir_info is present."""
DirectUrl.from_dict({"url": url, "dir_info": {}})
@pytest.mark.parametrize(
"url",
[
"file:relative",
"file:./relative",
"file:",
"file://relative",
"file://localhost",
],
)
def test_dir_info_url_requires_absolute_file_url(url: str) -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"File URL must be absolute when dir_info is present",
):
DirectUrl.from_dict({"url": url, "dir_info": {}})
def test_missing_url() -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"Missing required value in 'url'",
):
DirectUrl.from_dict(
{
"dir_info": {},
}
)
def test_commit_id_type() -> None:
with pytest.raises(
DirectUrlValidationError,
match=r"Unexpected type int \(expected str\) in 'vcs_info.commit_id'",
):
DirectUrl.from_dict(
{
"url": "https://g.c/user/repo.git",
"vcs_info": {"vcs": "git", "commit_id": 12345},
}
)
def test_validate() -> None:
direct_url = DirectUrl(url="file:///projects/myproject", dir_info=DirInfo())
direct_url.validate()
def test_validate_error() -> None:
direct_url = DirectUrl(url="file:///projects/myproject")
with pytest.raises(DirectUrlValidationError):
direct_url.validate()
@pytest.mark.parametrize(
("url", "safe_user_passwords", "expected_url"),
[
("https://g.c/user/repo.git", ["git"], "https://g.c/user/repo.git"),
("https://user:pass@g.c/user/repo.git", ["git"], "https://g.c/user/repo.git"),
("https://user:pa@ss@g.c/user/repo.git", ["git"], "https://g.c/user/repo.git"),
("ssh://git@g.c/user/repo.git", [], "ssh://g.c/user/repo.git"),
("ssh://git@g.c/user/repo.git", ["git"], "ssh://git@g.c/user/repo.git"),
("ssh://cvs@g.c/user/repo.git", ["git"], "ssh://g.c/user/repo.git"),
("ssh://cvs@g.c/user/repo.git", ["git", "cvs"], "ssh://cvs@g.c/user/repo.git"),
(
"https://${USER}:${PASSWORD}@g.c/user/repo.git",
["git"],
"https://${USER}:${PASSWORD}@g.c/user/repo.git",
),
(
"https://user:${PASSWORD}@g.c/user/repo.git",
["git"],
"https://g.c/user/repo.git",
),
(
"https://git:${PASSWORD}@g.c/user/repo.git",
["git"],
"https://g.c/user/repo.git",
),
(
"https://${TOKEN}@g.c/user/repo.git",
["git"],
"https://${TOKEN}@g.c/user/repo.git",
),
],
)
def test_strip_url(url: str, safe_user_passwords: list[str], expected_url: str) -> None:
assert _strip_url(url, safe_user_passwords) == expected_url
def test_to_dict_strip_url() -> None:
direct_url = DirectUrl(
url="https://user:pass@g.c/user/repo.git",
vcs_info=VcsInfo(vcs="git", commit_id="a" * 40),
)
assert direct_url.to_dict()["url"] == "https://g.c/user/repo.git"
def test_to_dict_strip_url_with_at_in_password() -> None:
direct_url = DirectUrl(
url="https://user:pa@ss@g.c/user/repo.git",
vcs_info=VcsInfo(vcs="git", commit_id="a" * 40),
)
assert direct_url.to_dict()["url"] == "https://g.c/user/repo.git"
def test_to_dict_no_strip_url() -> None:
direct_url = DirectUrl(
url="https://user:pass@g.c/user/repo.git",
vcs_info=VcsInfo(vcs="git", commit_id="a" * 40),
)
assert (
direct_url.to_dict(strip_user_password=False)["url"]
== "https://user:pass@g.c/user/repo.git"
)
def test_to_dict_strip_url_git_safe_default() -> None:
direct_url = DirectUrl(
url="ssh://git@g.c/user/repo.git",
vcs_info=VcsInfo(vcs="git", commit_id="a" * 40),
)
assert direct_url.to_dict()["url"] == "ssh://git@g.c/user/repo.git"
|