|
| 1 | +from enum import auto |
| 2 | +from enum import Enum |
1 | 3 | import os |
2 | 4 | from pathlib import Path |
3 | 5 | import shutil |
|
7 | 9 | from typing import Sequence |
8 | 10 | from typing import Tuple |
9 | 11 |
|
| 12 | +from _pytest.compat import assert_never |
10 | 13 | from _pytest.config import ExitCode |
11 | 14 | from _pytest.monkeypatch import MonkeyPatch |
12 | 15 | from _pytest.pytester import Pytester |
@@ -1260,20 +1263,41 @@ def test_readme_failed(self, pytester: Pytester) -> None: |
1260 | 1263 | assert self.check_readme(pytester) is True |
1261 | 1264 |
|
1262 | 1265 |
|
1263 | | -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: |
1264 | 1278 | """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" |
1265 | 1279 | from _pytest.cacheprovider import Cache |
1266 | 1280 |
|
1267 | 1281 | config = pytester.parseconfig() |
1268 | 1282 | cache = Cache.for_config(config, _ispytest=True) |
1269 | | - 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) |
1270 | 1289 | msg = "# Created by pytest automatically.\n*\n" |
1271 | 1290 | gitignore_path = cache._cachedir.joinpath(".gitignore") |
1272 | 1291 | assert gitignore_path.read_text(encoding="UTF-8") == msg |
1273 | 1292 |
|
1274 | 1293 | # Does not overwrite existing/custom one. |
1275 | 1294 | gitignore_path.write_text("custom", encoding="utf-8") |
1276 | | - 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) |
1277 | 1301 | assert gitignore_path.read_text(encoding="UTF-8") == "custom" |
1278 | 1302 |
|
1279 | 1303 |
|
|
0 commit comments