Skip to content

Commit 1f001cd

Browse files
authored
Merge pull request #12199 from tamird/mkdir-test-init
Add test for Cache.mkdir
2 parents 9989063 + e06c337 commit 1f001cd

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

testing/test_cacheprovider.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
# mypy: allow-untyped-defs
1+
from enum import auto
2+
from enum import Enum
23
import os
34
from pathlib import Path
45
import shutil
6+
from typing import Any
57
from typing import Generator
68
from typing import List
9+
from typing import Sequence
10+
from typing import Tuple
711

12+
from _pytest.compat import assert_never
813
from _pytest.config import ExitCode
914
from _pytest.monkeypatch import MonkeyPatch
1015
from _pytest.pytester import Pytester
@@ -175,7 +180,9 @@ def test_custom_cache_dir_with_env_var(
175180

176181

177182
@pytest.mark.parametrize("env", ((), ("TOX_ENV_DIR", "/tox_env_dir")))
178-
def test_cache_reportheader(env, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
183+
def test_cache_reportheader(
184+
env: Sequence[str], pytester: Pytester, monkeypatch: MonkeyPatch
185+
) -> None:
179186
pytester.makepyfile("""def test_foo(): pass""")
180187
if env:
181188
monkeypatch.setenv(*env)
@@ -507,7 +514,7 @@ def test_hello():
507514
"""
508515
)
509516

510-
def rlf(fail_import, fail_run):
517+
def rlf(fail_import: int, fail_run: int) -> Any:
511518
monkeypatch.setenv("FAILIMPORT", str(fail_import))
512519
monkeypatch.setenv("FAILTEST", str(fail_run))
513520

@@ -555,7 +562,9 @@ def test_pass():
555562
"""
556563
)
557564

558-
def rlf(fail_import, fail_run, args=()):
565+
def rlf(
566+
fail_import: int, fail_run: int, args: Sequence[str] = ()
567+
) -> Tuple[Any, Any]:
559568
monkeypatch.setenv("FAILIMPORT", str(fail_import))
560569
monkeypatch.setenv("FAILTEST", str(fail_run))
561570

@@ -1254,20 +1263,41 @@ def test_readme_failed(self, pytester: Pytester) -> None:
12541263
assert self.check_readme(pytester) is True
12551264

12561265

1257-
def test_gitignore(pytester: Pytester) -> None:
1266+
class Action(Enum):
1267+
"""Action to perform on the cache directory."""
1268+
1269+
MKDIR = auto()
1270+
SET = auto()
1271+
1272+
1273+
@pytest.mark.parametrize("action", list(Action))
1274+
def test_gitignore(
1275+
pytester: Pytester,
1276+
action: Action,
1277+
) -> None:
12581278
"""Ensure we automatically create .gitignore file in the pytest_cache directory (#3286)."""
12591279
from _pytest.cacheprovider import Cache
12601280

12611281
config = pytester.parseconfig()
12621282
cache = Cache.for_config(config, _ispytest=True)
1263-
cache.set("foo", "bar")
1283+
if action == Action.MKDIR:
1284+
cache.mkdir("foo")
1285+
elif action == Action.SET:
1286+
cache.set("foo", "bar")
1287+
else:
1288+
assert_never(action)
12641289
msg = "# Created by pytest automatically.\n*\n"
12651290
gitignore_path = cache._cachedir.joinpath(".gitignore")
12661291
assert gitignore_path.read_text(encoding="UTF-8") == msg
12671292

12681293
# Does not overwrite existing/custom one.
12691294
gitignore_path.write_text("custom", encoding="utf-8")
1270-
cache.set("something", "else")
1295+
if action == Action.MKDIR:
1296+
cache.mkdir("something")
1297+
elif action == Action.SET:
1298+
cache.set("something", "else")
1299+
else:
1300+
assert_never(action)
12711301
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
12721302

12731303

0 commit comments

Comments
 (0)