Merge pull request #12199 from tamird/mkdir-test-init

Add test for Cache.mkdir
This commit is contained in:
Ran Benita 2024-04-09 22:26:36 +03:00 committed by GitHub
commit 1f001cd105
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 37 additions and 7 deletions

View File

@ -1,10 +1,15 @@
# mypy: allow-untyped-defs from enum import auto
from enum import Enum
import os import os
from pathlib import Path from pathlib import Path
import shutil import shutil
from typing import Any
from typing import Generator from typing import Generator
from typing import List 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.config import ExitCode
from _pytest.monkeypatch import MonkeyPatch from _pytest.monkeypatch import MonkeyPatch
from _pytest.pytester import Pytester from _pytest.pytester import Pytester
@ -175,7 +180,9 @@ class TestNewAPI:
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir"))) @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""") pytester.makepyfile("""def test_foo(): pass""")
if env: if env:
monkeypatch.setenv(*env) monkeypatch.setenv(*env)
@ -507,7 +514,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("FAILIMPORT", str(fail_import))
monkeypatch.setenv("FAILTEST", str(fail_run)) monkeypatch.setenv("FAILTEST", str(fail_run))
@ -555,7 +562,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("FAILIMPORT", str(fail_import))
monkeypatch.setenv("FAILTEST", str(fail_run)) monkeypatch.setenv("FAILTEST", str(fail_run))
@ -1254,20 +1263,41 @@ class TestReadme:
assert self.check_readme(pytester) is True 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).""" """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286)."""
from _pytest.cacheprovider import Cache from _pytest.cacheprovider import Cache
config = pytester.parseconfig() config = pytester.parseconfig()
cache = Cache.for_config(config, _ispytest=True) 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" msg = "# Created by pytest automatically.\n*\n"
gitignore_path = cache._cachedir.joinpath(".gitignore") gitignore_path = cache._cachedir.joinpath(".gitignore")
assert gitignore_path.read_text(encoding="UTF-8") == msg assert gitignore_path.read_text(encoding="UTF-8") == msg
# Does not overwrite existing/custom one. # Does not overwrite existing/custom one.
gitignore_path.write_text("custom", encoding="utf-8") 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" assert gitignore_path.read_text(encoding="UTF-8") == "custom"