From a02cc0ad1b575cbc5b87f8db4ac49f3783c921d3 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 8 Apr 2024 14:59:51 +0100 Subject: [PATCH 1/2] Add types --- testing/test_cacheprovider.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index c020b77f9..9c70271b4 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -1,9 +1,11 @@ -# mypy: allow-untyped-defs import os from pathlib import Path import shutil +from typing import Any from typing import Generator from typing import List +from typing import Sequence +from typing import Tuple from _pytest.config import ExitCode from _pytest.monkeypatch import MonkeyPatch @@ -175,7 +177,9 @@ class TestNewAPI: @pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir"))) -def test_cache_reportheader(env, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: +def test_cache_reportheader( + env: Sequence[str], pytester: Pytester, monkeypatch: MonkeyPatch +) -> None: pytester.makepyfile("""def test_foo(): pass""") if env: monkeypatch.setenv(*env) @@ -507,7 +511,7 @@ class TestLastFailed: """ ) - def rlf(fail_import, fail_run): + def rlf(fail_import: int, fail_run: int) -> Any: monkeypatch.setenv("FAILIMPORT", str(fail_import)) monkeypatch.setenv("FAILTEST", str(fail_run)) @@ -555,7 +559,9 @@ class TestLastFailed: """ ) - def rlf(fail_import, fail_run, args=()): + def rlf( + fail_import: int, fail_run: int, args: Sequence[str] = () + ) -> Tuple[Any, Any]: monkeypatch.setenv("FAILIMPORT", str(fail_import)) monkeypatch.setenv("FAILTEST", str(fail_run)) From e06c337bd851c24b2d3db188b4a137ab63015b03 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 8 Apr 2024 15:02:31 +0100 Subject: [PATCH 2/2] Add test for Cache.mkdir --- testing/test_cacheprovider.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 9c70271b4..304e5414a 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -1,3 +1,5 @@ +from enum import auto +from enum import Enum import os from pathlib import Path import shutil @@ -7,6 +9,7 @@ from typing import List from typing import Sequence from typing import Tuple +from _pytest.compat import assert_never from _pytest.config import ExitCode from _pytest.monkeypatch import MonkeyPatch from _pytest.pytester import Pytester @@ -1260,20 +1263,41 @@ class TestReadme: assert self.check_readme(pytester) is True -def test_gitignore(pytester: Pytester) -> None: +class Action(Enum): + """Action to perform on the cache directory.""" + + MKDIR = auto() + SET = auto() + + +@pytest.mark.parametrize("action", list(Action)) +def test_gitignore( + pytester: Pytester, + action: Action, +) -> None: """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" from _pytest.cacheprovider import Cache config = pytester.parseconfig() cache = Cache.for_config(config, _ispytest=True) - cache.set("foo", "bar") + if action == Action.MKDIR: + cache.mkdir("foo") + elif action == Action.SET: + cache.set("foo", "bar") + else: + assert_never(action) msg = "# Created by pytest automatically.\n*\n" gitignore_path = cache._cachedir.joinpath(".gitignore") assert gitignore_path.read_text(encoding="UTF-8") == msg # Does not overwrite existing/custom one. gitignore_path.write_text("custom", encoding="utf-8") - cache.set("something", "else") + if action == Action.MKDIR: + cache.mkdir("something") + elif action == Action.SET: + cache.set("something", "else") + else: + assert_never(action) assert gitignore_path.read_text(encoding="UTF-8") == "custom"