5
5
import sys
6
6
import tempfile
7
7
from functools import partial
8
- from typing import Iterator , Tuple , cast
8
+ from typing import Iterator , Optional , Tuple , cast
9
9
from unittest import mock
10
10
11
11
import pytest
12
12
from pip ._vendor .packaging .markers import Marker
13
13
from pip ._vendor .packaging .requirements import Requirement
14
14
15
+ from pip ._internal .cache import WheelCache
15
16
from pip ._internal .commands import create_command
16
17
from pip ._internal .commands .install import InstallCommand
17
18
from pip ._internal .exceptions import (
22
23
)
23
24
from pip ._internal .index .package_finder import PackageFinder
24
25
from pip ._internal .metadata import select_backend
25
- from pip ._internal .models .direct_url import ArchiveInfo , DirInfo , VcsInfo
26
+ from pip ._internal .models .direct_url import ArchiveInfo , DirectUrl , DirInfo , VcsInfo
27
+ from pip ._internal .models .format_control import FormatControl
28
+ from pip ._internal .models .link import Link
26
29
from pip ._internal .network .session import PipSession
27
30
from pip ._internal .operations .build .build_tracker import get_build_tracker
28
31
from pip ._internal .operations .prepare import RequirementPreparer
43
46
)
44
47
from pip ._internal .resolution .legacy .resolver import Resolver
45
48
from pip ._internal .utils .urls import path_to_url
46
- from tests .lib import TestData , make_test_finder , requirements_file
49
+ from tests .lib import TestData , make_test_finder , requirements_file , wheel
47
50
from tests .lib .path import Path
48
51
49
52
@@ -77,7 +80,10 @@ def teardown(self) -> None:
77
80
78
81
@contextlib .contextmanager
79
82
def _basic_resolver (
80
- self , finder : PackageFinder , require_hashes : bool = False
83
+ self ,
84
+ finder : PackageFinder ,
85
+ require_hashes : bool = False ,
86
+ wheel_cache : Optional [WheelCache ] = None ,
81
87
) -> Iterator [Resolver ]:
82
88
make_install_req = partial (
83
89
install_req_from_req_string ,
@@ -106,7 +112,7 @@ def _basic_resolver(
106
112
preparer = preparer ,
107
113
make_install_req = make_install_req ,
108
114
finder = finder ,
109
- wheel_cache = None ,
115
+ wheel_cache = wheel_cache ,
110
116
use_user_site = False ,
111
117
upgrade_strategy = "to-satisfy-only" ,
112
118
ignore_dependencies = False ,
@@ -366,7 +372,6 @@ def test_download_info_index_url(self) -> None:
366
372
req = reqset .all_requirements [0 ]
367
373
assert req .download_info
368
374
assert isinstance (req .download_info .info , ArchiveInfo )
369
- assert req .download_info .info .hash
370
375
371
376
@pytest .mark .network
372
377
def test_download_info_web_archive (self ) -> None :
@@ -391,6 +396,53 @@ def test_download_info_web_archive(self) -> None:
391
396
"ad977496000576e1b6c41f6449a9897087ce9da6db4f15b603fe8372af4bf3c6"
392
397
)
393
398
399
+ def test_download_info_archive_legacy_cache (
400
+ self , tmp_path : Path , shared_data : TestData
401
+ ) -> None :
402
+ """Test download_info hash is not set for an archive with legacy cache entry."""
403
+ url = path_to_url (shared_data .packages / "simple-1.0.tar.gz" )
404
+ finder = make_test_finder ()
405
+ wheel_cache = WheelCache (str (tmp_path / "cache" ), FormatControl ())
406
+ cache_entry_dir = wheel_cache .get_path_for_link (Link (url ))
407
+ Path (cache_entry_dir ).mkdir (parents = True )
408
+ wheel .make_wheel (name = "simple" , version = "1.0" ).save_to_dir (cache_entry_dir )
409
+ with self ._basic_resolver (finder , wheel_cache = wheel_cache ) as resolver :
410
+ ireq = get_processed_req_from_line (f"simple @ { url } " )
411
+ reqset = resolver .resolve ([ireq ], True )
412
+ assert len (reqset .all_requirements ) == 1
413
+ req = reqset .all_requirements [0 ]
414
+ assert req .original_link_is_in_wheel_cache
415
+ assert req .download_info
416
+ assert req .download_info .url == url
417
+ assert isinstance (req .download_info .info , ArchiveInfo )
418
+ assert not req .download_info .info .hash
419
+
420
+ def test_download_info_archive_cache_with_origin (
421
+ self , tmp_path : Path , shared_data : TestData
422
+ ) -> None :
423
+ """Test download_info hash is set for a web archive with cache entry
424
+ that has origin.json."""
425
+ url = path_to_url (shared_data .packages / "simple-1.0.tar.gz" )
426
+ hash = "sha256=ad977496000576e1b6c41f6449a9897087ce9da6db4f15b603fe8372af4bf3c6"
427
+ finder = make_test_finder ()
428
+ wheel_cache = WheelCache (str (tmp_path / "cache" ), FormatControl ())
429
+ cache_entry_dir = wheel_cache .get_path_for_link (Link (url ))
430
+ Path (cache_entry_dir ).mkdir (parents = True )
431
+ Path (cache_entry_dir ).joinpath ("origin.json" ).write_text (
432
+ DirectUrl (url , ArchiveInfo (hash = hash )).to_json ()
433
+ )
434
+ wheel .make_wheel (name = "simple" , version = "1.0" ).save_to_dir (cache_entry_dir )
435
+ with self ._basic_resolver (finder , wheel_cache = wheel_cache ) as resolver :
436
+ ireq = get_processed_req_from_line (f"simple @ { url } " )
437
+ reqset = resolver .resolve ([ireq ], True )
438
+ assert len (reqset .all_requirements ) == 1
439
+ req = reqset .all_requirements [0 ]
440
+ assert req .original_link_is_in_wheel_cache
441
+ assert req .download_info
442
+ assert req .download_info .url == url
443
+ assert isinstance (req .download_info .info , ArchiveInfo )
444
+ assert req .download_info .info .hash == hash
445
+
394
446
def test_download_info_local_wheel (self , data : TestData ) -> None :
395
447
"""Test that download_info is set for requirements from a local wheel."""
396
448
finder = make_test_finder ()
0 commit comments