From c8e4e84b750bfba12d3113b8752d200e400b9195 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Sat, 17 Aug 2024 21:24:05 +0200 Subject: [PATCH 1/9] feat: add test to solve the issue --- test/test_cache.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/test/test_cache.py b/test/test_cache.py index 7da5c907..5793f12e 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -1,10 +1,30 @@ +import platform from pathlib import Path import pretend # type: ignore +import pytest from packaging.version import Version import pip_audit._cache as cache -from pip_audit._cache import _get_cache_dir, _get_pip_cache +from pip_audit._cache import _get_cache_dir, _get_internal_cache_path, _get_pip_cache + + +@pytest.mark.parametrize( + "sys_platform,expected,is_xdg_set", + [ + ("Linux", Path.home() / ".cache" / "pip-audit", False), + ("Darwin", Path.home() / "Library" / "Caches" / "pip-audit", False), + ("Windows", Path.home() / ".cache" / "pip-audit", False), + ("Linux", Path("/tmp/foo/cache_dir/pip-audit"), True), + ("Darwin", Path.home() / "Library" / "Caches" / "pip-audit", True), + ("Windows", Path("/tmp/foo/cache_dir/pip-audit"), True), + ], +) +def test_get_internal_cache_path(monkeypatch, sys_platform, expected, is_xdg_set): + monkeypatch.setattr(platform, "system", lambda: sys_platform) + if is_xdg_set: + monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/foo/cache_dir") + assert _get_internal_cache_path() == expected def test_get_cache_dir(monkeypatch): From a43351f29e7a8a3221ba1f5dcf5f8f46525603b9 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Sat, 17 Aug 2024 21:25:32 +0200 Subject: [PATCH 2/9] feat: more respectful cache location This commit implements cache's location as described in #342. It default to the XDG Base Directory Specification excepts on macOS where it is located at `$HOME/Library/Caches/pip-audit`. Closes #342 --- pip_audit/_cache.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index 41d6e462..65faf65b 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -6,6 +6,7 @@ import logging import os +import platform import subprocess import sys from pathlib import Path @@ -28,7 +29,24 @@ _PIP_VERSION = Version(str(pip_api.PIP_VERSION)) -_PIP_AUDIT_INTERNAL_CACHE = Path.home() / ".pip-audit-cache" + +def _get_internal_cache_path() -> Path: + """ + Returns the default cache directory path used internally by `pip-audit`. + + This should be used as a fallback when other caching options are not specified. + + On macOS, it will be in `$HOME/Library/Caches/pip_audit`. + + On other OS, it will follow the `XDG Base Directory Specification` and + place the cache in either `$HOME/.cache/pip-audit` or respect `$XDG_CACHE_HOME` + environment variable i-e `$XDG_CACHE_HOME/pip-audit`. + """ + return ( + (Path.home() / "Library" / "Caches" / "pip-audit") + if platform.system() == "Darwin" + else (Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "pip-audit") + ) def _get_pip_cache() -> Path: @@ -60,6 +78,9 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa if custom_cache_dir is not None: return custom_cache_dir + # Retrieve pip-audit's default internal cache path following OS conventions. + pip_audit_cache_dir = _get_internal_cache_path() + # Respect pip's PIP_NO_CACHE_DIR environment setting. if use_pip and not os.getenv("PIP_NO_CACHE_DIR"): pip_cache_dir = _get_pip_cache() if _PIP_VERSION >= _MINIMUM_PIP_VERSION else None @@ -68,11 +89,11 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa else: logger.warning( f"pip {_PIP_VERSION} doesn't support the `cache dir` subcommand, " - f"using {_PIP_AUDIT_INTERNAL_CACHE} instead" + f"using {pip_audit_cache_dir} instead" ) - return _PIP_AUDIT_INTERNAL_CACHE + return pip_audit_cache_dir else: - return _PIP_AUDIT_INTERNAL_CACHE + return pip_audit_cache_dir class _SafeFileCache(FileCache): From 5433cd974ce6ded72910845dc3a1aabbae185fda Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Sun, 18 Aug 2024 12:00:39 +0200 Subject: [PATCH 3/9] feat: add test for Windows specific location --- test/test_cache.py | 49 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index 5793f12e..75838bb2 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -10,20 +10,53 @@ @pytest.mark.parametrize( - "sys_platform,expected,is_xdg_set", + "sys_platform,expected,is_xdg_set,is_local_app_data_set", [ - ("Linux", Path.home() / ".cache" / "pip-audit", False), - ("Darwin", Path.home() / "Library" / "Caches" / "pip-audit", False), - ("Windows", Path.home() / ".cache" / "pip-audit", False), - ("Linux", Path("/tmp/foo/cache_dir/pip-audit"), True), - ("Darwin", Path.home() / "Library" / "Caches" / "pip-audit", True), - ("Windows", Path("/tmp/foo/cache_dir/pip-audit"), True), + pytest.param( + "Linux", + Path.home() / ".cache" / "pip-audit", + False, + False, + id="default XDG's convention cache on Linux", + ), + pytest.param( + "Darwin", + Path.home() / "Library" / "Caches" / "pip-audit", + False, + False, + id="default cache on macOS", + ), + pytest.param( + "Windows", + Path.home() / ".cache" / "pip-audit", + False, + False, + id="default fallback cache on Windows", + ), + pytest.param( + "Linux", + Path("/tmp/foo/cache_dir/pip-audit"), + True, + False, + id="custom cache dir on Linux with XDG_CACHE_HOME set", + ), + pytest.param( + "Windows", + Path("/tmp/bar/cache_dir/pip-audit/Cache"), + False, + True, + id="custom cache dir on Windows with LOCALAPPDATA set", + ), ], ) -def test_get_internal_cache_path(monkeypatch, sys_platform, expected, is_xdg_set): +def test_get_internal_cache_path( + monkeypatch, sys_platform, expected, is_xdg_set, is_local_app_data_set +): monkeypatch.setattr(platform, "system", lambda: sys_platform) if is_xdg_set: monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/foo/cache_dir") + if is_local_app_data_set: + monkeypatch.setenv("LOCALAPPDATA", "/tmp/bar/cache_dir") assert _get_internal_cache_path() == expected From d191416491d1825008595036f6378a5891ad8e27 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Sun, 18 Aug 2024 12:02:18 +0200 Subject: [PATCH 4/9] feat: add Windows cache implementation On Windows, although not "officially" a convention, it appears that programs like `pip` use %LocalAppData%\\Cache. This commit implements that default. See https://pip.pypa.io/en/latest/topics/caching/#default-paths. Closes #342. --- pip_audit/_cache.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index 65faf65b..b5581d7d 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -34,19 +34,36 @@ def _get_internal_cache_path() -> Path: """ Returns the default cache directory path used internally by `pip-audit`. - This should be used as a fallback when other caching options are not specified. + It uses the same pip's default paths to select the caching directory. + See https://pip.pypa.io/en/latest/topics/caching/#default-paths. - On macOS, it will be in `$HOME/Library/Caches/pip_audit`. + On macOS, it will be in `$HOME/Library/Caches/pip-audit`. + + On Windows, it will be in `%LOCALAPPDATA%/Cache/pip-audit` and + fallback to the `XDG Base Directory Specification` default folder. On other OS, it will follow the `XDG Base Directory Specification` and place the cache in either `$HOME/.cache/pip-audit` or respect `$XDG_CACHE_HOME` environment variable i-e `$XDG_CACHE_HOME/pip-audit`. """ - return ( - (Path.home() / "Library" / "Caches" / "pip-audit") - if platform.system() == "Darwin" - else (Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "pip-audit") - ) + os_platform = platform.system() + default_cache_dir = Path.home() / ".cache" / "pip-audit" + + if os_platform == "Windows": + return ( + default_cache_dir + if (local_app_data := os.getenv("LOCALAPPDATA")) is None + else Path(local_app_data) / "pip-audit" / "Cache" + ) + elif os_platform == "Darwin": + return Path.home() / "Library" / "Caches" / "pip-audit" + else: + # Follow XDG Base Directory Specification + return ( + default_cache_dir + if (xdg_cache_home := os.getenv("XDG_CACHE_HOME")) is None + else Path(xdg_cache_home) / "pip-audit" + ) def _get_pip_cache() -> Path: From 4fe41f31116ec487ad7a007c37bcedf05d91de7b Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Sun, 25 Aug 2024 13:39:12 +0200 Subject: [PATCH 5/9] feat: use platformdirs for cache location --- pip_audit/_cache.py | 42 +------------ pyproject.toml | 1 + test/test_cache.py | 144 ++++++++++++++++++++++++++------------------ 3 files changed, 91 insertions(+), 96 deletions(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index b5581d7d..6c20d890 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -6,7 +6,6 @@ import logging import os -import platform import subprocess import sys from pathlib import Path @@ -18,6 +17,7 @@ from cachecontrol import CacheControl from cachecontrol.caches import FileCache from packaging.version import Version +from platformdirs import user_cache_path from pip_audit._service.interface import ServiceError @@ -30,42 +30,6 @@ _PIP_VERSION = Version(str(pip_api.PIP_VERSION)) -def _get_internal_cache_path() -> Path: - """ - Returns the default cache directory path used internally by `pip-audit`. - - It uses the same pip's default paths to select the caching directory. - See https://pip.pypa.io/en/latest/topics/caching/#default-paths. - - On macOS, it will be in `$HOME/Library/Caches/pip-audit`. - - On Windows, it will be in `%LOCALAPPDATA%/Cache/pip-audit` and - fallback to the `XDG Base Directory Specification` default folder. - - On other OS, it will follow the `XDG Base Directory Specification` and - place the cache in either `$HOME/.cache/pip-audit` or respect `$XDG_CACHE_HOME` - environment variable i-e `$XDG_CACHE_HOME/pip-audit`. - """ - os_platform = platform.system() - default_cache_dir = Path.home() / ".cache" / "pip-audit" - - if os_platform == "Windows": - return ( - default_cache_dir - if (local_app_data := os.getenv("LOCALAPPDATA")) is None - else Path(local_app_data) / "pip-audit" / "Cache" - ) - elif os_platform == "Darwin": - return Path.home() / "Library" / "Caches" / "pip-audit" - else: - # Follow XDG Base Directory Specification - return ( - default_cache_dir - if (xdg_cache_home := os.getenv("XDG_CACHE_HOME")) is None - else Path(xdg_cache_home) / "pip-audit" - ) - - def _get_pip_cache() -> Path: # Unless the cache directory is specifically set by the `--cache-dir` option, we try to share # the `pip` HTTP cache @@ -95,8 +59,8 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa if custom_cache_dir is not None: return custom_cache_dir - # Retrieve pip-audit's default internal cache path following OS conventions. - pip_audit_cache_dir = _get_internal_cache_path() + # Retrieve pip-audit's default internal cache using `platformdirs`. + pip_audit_cache_dir = user_cache_path("pip-audit", appauthor=False, ensure_exists=True) # Respect pip's PIP_NO_CACHE_DIR environment setting. if use_pip and not os.getenv("PIP_NO_CACHE_DIR"): diff --git a/pyproject.toml b/pyproject.toml index 57feb10a..461bdb3f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,6 +36,7 @@ dependencies = [ "requests >= 2.31.0", "rich>=12.4", "toml>=0.10", + "platformdirs>=4.2.0" ] requires-python = ">=3.8" diff --git a/test/test_cache.py b/test/test_cache.py index 75838bb2..b2b6702b 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -1,63 +1,27 @@ -import platform +import importlib +import sys from pathlib import Path +import platformdirs import pretend # type: ignore import pytest from packaging.version import Version +from pytest import MonkeyPatch import pip_audit._cache as cache -from pip_audit._cache import _get_cache_dir, _get_internal_cache_path, _get_pip_cache +from pip_audit._cache import _get_cache_dir, _get_pip_cache -@pytest.mark.parametrize( - "sys_platform,expected,is_xdg_set,is_local_app_data_set", - [ - pytest.param( - "Linux", - Path.home() / ".cache" / "pip-audit", - False, - False, - id="default XDG's convention cache on Linux", - ), - pytest.param( - "Darwin", - Path.home() / "Library" / "Caches" / "pip-audit", - False, - False, - id="default cache on macOS", - ), - pytest.param( - "Windows", - Path.home() / ".cache" / "pip-audit", - False, - False, - id="default fallback cache on Windows", - ), - pytest.param( - "Linux", - Path("/tmp/foo/cache_dir/pip-audit"), - True, - False, - id="custom cache dir on Linux with XDG_CACHE_HOME set", - ), - pytest.param( - "Windows", - Path("/tmp/bar/cache_dir/pip-audit/Cache"), - False, - True, - id="custom cache dir on Windows with LOCALAPPDATA set", - ), - ], -) -def test_get_internal_cache_path( - monkeypatch, sys_platform, expected, is_xdg_set, is_local_app_data_set -): - monkeypatch.setattr(platform, "system", lambda: sys_platform) - if is_xdg_set: - monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/foo/cache_dir") - if is_local_app_data_set: - monkeypatch.setenv("LOCALAPPDATA", "/tmp/bar/cache_dir") - assert _get_internal_cache_path() == expected +def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: + """Utility function to patch `platformdirs` in order to test cross-platforms.""" + # Mocking OS host + monkeypatch.setattr(sys, "platform", sys_platform) + # We are forced to reload `platformdirs` to get the correct cache directory + # as cache definition is stored in the top level `__init__.py` file of the + # `platformdirs` package + importlib.reload(platformdirs) + if sys_platform == "win32": + monkeypatch.setenv("LOCALAPPDATA", "/tmp/AppData/Local") def test_get_cache_dir(monkeypatch): @@ -79,22 +43,88 @@ def test_get_pip_cache(): assert cache_dir.stem == "http" -def test_get_cache_dir_do_not_use_pip(): +@pytest.mark.parametrize( + "sys_platform,expected", + [ + pytest.param( + "linux", + Path.home() / ".cache" / "pip-audit", + id="on Linux", + ), + pytest.param( + "win32", + Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache", + id="on Windows", + ), + pytest.param( + "darwin", + Path.home() / "Library" / "Caches" / "pip-audit", + id="on MacOS", + ), + ], +) +def test_get_cache_dir_do_not_use_pip(monkeypatch, sys_platform, expected): + # Check cross-platforms + _patch_platformdirs(monkeypatch, sys_platform) # Even with None, we never use the pip cache if we're told not to. cache_dir = _get_cache_dir(None, use_pip=False) - assert cache_dir == Path.home() / ".pip-audit-cache" + assert cache_dir == expected -def test_get_cache_dir_pip_disabled_in_environment(monkeypatch): +@pytest.mark.parametrize( + "sys_platform,expected", + [ + pytest.param( + "linux", + Path.home() / ".cache" / "pip-audit", + id="on Linux", + ), + pytest.param( + "win32", + Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache", + id="on Windows", + ), + pytest.param( + "darwin", + Path.home() / "Library" / "Caches" / "pip-audit", + id="on MacOS", + ), + ], +) +def test_get_cache_dir_pip_disabled_in_environment(monkeypatch, sys_platform, expected): monkeypatch.setenv("PIP_NO_CACHE_DIR", "1") + # Check cross-platforms + _patch_platformdirs(monkeypatch, sys_platform) # Even with use_pip=True, we avoid pip's cache if the environment tells us to. - assert _get_cache_dir(None, use_pip=True) == Path.home() / ".pip-audit-cache" + assert _get_cache_dir(None, use_pip=True) == expected -def test_get_cache_dir_old_pip(monkeypatch): +@pytest.mark.parametrize( + "sys_platform,expected", + [ + pytest.param( + "linux", + Path.home() / ".cache" / "pip-audit", + id="on Linux", + ), + pytest.param( + "win32", + Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache", + id="on Windows", + ), + pytest.param( + "darwin", + Path.home() / "Library" / "Caches" / "pip-audit", + id="on MacOS", + ), + ], +) +def test_get_cache_dir_old_pip(monkeypatch, sys_platform, expected): # Check the case where we have an old `pip` monkeypatch.setattr(cache, "_PIP_VERSION", Version("1.0.0")) + # Check cross-platforms + _patch_platformdirs(monkeypatch, sys_platform) # When we supply a cache directory, always use that cache_dir = _get_cache_dir(Path("/tmp/foo/cache_dir")) @@ -103,7 +133,7 @@ def test_get_cache_dir_old_pip(monkeypatch): # In this case, we can't query `pip` to figure out where its HTTP cache is # Instead, we use `~/.pip-audit-cache` cache_dir = _get_cache_dir(None) - assert cache_dir == Path.home() / ".pip-audit-cache" + assert cache_dir == expected def test_cache_warns_about_old_pip(monkeypatch, cache_dir): From f67e9cc1422379891bdf5be057bb04a309747db9 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Mon, 26 Aug 2024 20:52:57 +0200 Subject: [PATCH 6/9] enh: delete legacy cache if it exists --- pip_audit/_cache.py | 15 +++++++++++++++ test/test_cache.py | 12 +++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index 6c20d890..cde0d438 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -6,6 +6,7 @@ import logging import os +import shutil import subprocess import sys from pathlib import Path @@ -29,6 +30,8 @@ _PIP_VERSION = Version(str(pip_api.PIP_VERSION)) +_PIP_AUDIT_LEGACY_INTERNAL_CACHE = Path.home() / ".pip-audit-cache" + def _get_pip_cache() -> Path: # Unless the cache directory is specifically set by the `--cache-dir` option, we try to share @@ -45,6 +48,14 @@ def _get_pip_cache() -> Path: return http_cache_dir +def _delete_legacy_cache_dir(current_cache_dir: Path, legacy_cache_dir: Path) -> None: + """ + Deletes the legacy `pip-audit` if it exists. + """ + if current_cache_dir != legacy_cache_dir: + shutil.rmtree(legacy_cache_dir) + + def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Path: """ Returns a directory path suitable for HTTP caching. @@ -62,6 +73,10 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa # Retrieve pip-audit's default internal cache using `platformdirs`. pip_audit_cache_dir = user_cache_path("pip-audit", appauthor=False, ensure_exists=True) + # If the retrieved cache isn't the legacy one, try to delete it. + if _PIP_AUDIT_LEGACY_INTERNAL_CACHE.exists(): + _delete_legacy_cache_dir(pip_audit_cache_dir, _PIP_AUDIT_LEGACY_INTERNAL_CACHE) + # Respect pip's PIP_NO_CACHE_DIR environment setting. if use_pip and not os.getenv("PIP_NO_CACHE_DIR"): pip_cache_dir = _get_pip_cache() if _PIP_VERSION >= _MINIMUM_PIP_VERSION else None diff --git a/test/test_cache.py b/test/test_cache.py index b2b6702b..d2aa173b 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -9,7 +9,7 @@ from pytest import MonkeyPatch import pip_audit._cache as cache -from pip_audit._cache import _get_cache_dir, _get_pip_cache +from pip_audit._cache import _delete_legacy_cache_dir, _get_cache_dir, _get_pip_cache def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: @@ -150,3 +150,13 @@ def test_cache_warns_about_old_pip(monkeypatch, cache_dir): # have an old `pip`, then we should expect a warning to be logged _get_cache_dir(None) assert len(logger.warning.calls) == 1 + + +def test_delete_legacy_cache_dir(tmp_path): + legacy = tmp_path / "pip-audit-cache" + legacy.mkdir() + assert legacy.exists() + + current = _get_cache_dir(None, use_pip=False) + _delete_legacy_cache_dir(current, legacy) + assert not legacy.exists() From 936dc292ce8a97e18e519665ac2fc3471ba18371 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Tue, 27 Aug 2024 21:04:24 +0200 Subject: [PATCH 7/9] enh: inline legacy cache deletion --- pip_audit/_cache.py | 17 ++++++----------- test/test_cache.py | 8 ++++---- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index cde0d438..2dada596 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -48,14 +48,6 @@ def _get_pip_cache() -> Path: return http_cache_dir -def _delete_legacy_cache_dir(current_cache_dir: Path, legacy_cache_dir: Path) -> None: - """ - Deletes the legacy `pip-audit` if it exists. - """ - if current_cache_dir != legacy_cache_dir: - shutil.rmtree(legacy_cache_dir) - - def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Path: """ Returns a directory path suitable for HTTP caching. @@ -73,9 +65,12 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa # Retrieve pip-audit's default internal cache using `platformdirs`. pip_audit_cache_dir = user_cache_path("pip-audit", appauthor=False, ensure_exists=True) - # If the retrieved cache isn't the legacy one, try to delete it. - if _PIP_AUDIT_LEGACY_INTERNAL_CACHE.exists(): - _delete_legacy_cache_dir(pip_audit_cache_dir, _PIP_AUDIT_LEGACY_INTERNAL_CACHE) + # If the retrieved cache isn't the legacy one, try to delete it the old cache. + if ( + _PIP_AUDIT_LEGACY_INTERNAL_CACHE.exists() + and pip_audit_cache_dir != _PIP_AUDIT_LEGACY_INTERNAL_CACHE + ): + shutil.rmtree(_PIP_AUDIT_LEGACY_INTERNAL_CACHE) # Respect pip's PIP_NO_CACHE_DIR environment setting. if use_pip and not os.getenv("PIP_NO_CACHE_DIR"): diff --git a/test/test_cache.py b/test/test_cache.py index d2aa173b..de2c52bf 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -9,7 +9,7 @@ from pytest import MonkeyPatch import pip_audit._cache as cache -from pip_audit._cache import _delete_legacy_cache_dir, _get_cache_dir, _get_pip_cache +from pip_audit._cache import _get_cache_dir, _get_pip_cache def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: @@ -152,11 +152,11 @@ def test_cache_warns_about_old_pip(monkeypatch, cache_dir): assert len(logger.warning.calls) == 1 -def test_delete_legacy_cache_dir(tmp_path): +def test_delete_legacy_cache_dir(monkeypatch, tmp_path): legacy = tmp_path / "pip-audit-cache" legacy.mkdir() assert legacy.exists() + monkeypatch.setattr(cache, "_PIP_AUDIT_LEGACY_INTERNAL_CACHE", legacy) - current = _get_cache_dir(None, use_pip=False) - _delete_legacy_cache_dir(current, legacy) + _get_cache_dir(None, use_pip=False) assert not legacy.exists() From 653fba92621cf468c21d28f903fd4c31c28823a8 Mon Sep 17 00:00:00 2001 From: Alain Anghelidi Date: Tue, 27 Aug 2024 21:09:57 +0200 Subject: [PATCH 8/9] fix: typo in comments --- pip_audit/_cache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pip_audit/_cache.py b/pip_audit/_cache.py index 2dada596..cef93e65 100644 --- a/pip_audit/_cache.py +++ b/pip_audit/_cache.py @@ -65,7 +65,7 @@ def _get_cache_dir(custom_cache_dir: Path | None, *, use_pip: bool = True) -> Pa # Retrieve pip-audit's default internal cache using `platformdirs`. pip_audit_cache_dir = user_cache_path("pip-audit", appauthor=False, ensure_exists=True) - # If the retrieved cache isn't the legacy one, try to delete it the old cache. + # If the retrieved cache isn't the legacy one, try to delete the old cache if it exists. if ( _PIP_AUDIT_LEGACY_INTERNAL_CACHE.exists() and pip_audit_cache_dir != _PIP_AUDIT_LEGACY_INTERNAL_CACHE From 319cb2320387af3ee4cf7513bd74f237ff15ea93 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Tue, 22 Oct 2024 13:21:09 -0400 Subject: [PATCH 9/9] CHANGELOG: record changes Signed-off-by: William Woodruff --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0bfc987..fa5fda99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,12 @@ All versions prior to 0.0.9 are untracked. * `pip-audit` now allows some CLI flags to be configured via environment variables ([#755](https://github.com/pypa/pip-audit/pull/755)) +### Changed + +* The default cache locations on macOS and Linux now respect each platform's + caching directory idioms (e.g. XDG) + ([#814](https://github.com/pypa/pip-audit/pull/814)) + ## [2.7.3] ### Fixed