From f8f36eeef53e4ed3d2ac8232c3ede5bfabf659cd Mon Sep 17 00:00:00 2001 From: Antti Kaihola <13725+akaihola@users.noreply.github.com> Date: Mon, 30 Jan 2023 22:23:36 +0200 Subject: [PATCH] Use `lru_cache` clear fixture at test module level --- src/darker/tests/conftest.py | 4 ++++ src/darker/tests/test_config.py | 4 +++- src/darker/tests/test_import_sorting.py | 11 +++-------- src/darker/tests/test_main.py | 9 ++++----- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/darker/tests/conftest.py b/src/darker/tests/conftest.py index 2760ec90d..0689b2de7 100644 --- a/src/darker/tests/conftest.py +++ b/src/darker/tests/conftest.py @@ -129,6 +129,10 @@ def git_repo(tmp_path, monkeypatch): def find_project_root_cache_clear(): """Clear LRU caching in :func:`black.find_project_root` before each test + To use this on all test cases in a test module, add this to the top:: + + pytestmark = pytest.mark.usefixtures("find_project_root_cache_clear") + 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. diff --git a/src/darker/tests/test_config.py b/src/darker/tests/test_config.py index 44f208835..3512dff09 100644 --- a/src/darker/tests/test_config.py +++ b/src/darker/tests/test_config.py @@ -23,6 +23,8 @@ ) from darker.tests.helpers import raises_if_exception +pytestmark = pytest.mark.usefixtures("find_project_root_cache_clear") + @pytest.mark.kwparametrize( dict(list_value=[], expect="[\n]"), @@ -376,7 +378,7 @@ def test_output_mode_from_args(diff, stdout, expect): expect={"CONFIG_PATH": "."}, ) def test_load_config( # pylint: disable=too-many-arguments - find_project_root_cache_clear, tmp_path, monkeypatch, srcs, cwd, confpath, expect + tmp_path, monkeypatch, srcs, cwd, confpath, expect ): """``load_config()`` finds and loads configuration based on source file paths""" (tmp_path / ".git").mkdir() diff --git a/src/darker/tests/test_import_sorting.py b/src/darker/tests/test_import_sorting.py index a624102b5..9fb78262c 100644 --- a/src/darker/tests/test_import_sorting.py +++ b/src/darker/tests/test_import_sorting.py @@ -16,6 +16,8 @@ ORIGINAL_SOURCE = ("import sys", "import os", "", "print(42)") ISORTED_SOURCE = ("import os", "import sys", "", "print(42)") +pytestmark = pytest.mark.usefixtures("find_project_root_cache_clear") + @pytest.mark.parametrize("present", [True, False]) def test_import_sorting_importable_with_and_without_isort(present): @@ -132,14 +134,7 @@ def test_apply_isort_exclude(git_repo, encoding, newline, content, exclude, expe ), ), ) -def test_isort_config( - monkeypatch, - tmpdir, - find_project_root_cache_clear, - line_length, - settings_file, - expect, -): +def test_isort_config(monkeypatch, tmpdir, line_length, settings_file, expect): """``apply_isort()`` parses ``pyproject.toml``correctly""" monkeypatch.chdir(tmpdir) (tmpdir / "pyproject.toml").write( diff --git a/src/darker/tests/test_main.py b/src/darker/tests/test_main.py index 20be5a3e2..d1d5ca9f3 100644 --- a/src/darker/tests/test_main.py +++ b/src/darker/tests/test_main.py @@ -25,6 +25,8 @@ from darker.utils import TextDocument, joinlines from darker.verification import NotEquivalentError +pytestmark = pytest.mark.usefixtures("find_project_root_cache_clear") + def _replace_diff_timestamps(text, replacement=""): return re.sub(r"\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d", replacement, text) @@ -45,7 +47,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): """Fixture for running Darker with requested arguments and a patched `isort` Provides an `run_isort.isort_code` mock object which allows checking whether and how @@ -470,7 +472,6 @@ def test_main( git_repo, monkeypatch, capsys, - find_project_root_cache_clear, arguments, newline, pyproject_toml, @@ -573,9 +574,7 @@ def test_main_in_plain_directory(tmp_path, capsys): "encoding, text", [(b"utf-8", b"touch\xc3\xa9"), (b"iso-8859-1", b"touch\xe9")] ) @pytest.mark.parametrize("newline", [b"\n", b"\r\n"]) -def test_main_encoding( - git_repo, find_project_root_cache_clear, encoding, text, newline -): +def test_main_encoding(git_repo, encoding, text, newline): """Encoding and newline of the file is kept unchanged after reformatting""" paths = git_repo.add({"a.py": newline.decode("ascii")}, commit="Initial commit") edited = [b"# coding: ", encoding, newline, b's="', text, b'"', newline]