From 5d5bf6e0878539baeef797b87636235b8c02be3f Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:44:36 -0700 Subject: [PATCH] Fix cache versioning when BLACK_CACHE_DIR is set (#3937) --- CHANGES.md | 2 ++ src/black/cache.py | 3 ++- tests/test_black.py | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ffc63b3..fe4b621 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,8 @@ +- Fix cache versioning logic when `BLACK_CACHE_DIR` is set (#3937) + ### Packaging diff --git a/src/black/cache.py b/src/black/cache.py index 77f66cc..f7dc64c 100644 --- a/src/black/cache.py +++ b/src/black/cache.py @@ -36,8 +36,9 @@ def get_cache_dir() -> Path: repeated calls. """ # NOTE: Function mostly exists as a clean way to test getting the cache directory. - default_cache_dir = user_cache_dir("black", version=__version__) + default_cache_dir = user_cache_dir("black") cache_dir = Path(os.environ.get("BLACK_CACHE_DIR", default_cache_dir)) + cache_dir = cache_dir / __version__ return cache_dir diff --git a/tests/test_black.py b/tests/test_black.py index bb5cc1e..537ca80 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1963,11 +1963,11 @@ class TestCaching: # If BLACK_CACHE_DIR is not set, use user_cache_dir monkeypatch.delenv("BLACK_CACHE_DIR", raising=False) with patch_user_cache_dir: - assert get_cache_dir() == workspace1 + assert get_cache_dir().parent == workspace1 # If it is set, use the path provided in the env var. monkeypatch.setenv("BLACK_CACHE_DIR", str(workspace2)) - assert get_cache_dir() == workspace2 + assert get_cache_dir().parent == workspace2 def test_cache_broken_file(self) -> None: mode = DEFAULT_MODE -- 2.39.5