Skip to content

Commit b5a4da5

Browse files
committed
Avoid deleting the user download cache
1 parent 8a59b16 commit b5a4da5

1 file changed

Lines changed: 29 additions & 27 deletions

File tree

‎astropy/utils/tests/test_data.py‎

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import pytest
1313

14+
from astropy.config.paths import set_temp_cache
1415
from astropy.utils.data import (_get_download_cache_locs, CacheMissingWarning,
1516
get_pkg_data_filename, get_readable_fileobj, conf)
1617

@@ -111,36 +112,37 @@ def test_download_noprogress():
111112

112113

113114
@pytest.mark.remote_data(source='astropy')
114-
def test_download_cache():
115+
def test_download_cache(tmpdir):
115116

116117
from astropy.utils.data import download_file, clear_download_cache
117118

118-
download_dir = _get_download_cache_locs()[0]
119-
120-
# Download the test URL and make sure it exists, then clear just that
121-
# URL and make sure it got deleted.
122-
fnout = download_file(TESTURL, cache=True)
123-
assert os.path.isdir(download_dir)
124-
assert os.path.isfile(fnout)
125-
clear_download_cache(TESTURL)
126-
assert not os.path.exists(fnout)
127-
128-
# Test issues raised in #4427 with clear_download_cache() without a URL,
129-
# followed by subsequent download.
130-
fnout = download_file(TESTURL, cache=True)
131-
assert os.path.isfile(fnout)
132-
clear_download_cache()
133-
assert not os.path.exists(fnout)
134-
assert not os.path.exists(download_dir)
135-
fnout = download_file(TESTURL, cache=True)
136-
assert os.path.isfile(fnout)
137-
138-
# Clearing download cache succeeds even if the URL does not exist.
139-
clear_download_cache('http://this_was_never_downloaded_before.com')
140-
141-
# Make sure lockdir was released
142-
lockdir = os.path.join(download_dir, 'lock')
143-
assert not os.path.isdir(lockdir), 'Cache dir lock was not released!'
119+
with set_temp_cache(tmpdir):
120+
download_dir = _get_download_cache_locs()[0]
121+
122+
# Download the test URL and make sure it exists, then clear just that
123+
# URL and make sure it got deleted.
124+
fnout = download_file(TESTURL, cache=True)
125+
assert os.path.isdir(download_dir)
126+
assert os.path.isfile(fnout)
127+
clear_download_cache(TESTURL)
128+
assert not os.path.exists(fnout)
129+
130+
# Test issues raised in #4427 with clear_download_cache() without a
131+
# URL, followed by subsequent download.
132+
fnout = download_file(TESTURL, cache=True)
133+
assert os.path.isfile(fnout)
134+
clear_download_cache()
135+
assert not os.path.exists(fnout)
136+
assert not os.path.exists(download_dir)
137+
fnout = download_file(TESTURL, cache=True)
138+
assert os.path.isfile(fnout)
139+
140+
# Clearing download cache succeeds even if the URL does not exist.
141+
clear_download_cache('http://this_was_never_downloaded_before.com')
142+
143+
# Make sure lockdir was released
144+
lockdir = os.path.join(download_dir, 'lock')
145+
assert not os.path.isdir(lockdir), 'Cache dir lock was not released!'
144146

145147

146148
@pytest.mark.remote_data(source='astropy')

0 commit comments

Comments
 (0)