Skip to content

Commit 577bb1f

Browse files
aignasgemini-code-assist[bot]
authored andcommitted
fix(pypi): correctly write the used facts back (bazel-contrib#3719)
Summary: 1. Added 3 new tests to pypi_cache_tests.bzl 2. Fixed `_cache` helper to handle both struct and dict return values 3. Fixed `_filter_packages`: Returns None for empty dict results instead of empty dict 4. Fixed `_get_from_facts`: Now stores facts when reading index_urls from the dict case Fixes bazel-contrib#3711 Fixes bazel-contrib#3707 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> (cherry picked from commit 6da1431)
1 parent 8c726cb commit 577bb1f

2 files changed

Lines changed: 104 additions & 2 deletions

File tree

python/private/pypi/pypi_cache.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,12 @@ def _filter_packages(dists, requested_versions):
128128
return dists
129129

130130
if type(dists) == "dict":
131-
return {
131+
result = {
132132
pkg: url
133133
for pkg, url in dists.items()
134134
if pkg in requested_versions
135135
}
136+
return result if result else None
136137

137138
sha256s_by_version = {}
138139
whls = {}
@@ -206,10 +207,13 @@ def _get_from_facts(facts, known_facts, index_url, requested_versions, facts_ver
206207
return None
207208

208209
if type(requested_versions) == "dict":
209-
return _filter_packages(
210+
result = _filter_packages(
210211
dists = known_facts.get("index_urls", {}).get(index_url, {}),
211212
requested_versions = requested_versions,
212213
)
214+
if result:
215+
_store_facts(facts, facts_version, index_url, result)
216+
return result
213217

214218
known_sources = {}
215219

tests/pypi/pypi_cache/pypi_cache_tests.bzl

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ def _cache(env, **kwargs):
1919
if not value:
2020
return env.expect.that_str(value)
2121

22+
if type(value) == "dict":
23+
return env.expect.that_dict(value)
24+
2225
return env.expect.that_struct(
2326
value,
2427
attrs = attrs,
@@ -265,6 +268,101 @@ def _test_pypi_cache_reads_from_facts(env):
265268

266269
_tests.append(_test_pypi_cache_reads_from_facts)
267270

271+
def _test_memory_cache_index_urls(env):
272+
"""Verifies that the cache returns stored values for index_urls."""
273+
store = {}
274+
cache = _cache(env, mctx = None, store = store)
275+
276+
fake_result = {
277+
"pkg-a": "https://pypi.org/simple/pkg-a/",
278+
"pkg_b": "https://pypi.org/simple/pkg-b/",
279+
}
280+
281+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None, "pkg_b": None})
282+
283+
cache.setdefault(key, fake_result)
284+
285+
got = cache.get(key)
286+
got.contains_exactly(fake_result)
287+
288+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None})
289+
got = cache.get(key)
290+
got.contains_exactly({"pkg-a": "https://pypi.org/simple/pkg-a/"})
291+
292+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-c": None})
293+
cache.get(key).equals(None)
294+
295+
_tests.append(_test_memory_cache_index_urls)
296+
297+
def _test_pypi_cache_writes_index_urls_to_facts(env):
298+
"""Verifies that setting index_urls in the cache also populates the facts store."""
299+
mock_ctx = mocks.mctx(facts = {})
300+
cache = _cache(env, mctx = mock_ctx)
301+
302+
fake_result = {
303+
"pkg-a": "https://pypi.org/simple/pkg-a/",
304+
"pkg_b": "https://pypi.org/simple/pkg-b/",
305+
}
306+
307+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None})
308+
309+
cache.setdefault(key, fake_result)
310+
311+
cache.get_facts().contains_exactly({
312+
"fact_version": "v1",
313+
"index_urls": {
314+
"https://pypi.org/simple/": {
315+
"pkg-a": "https://pypi.org/simple/pkg-a/",
316+
},
317+
},
318+
})
319+
320+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg_b": None})
321+
cache.setdefault(key, fake_result)
322+
323+
cache.get_facts().contains_exactly({
324+
"fact_version": "v1",
325+
"index_urls": {
326+
"https://pypi.org/simple/": {
327+
"pkg-a": "https://pypi.org/simple/pkg-a/",
328+
"pkg_b": "https://pypi.org/simple/pkg-b/",
329+
},
330+
},
331+
})
332+
333+
_tests.append(_test_pypi_cache_writes_index_urls_to_facts)
334+
335+
def _test_pypi_cache_reads_index_urls_from_facts(env):
336+
"""Verifies that reading index_urls from facts works correctly."""
337+
mock_ctx = mocks.mctx(facts = {
338+
"fact_version": "v1",
339+
"index_urls": {
340+
"https://pypi.org/simple/": {
341+
"pkg-a": "https://pypi.org/simple/pkg-a/",
342+
"pkg-b": "https://pypi.org/simple/pkg-b/",
343+
},
344+
},
345+
})
346+
cache = _cache(env, mctx = mock_ctx)
347+
348+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None})
349+
got = cache.get(key)
350+
got.contains_exactly({"pkg-a": "https://pypi.org/simple/pkg-a/"})
351+
352+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-a": None, "pkg-b": None})
353+
got = cache.get(key)
354+
got.contains_exactly({
355+
"pkg-a": "https://pypi.org/simple/pkg-a/",
356+
"pkg-b": "https://pypi.org/simple/pkg-b/",
357+
})
358+
359+
key = ("https://pypi.org/simple/", "https://pypi.org/simple/", {"pkg-c": None})
360+
cache.get(key).equals(None)
361+
362+
cache.get_facts().contains_exactly(mock_ctx.facts)
363+
364+
_tests.append(_test_pypi_cache_reads_index_urls_from_facts)
365+
268366
def pypi_cache_test_suite(name):
269367
test_suite(
270368
name = name,

0 commit comments

Comments
 (0)