From d7adfad199419de0ecfc3d4401c7205b2e9c9a85 Mon Sep 17 00:00:00 2001 From: Jordan Cook Date: Mon, 23 Aug 2021 11:32:12 -0500 Subject: [PATCH] Fix incorrect location of redirects.sqlite when using filesystem backend --- HISTORY.md | 3 +++ requests_cache/backends/filesystem.py | 16 ++++++++++++---- tests/integration/base_cache_test.py | 4 ++++ tests/integration/test_filesystem.py | 5 ++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 4dc1c4ee..d431c2d5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -45,6 +45,9 @@ * Add aliases for previous names for backwards-compatibility ----- +### 0.7.5 (2021-TBD) +* Fix incorrect location of `redirects.sqlite` when using filesystem backend + ### 0.7.4 (2021-08-16) * Fix an issue with httpdate strings from `Expires` headers not getting converted to UTC * Fix a packaging issue with extra files added to top-level wheel directory diff --git a/requests_cache/backends/filesystem.py b/requests_cache/backends/filesystem.py index a70a6f7c..44d0470b 100644 --- a/requests_cache/backends/filesystem.py +++ b/requests_cache/backends/filesystem.py @@ -38,7 +38,7 @@ from contextlib import contextmanager from glob import glob from os import listdir, makedirs, unlink -from os.path import basename, dirname, join, splitext +from os.path import basename, join, splitext from pathlib import Path from pickle import PickleError from shutil import rmtree @@ -64,13 +64,19 @@ class FileCache(BaseCache): def __init__(self, cache_name: Union[Path, str] = 'http_cache', use_temp: bool = False, **kwargs): super().__init__(**kwargs) self.responses = FileDict(cache_name, use_temp=use_temp, **kwargs) - db_path = join(dirname(self.responses.cache_dir), 'redirects.sqlite') + db_path = join(self.responses.cache_dir, 'redirects.sqlite') self.redirects = SQLiteDict(db_path, 'redirects', **kwargs) def paths(self) -> List[str]: """Get absolute file paths to all cached responses""" return self.responses.paths() + def clear(self): + """Clear the cache""" + # FileDict.clear() removes and re-creates the cache directory, including redirects.sqlite + self.responses.clear() + self.redirects.init_db() + class FileDict(BaseStorage): """A dictionary-like interface to files on the local filesystem""" @@ -126,8 +132,7 @@ def __setitem__(self, key, value): f.write(self.serializer.dumps(value)) def __iter__(self): - for path in self.paths(): - yield splitext(basename(path))[0] + yield from self.keys() def __len__(self): return len(listdir(self.cache_dir)) @@ -137,6 +142,9 @@ def clear(self): rmtree(self.cache_dir, ignore_errors=True) makedirs(self.cache_dir) + def keys(self): + return [splitext(basename(path))[0] for path in self.paths()] + def paths(self) -> List[str]: """Get absolute file paths to all cached responses""" return glob(self._path('*')) diff --git a/tests/integration/base_cache_test.py b/tests/integration/base_cache_test.py index daa74a3c..9363c4bd 100644 --- a/tests/integration/base_cache_test.py +++ b/tests/integration/base_cache_test.py @@ -52,6 +52,10 @@ def init_session(self, clear=True, **kwargs) -> CachedSession: return CachedSession(backend=backend, **self.init_kwargs, **kwargs) + @classmethod + def teardown_class(cls): + cls().init_session(clear=True) + @pytest.mark.parametrize('serializer', TEST_SERIALIZERS.values()) @pytest.mark.parametrize('method', HTTPBIN_METHODS) @pytest.mark.parametrize('field', ['params', 'data', 'json']) diff --git a/tests/integration/test_filesystem.py b/tests/integration/test_filesystem.py index 3a601901..a8e72291 100644 --- a/tests/integration/test_filesystem.py +++ b/tests/integration/test_filesystem.py @@ -1,5 +1,5 @@ import pickle -from os.path import isfile +from os.path import dirname, isfile from shutil import rmtree from tempfile import gettempdir @@ -57,3 +57,6 @@ def test_paths(self, serializer_name): for path in session.cache.paths(): assert isfile(path) assert path.endswith(f'.{expected_extension}') + + # Redirects db should be in the same directory as response files + assert dirname(session.cache.redirects.db_path) == session.cache.responses.cache_dir