|
1 | | -# mypy: allow-untyped-defs |
| 1 | +from enum import auto |
| 2 | +from enum import Enum |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
4 | 5 | import shutil |
| 6 | +from typing import Any |
5 | 7 | from typing import Generator |
6 | 8 | from typing import List |
| 9 | +from typing import Sequence |
| 10 | +from typing import Tuple |
7 | 11 |
|
| 12 | +from _pytest.compat import assert_never |
8 | 13 | from _pytest.config import ExitCode |
9 | 14 | from _pytest.monkeypatch import MonkeyPatch |
10 | 15 | from _pytest.pytester import Pytester |
@@ -175,7 +180,9 @@ def test_custom_cache_dir_with_env_var( |
175 | 180 |
|
176 | 181 |
|
177 | 182 | @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: |
179 | 186 | pytester.makepyfile("""def test_foo(): pass""") |
180 | 187 | if env: |
181 | 188 | monkeypatch.setenv(*env) |
@@ -507,7 +514,7 @@ def test_hello(): |
507 | 514 | """ |
508 | 515 | ) |
509 | 516 |
|
510 | | - def rlf(fail_import, fail_run): |
| 517 | + def rlf(fail_import: int, fail_run: int) -> Any: |
511 | 518 | monkeypatch.setenv("FAILIMPORT", str(fail_import)) |
512 | 519 | monkeypatch.setenv("FAILTEST", str(fail_run)) |
513 | 520 |
|
@@ -555,7 +562,9 @@ def test_pass(): |
555 | 562 | """ |
556 | 563 | ) |
557 | 564 |
|
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]: |
559 | 568 | monkeypatch.setenv("FAILIMPORT", str(fail_import)) |
560 | 569 | monkeypatch.setenv("FAILTEST", str(fail_run)) |
561 | 570 |
|
@@ -1254,20 +1263,41 @@ def test_readme_failed(self, pytester: Pytester) -> None: |
1254 | 1263 | assert self.check_readme(pytester) is True |
1255 | 1264 |
|
1256 | 1265 |
|
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: |
1258 | 1278 | """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" |
1259 | 1279 | from _pytest.cacheprovider import Cache |
1260 | 1280 |
|
1261 | 1281 | config = pytester.parseconfig() |
1262 | 1282 | 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) |
1264 | 1289 | msg = "# Created by pytest automatically.\n*\n" |
1265 | 1290 | gitignore_path = cache._cachedir.joinpath(".gitignore") |
1266 | 1291 | assert gitignore_path.read_text(encoding="UTF-8") == msg |
1267 | 1292 |
|
1268 | 1293 | # Does not overwrite existing/custom one. |
1269 | 1294 | 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) |
1271 | 1301 | assert gitignore_path.read_text(encoding="UTF-8") == "custom" |
1272 | 1302 |
|
1273 | 1303 |
|
|
0 commit comments