Skip to content

Commit 73fd93f

Browse files
committed
Internal refactor: no default cache dirname
The two cache dirs are currently "refs" and "downloads". Make these always explicit, rather than `"downloads"` being an internal default baked into the downloader.
1 parent 79efd48 commit 73fd93f

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

src/check_jsonschema/cachedownloader.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _base_cache_dir() -> str | None:
3333
return cache_dir
3434

3535

36-
def _resolve_cache_dir(dirname: str = "downloads") -> str | None:
36+
def _resolve_cache_dir(dirname: str) -> str | None:
3737
cache_dir = _base_cache_dir()
3838
if cache_dir:
3939
cache_dir = os.path.join(cache_dir, "check_jsonschema", dirname)
@@ -100,13 +100,8 @@ class FailedDownloadError(Exception):
100100

101101

102102
class CacheDownloader:
103-
def __init__(
104-
self, cache_dir: str | None = None, disable_cache: bool = False
105-
) -> None:
106-
if cache_dir is None:
107-
self._cache_dir = _resolve_cache_dir()
108-
else:
109-
self._cache_dir = _resolve_cache_dir(cache_dir)
103+
def __init__(self, cache_dir: str, *, disable_cache: bool = False) -> None:
104+
self._cache_dir = _resolve_cache_dir(cache_dir)
110105
self._disable_cache = disable_cache
111106

112107
def _download(

src/check_jsonschema/schema_loader/readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(
7979
self.url = url
8080
self.parsers = ParserSet()
8181
self.downloader = CacheDownloader(
82-
disable_cache=disable_cache,
82+
"downloads", disable_cache=disable_cache
8383
).bind(url, cache_filename, validation_callback=self._parse)
8484
self._parsed_schema: dict | _UnsetType = _UNSET
8585

src/check_jsonschema/schema_loader/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def create_retrieve_callable(
6666
base_uri = retrieval_uri
6767

6868
cache = ResourceCache()
69-
downloader = CacheDownloader("refs", disable_cache)
69+
downloader = CacheDownloader("refs", disable_cache=disable_cache)
7070

7171
def get_local_file(uri: str) -> t.Any:
7272
path = filename2path(uri)

tests/unit/test_cachedownloader.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def default_response():
3030

3131

3232
def test_default_filename_from_uri(default_response):
33-
cd = CacheDownloader().bind("https://example.com/schema1.json")
33+
cd = CacheDownloader("downloads").bind("https://example.com/schema1.json")
3434
assert cd._filename == "schema1.json"
3535

3636

@@ -76,7 +76,7 @@ def fake_expanduser(path):
7676
monkeypatch.setattr(platform, "system", fakesystem)
7777
monkeypatch.setattr(os.path, "expanduser", fake_expanduser)
7878

79-
cd = CacheDownloader()
79+
cd = CacheDownloader("downloads")
8080
assert cd._cache_dir == expect_value
8181

8282
if sysname == "Darwin":
@@ -114,7 +114,7 @@ def test_cachedownloader_cached_file(tmp_path, monkeypatch, default_response):
114114
f.write_text("{}")
115115

116116
# set the cache_dir to the tmp dir (so that cache_dir will always be set)
117-
cd = CacheDownloader(cache_dir=tmp_path).bind(str(f))
117+
cd = CacheDownloader(tmp_path).bind(str(f))
118118
# patch the downloader to skip any download "work"
119119
monkeypatch.setattr(
120120
cd._downloader, "_download", lambda file_uri, filename, response_ok: str(f)
@@ -128,7 +128,7 @@ def test_cachedownloader_cached_file(tmp_path, monkeypatch, default_response):
128128
def test_cachedownloader_on_success(get_download_cache_loc, disable_cache):
129129
add_default_response()
130130
f = get_download_cache_loc("schema1.json")
131-
cd = CacheDownloader(disable_cache=disable_cache).bind(
131+
cd = CacheDownloader("downloads", disable_cache=disable_cache).bind(
132132
"https://example.com/schema1.json"
133133
)
134134

@@ -171,7 +171,7 @@ def test_cachedownloader_succeeds_after_few_errors(
171171
)
172172
add_default_response()
173173
f = get_download_cache_loc("schema1.json")
174-
cd = CacheDownloader(disable_cache=disable_cache).bind(
174+
cd = CacheDownloader("downloads", disable_cache=disable_cache).bind(
175175
"https://example.com/schema1.json"
176176
)
177177

@@ -205,7 +205,7 @@ def test_cachedownloader_fails_after_many_errors(
205205
)
206206
add_default_response() # never reached, the 11th response
207207
f = get_download_cache_loc("schema1.json")
208-
cd = CacheDownloader(disable_cache=disable_cache).bind(
208+
cd = CacheDownloader("downloads", disable_cache=disable_cache).bind(
209209
"https://example.com/schema1.json"
210210
)
211211
with pytest.raises(FailedDownloadError):
@@ -226,6 +226,7 @@ def test_cachedownloader_retries_on_bad_data(get_download_cache_loc, disable_cac
226226
add_default_response()
227227
f = get_download_cache_loc("schema1.json")
228228
cd = CacheDownloader(
229+
"downloads",
229230
disable_cache=disable_cache,
230231
).bind(
231232
"https://example.com/schema1.json",
@@ -281,7 +282,7 @@ def fake_mktime(*args):
281282
if file_exists:
282283
inject_cached_download(uri, original_file_contents)
283284

284-
cd = CacheDownloader().bind(uri)
285+
cd = CacheDownloader("downloads").bind(uri)
285286

286287
# if the file already existed, it will not be overwritten by the cachedownloader
287288
# so the returned value for both the downloader and a direct file read should be the
@@ -326,7 +327,7 @@ def dummy_validate_bytes(data):
326327

327328
# construct a downloader pointed at the schema and file, expecting a cache hit
328329
# and use the above validation method
329-
cd = CacheDownloader().bind(
330+
cd = CacheDownloader("downloads").bind(
330331
"https://example.com/schema1.json",
331332
validation_callback=dummy_validate_bytes,
332333
)

0 commit comments

Comments
 (0)