Skip to content

Commit 169e2e4

Browse files
committed
Include subdirectory URL fragment in the cache key
1 parent 53770a7 commit 169e2e4

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

news/7333.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Include ``subdirectory`` URL fragments in the cache keys.

src/pip/_internal/cache.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def _get_cache_path_parts(self, link):
5858
key_parts = [link.url_without_fragment]
5959
if link.hash_name is not None and link.hash is not None:
6060
key_parts.append("=".join([link.hash_name, link.hash]))
61+
if link.subdirectory_fragment:
62+
key_parts.append(
63+
"=".join(["subdirectory", link.subdirectory_fragment])
64+
)
6165
key_url = "#".join(key_parts)
6266

6367
# Encode our key url with sha224, we'll use this because it has similar
@@ -168,11 +172,14 @@ def get(
168172
# type: (...) -> Link
169173
candidates = []
170174

175+
canonical_package_name = canonicalize_name(package_name)
171176
for wheel_name in self._get_candidates(link, package_name):
172177
try:
173178
wheel = Wheel(wheel_name)
174179
except InvalidWheelFilename:
175180
continue
181+
if wheel.name != canonical_package_name:
182+
continue
176183
if not wheel.supported(supported_tags):
177184
# Built for a different python/arch/etc
178185
continue

tests/unit/test_cache.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
from pip._internal.cache import WheelCache
2+
from pip._internal.models.format_control import FormatControl
3+
from pip._internal.models.link import Link
24
from pip._internal.utils.compat import expanduser
35

46

5-
class TestWheelCache:
7+
def test_expands_path():
8+
wc = WheelCache("~/.foo/", None)
9+
assert wc.cache_dir == expanduser("~/.foo/")
610

7-
def test_expands_path(self):
8-
wc = WheelCache("~/.foo/", None)
9-
assert wc.cache_dir == expanduser("~/.foo/")
1011

11-
def test_falsey_path_none(self):
12-
wc = WheelCache(False, None)
13-
assert wc.cache_dir is None
12+
def test_falsey_path_none():
13+
wc = WheelCache(False, None)
14+
assert wc.cache_dir is None
15+
16+
17+
def test_subdirectory_fragment():
18+
"""
19+
Test the subdirectory URL fragment is part of the cache key.
20+
"""
21+
wc = WheelCache("~/.foo/", None)
22+
link1 = Link("git+https://g.c/o/r#subdirectory=d1")
23+
link2 = Link("git+https://g.c/o/r#subdirectory=d2")
24+
assert wc.get_path_for_link(link1) != wc.get_path_for_link(link2)
25+
26+
27+
def test_wheel_name_filter(tmpdir):
28+
"""
29+
Test the wheel cache filters on wheel name when several wheels
30+
for different package are stored under the same cache directory.
31+
"""
32+
wc = WheelCache(tmpdir, FormatControl())
33+
link = Link("https://g.c/package.tar.gz")
34+
cache_path = wc.get_path_for_link(link)
35+
cache_path.mkdir(parents=True)
36+
(cache_path / "package-1.0-py3-none-any.whl").touch()
37+
# package matches wheel name
38+
assert wc.get(link, "package", [("py3", "none", "any")]) is not link
39+
# package2 does not match wheel name
40+
assert wc.get(link, "package2", [("py3", "none", "any")]) is link

0 commit comments

Comments
 (0)