From 08b98a4f3cf3c9b676f41bb738671427fed6b9fd Mon Sep 17 00:00:00 2001 From: gesh Date: Mon, 10 Feb 2025 16:09:46 +0200 Subject: [PATCH 1/3] fix(test_cache): Correct XDG testing logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tests on Linux were relying on XDG_CACHE_HOME being set to its fallback value of ∼/.cache, which made them fail on systems with custom values for XDG_CACHE_HOME. To account for this, we take the cue from the Windows tests and set XDG_CACHE_HOME to a known, custom, value. This incidentally also tests that our logic can account for custom values of XDG_CACHE_HOME, so this doesn't need its own test. The value of "/tmp/home/.cache" was chosen as a cross between the Windows value of "/tmp/AppData/Local" and the fallback "∼/.cache" Fixes: #814 --- test/test_cache.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index de2c52bf..3e2eef2c 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -20,8 +20,11 @@ def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: # as cache definition is stored in the top level `__init__.py` file of the # `platformdirs` package importlib.reload(platformdirs) + # Setting directory-controlling environment variables to known state if sys_platform == "win32": monkeypatch.setenv("LOCALAPPDATA", "/tmp/AppData/Local") + elif sys_platform == "linux": + monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/home/.cache") def test_get_cache_dir(monkeypatch): @@ -48,7 +51,7 @@ def test_get_pip_cache(): [ pytest.param( "linux", - Path.home() / ".cache" / "pip-audit", + Path("/tmp") / "home" / ".cache" / "pip-audit", id="on Linux", ), pytest.param( @@ -76,7 +79,7 @@ def test_get_cache_dir_do_not_use_pip(monkeypatch, sys_platform, expected): [ pytest.param( "linux", - Path.home() / ".cache" / "pip-audit", + Path("/tmp") / "home" / ".cache" / "pip-audit", id="on Linux", ), pytest.param( @@ -105,7 +108,7 @@ def test_get_cache_dir_pip_disabled_in_environment(monkeypatch, sys_platform, ex [ pytest.param( "linux", - Path.home() / ".cache" / "pip-audit", + Path("/tmp") / "home" / ".cache" / "pip-audit", id="on Linux", ), pytest.param( From 4d1de2d66987786c407bc667e78b8496615546d9 Mon Sep 17 00:00:00 2001 From: gesh Date: Mon, 10 Feb 2025 16:19:05 +0200 Subject: [PATCH 2/3] fix(test_cache): Make tests more consistent Several changes to the tests in test_cache.py to make them more consistent with each other - test_get_cache_dir: Follow all other tests in not casting to posix paths in checking paths are as expected, instead checking an equal Path object is roundtripped. Also, run the test simulating all platform's path logic, like the other tests. - test_get_cache_dir_old_pip: Remove logic checking whether _get_cache_dir accepts explicitly-set paths. It is duplicated from test_get_cache_dir, doesn't appear to be exercising any new codepath, and it is unclear why this case needs extra exercise - test_get_pip_cache, test_get_cache_dir_old_pip: follow the lead of test_get_cache_dir_pip_disabled_in_environment and inline the call to _get_cache_dir --- test/test_cache.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/test/test_cache.py b/test/test_cache.py index 3e2eef2c..7a46790d 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -27,17 +27,28 @@ def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None: monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/home/.cache") -def test_get_cache_dir(monkeypatch): +@pytest.mark.parametrize( + "sys_platform", + [ + pytest.param("linux", id="on Linux"), + pytest.param("win32", id="on Windows"), + pytest.param("darwin", id="on MacOS"), + ], +) +def test_get_cache_dir(monkeypatch, sys_platform): + # 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")) - assert cache_dir.as_posix() == "/tmp/foo/cache_dir" + cache_dir = Path("/tmp/foo/cache_dir") + assert _get_cache_dir(cache_dir) == cache_dir - get_pip_cache = pretend.call_recorder(lambda: Path("/fake/pip/cache/dir")) + cache_dir = Path("/fake/pip/cache/dir") + get_pip_cache = pretend.call_recorder(lambda: cache_dir) monkeypatch.setattr(cache, "_get_pip_cache", get_pip_cache) # When `pip cache dir` works, we use it. In this case, it's mocked. - cache_dir = _get_cache_dir(None, use_pip=True) - assert cache_dir.as_posix() == "/fake/pip/cache/dir" + assert _get_cache_dir(None, use_pip=True) == cache_dir def test_get_pip_cache(): @@ -69,9 +80,9 @@ def test_get_pip_cache(): 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 == expected + assert _get_cache_dir(None, use_pip=False) == expected @pytest.mark.parametrize( @@ -129,14 +140,9 @@ def test_get_cache_dir_old_pip(monkeypatch, sys_platform, expected): # 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")) - assert cache_dir.as_posix() == "/tmp/foo/cache_dir" - # 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 == expected + assert _get_cache_dir(None) == expected def test_cache_warns_about_old_pip(monkeypatch, cache_dir): From 2f03ce18d7cd57603c7b89f275c30d0562711df3 Mon Sep 17 00:00:00 2001 From: William Woodruff Date: Thu, 27 Feb 2025 10:47:02 -0500 Subject: [PATCH 3/3] ci: add Windows test job Signed-off-by: William Woodruff --- .github/workflows/ci.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 053f5087..bcaf09a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -35,11 +35,29 @@ jobs: - name: test run: make test PIP_AUDIT_EXTRA=test + test-windows: + runs-on: windows-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + persist-credentials: false + + - uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5 + with: + # Always test with latest Python on Windows. + python-version: "3.x" + cache: "pip" + cache-dependency-path: pyproject.toml + + - name: test + run: make test PIP_AUDIT_EXTRA=test + all-tests-pass: if: always() needs: - test + - test-windows runs-on: ubuntu-latest