Skip to content

Commit

Permalink
Fix cache clearing in tests for find_project_root
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Feb 6, 2022
1 parent ba87061 commit 54c00d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/darker/black_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ def find_project_root(srcs: Sequence[str]) -> Path:
return cast(Tuple[Path], root)[0]
# Black < 22
return cast(Path, root)


find_project_root.cache_clear = black_find_project_root.cache_clear
12 changes: 9 additions & 3 deletions src/darker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from typing import Dict, Optional

import pytest
from black import find_project_root as black_find_project_root

from darker.black_compat import find_project_root
from darker.git import _git_check_output_lines


Expand Down Expand Up @@ -97,5 +97,11 @@ def git_repo(tmp_path, monkeypatch):

@pytest.fixture
def find_project_root_cache_clear():
"""Clear LRU caching in :func:`black.find_project_root` before each test"""
find_project_root.cache_clear()
"""Clear LRU caching in :func:`black.find_project_root` before each test
NOTE: We use `darker.black_compat.find_project_root` to wrap Black's original
function since its signature has changed along the way. However, clearing the cache
needs to be done on the original of course.
"""
black_find_project_root.cache_clear()
11 changes: 8 additions & 3 deletions src/darker/tests/test_import_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest

import darker.import_sorting
from darker.black_compat import find_project_root
from darker.tests.helpers import isort_present
from darker.utils import TextDocument

Expand Down Expand Up @@ -76,9 +75,15 @@ def test_apply_isort(encoding, newline):
),
),
)
def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect):
def test_isort_config(
monkeypatch,
tmpdir,
find_project_root_cache_clear,
line_length,
settings_file,
expect,
):
"""``apply_isort()`` parses ``pyproject.toml``correctly"""
find_project_root.cache_clear()
monkeypatch.chdir(tmpdir)
(tmpdir / "pyproject.toml").write(
dedent(
Expand Down
5 changes: 1 addition & 4 deletions src/darker/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import darker.__main__
import darker.import_sorting
import darker.linting
from darker.black_compat import find_project_root
from darker.exceptions import MissingPackageError
from darker.git import WORKTREE, RevisionRange
from darker.tests.helpers import isort_present
Expand All @@ -38,9 +37,7 @@ def test_isort_option_without_isort(git_repo, caplog):


@pytest.fixture
def run_isort(git_repo, monkeypatch, caplog, request):
find_project_root.cache_clear()

def run_isort(git_repo, monkeypatch, caplog, request, find_project_root_cache_clear):
monkeypatch.chdir(git_repo.root)
paths = git_repo.add({"test1.py": "original"}, commit="Initial commit")
paths["test1.py"].write_bytes(b"changed")
Expand Down

0 comments on commit 54c00d3

Please sign in to comment.