Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache versioning when BLACK_CACHE_DIR is set #3937

Merged
merged 5 commits into from Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -24,6 +24,8 @@

<!-- Changes to how Black can be configured -->

- Fix cache versioning logic when `BLACK_CACHE_DIR` is set (#3937)

### Packaging

<!-- Changes to how Black is packaged, such as dependency requirements -->
Expand Down
3 changes: 2 additions & 1 deletion src/black/cache.py
Expand Up @@ -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


Expand Down
4 changes: 2 additions & 2 deletions tests/test_black.py
Expand Up @@ -1961,11 +1961,11 @@ def test_get_cache_dir(
# 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
Expand Down