@@ -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+
268366def pypi_cache_test_suite (name ):
269367 test_suite (
270368 name = name ,
0 commit comments