@@ -234,6 +234,43 @@ def a_file(tmp_path):
234234 yield fn , contents
235235
236236
237+ def test_temp_cache (tmpdir ):
238+ dldir0 , urlmapfn0 = _get_download_cache_locs ()
239+ check_download_cache (check_hashes = True )
240+
241+ with paths .set_temp_cache (tmpdir ):
242+ dldir1 , urlmapfn1 = _get_download_cache_locs ()
243+ check_download_cache (check_hashes = True )
244+ assert dldir1 != dldir0
245+ assert urlmapfn1 != urlmapfn0
246+
247+ dldir2 , urlmapfn2 = _get_download_cache_locs ()
248+ check_download_cache (check_hashes = True )
249+ assert dldir2 != dldir1
250+ assert urlmapfn2 != urlmapfn1
251+ assert dldir2 == dldir0
252+ assert urlmapfn2 == urlmapfn0
253+
254+ class Special (Exception ):
255+ pass
256+ try :
257+ with paths .set_temp_cache (tmpdir ):
258+ dldir3 , urlmapfn3 = _get_download_cache_locs ()
259+ check_download_cache (check_hashes = True )
260+ assert dldir3 == dldir1
261+ assert urlmapfn3 == urlmapfn1
262+ raise Special
263+ except Special :
264+ pass
265+
266+ dldir4 , urlmapfn4 = _get_download_cache_locs ()
267+ check_download_cache (check_hashes = True )
268+ assert dldir4 != dldir3
269+ assert urlmapfn4 != urlmapfn3
270+ assert dldir4 == dldir0
271+ assert urlmapfn4 == urlmapfn0
272+
273+
237274@pytest .mark .parametrize ("parallel" , [False , True ])
238275def test_download_with_sources_and_bogus_original (
239276 valid_urls , invalid_urls , temp_cache , parallel ):
@@ -1318,6 +1355,7 @@ def check_quietly_ignores_bogus_cache():
13181355 with pytest .raises (OSError ):
13191356 check_download_cache ()
13201357
1358+ dldir , urlmapfn = _get_download_cache_locs ()
13211359 # set_temp_cache acts weird if it is pointed at a file (see below)
13221360 # but we want to see what happens when the cache is pointed
13231361 # at a file instead of a directory, so make a directory we can
@@ -1332,12 +1370,14 @@ def check_quietly_ignores_bogus_cache():
13321370 with pytest .raises (OSError ):
13331371 paths .get_cache_dir ()
13341372 check_quietly_ignores_bogus_cache ()
1373+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13351374 assert get_file_contents (fn ) == ct , "File should not be harmed."
13361375
13371376 # See what happens when set_temp_cache is pointed at a file
13381377 with pytest .raises (OSError ):
13391378 with paths .set_temp_cache (fn ):
13401379 pass
1380+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13411381 assert get_file_contents (str (fn )) == ct
13421382
13431383 # Now the cache directory is normal but the subdirectory it wants
@@ -1347,6 +1387,7 @@ def check_quietly_ignores_bogus_cache():
13471387 f .write (ct )
13481388 with paths .set_temp_cache (tmpdir ):
13491389 check_quietly_ignores_bogus_cache ()
1390+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13501391 assert get_file_contents (cd ) == ct
13511392 os .remove (cd )
13521393
@@ -1357,6 +1398,7 @@ def check_quietly_ignores_bogus_cache():
13571398 f .write (ct )
13581399 with paths .set_temp_cache (tmpdir ):
13591400 check_quietly_ignores_bogus_cache ()
1401+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13601402 assert get_file_contents (cd ) == ct
13611403 os .remove (cd )
13621404
@@ -1368,6 +1410,7 @@ def check_quietly_ignores_bogus_cache():
13681410 f .write (ct )
13691411 with paths .set_temp_cache (tmpdir ):
13701412 check_quietly_ignores_bogus_cache ()
1413+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13711414 assert get_file_contents (cd ) == ct
13721415 os .remove (cd )
13731416
@@ -1379,7 +1422,7 @@ def check_quietly_ignores_bogus_cache():
13791422 os .makedirs (cd )
13801423 with paths .set_temp_cache (tmpdir ):
13811424 check_quietly_ignores_bogus_cache ()
1382-
1425+ assert ( dldir , urlmapfn ) == _get_download_cache_locs ()
13831426
13841427def test_get_fileobj_str (a_file ):
13851428 fn , c = a_file
0 commit comments