@@ -257,6 +257,44 @@ def a_file(tmp_path):
257257 yield fn , contents
258258
259259
260+ def test_temp_cache (tmpdir ):
261+ dldir0 , urlmapfn0 = _get_download_cache_locs ()
262+ check_download_cache (check_hashes = True )
263+
264+ with paths .set_temp_cache (tmpdir ):
265+ dldir1 , urlmapfn1 = _get_download_cache_locs ()
266+ check_download_cache (check_hashes = True )
267+ assert dldir1 != dldir0
268+ assert urlmapfn1 != urlmapfn0
269+
270+ dldir2 , urlmapfn2 = _get_download_cache_locs ()
271+ check_download_cache (check_hashes = True )
272+ assert dldir2 != dldir1
273+ assert urlmapfn2 != urlmapfn1
274+ assert dldir2 == dldir0
275+ assert urlmapfn2 == urlmapfn0
276+
277+ # Check that things are okay even if we exit via an exception
278+ class Special (Exception ):
279+ pass
280+ try :
281+ with paths .set_temp_cache (tmpdir ):
282+ dldir3 , urlmapfn3 = _get_download_cache_locs ()
283+ check_download_cache (check_hashes = True )
284+ assert dldir3 == dldir1
285+ assert urlmapfn3 == urlmapfn1
286+ raise Special
287+ except Special :
288+ pass
289+
290+ dldir4 , urlmapfn4 = _get_download_cache_locs ()
291+ check_download_cache (check_hashes = True )
292+ assert dldir4 != dldir3
293+ assert urlmapfn4 != urlmapfn3
294+ assert dldir4 == dldir0
295+ assert urlmapfn4 == urlmapfn0
296+
297+
260298@pytest .mark .parametrize ("parallel" , [False , True ])
261299def test_download_with_sources_and_bogus_original (
262300 valid_urls , invalid_urls , temp_cache , parallel ):
@@ -1342,6 +1380,7 @@ def check_quietly_ignores_bogus_cache():
13421380 with pytest .raises (OSError ):
13431381 check_download_cache ()
13441382
1383+ dldir , urlmapfn = _get_download_cache_locs ()
13451384 # set_temp_cache acts weird if it is pointed at a file (see below)
13461385 # but we want to see what happens when the cache is pointed
13471386 # at a file instead of a directory, so make a directory we can
@@ -1356,12 +1395,14 @@ def check_quietly_ignores_bogus_cache():
13561395 with pytest .raises (OSError ):
13571396 paths .get_cache_dir ()
13581397 check_quietly_ignores_bogus_cache ()
1398+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13591399 assert get_file_contents (fn ) == ct , "File should not be harmed."
13601400
13611401 # See what happens when set_temp_cache is pointed at a file
13621402 with pytest .raises (OSError ):
13631403 with paths .set_temp_cache (fn ):
13641404 pass
1405+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13651406 assert get_file_contents (str (fn )) == ct
13661407
13671408 # Now the cache directory is normal but the subdirectory it wants
@@ -1371,6 +1412,7 @@ def check_quietly_ignores_bogus_cache():
13711412 f .write (ct )
13721413 with paths .set_temp_cache (tmpdir ):
13731414 check_quietly_ignores_bogus_cache ()
1415+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13741416 assert get_file_contents (cd ) == ct
13751417 os .remove (cd )
13761418
@@ -1381,6 +1423,7 @@ def check_quietly_ignores_bogus_cache():
13811423 f .write (ct )
13821424 with paths .set_temp_cache (tmpdir ):
13831425 check_quietly_ignores_bogus_cache ()
1426+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13841427 assert get_file_contents (cd ) == ct
13851428 os .remove (cd )
13861429
@@ -1392,6 +1435,7 @@ def check_quietly_ignores_bogus_cache():
13921435 f .write (ct )
13931436 with paths .set_temp_cache (tmpdir ):
13941437 check_quietly_ignores_bogus_cache ()
1438+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
13951439 assert get_file_contents (cd ) == ct
13961440 os .remove (cd )
13971441
@@ -1403,6 +1447,7 @@ def check_quietly_ignores_bogus_cache():
14031447 os .makedirs (cd )
14041448 with paths .set_temp_cache (tmpdir ):
14051449 check_quietly_ignores_bogus_cache ()
1450+ assert (dldir , urlmapfn ) == _get_download_cache_locs ()
14061451
14071452
14081453def test_get_fileobj_str (a_file ):
0 commit comments