|
1 | 1 | import os
|
2 | 2 | import shutil
|
3 |
| -import sys |
4 | 3 | from pathlib import Path
|
| 4 | +from typing import Generator |
5 | 5 | from typing import List
|
6 | 6 |
|
7 | 7 | import pytest
|
@@ -44,52 +44,54 @@ def test_cache_writefail_cachfile_silent(self, pytester: Pytester) -> None:
|
44 | 44 | assert cache is not None
|
45 | 45 | cache.set("test/broken", [])
|
46 | 46 |
|
47 |
| - @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") |
48 |
| - @pytest.mark.filterwarnings( |
49 |
| - "ignore:could not create cache path:pytest.PytestWarning" |
50 |
| - ) |
51 |
| - def test_cache_writefail_permissions(self, pytester: Pytester) -> None: |
52 |
| - pytester.makeini("[pytest]") |
| 47 | + @pytest.fixture |
| 48 | + def unwritable_cache_dir(self, pytester: Pytester) -> Generator[Path, None, None]: |
53 | 49 | cache_dir = pytester.path.joinpath(".pytest_cache")
|
54 | 50 | cache_dir.mkdir()
|
55 | 51 | mode = cache_dir.stat().st_mode
|
56 | 52 | cache_dir.chmod(0)
|
57 |
| - try: |
58 |
| - config = pytester.parseconfigure() |
59 |
| - cache = config.cache |
60 |
| - assert cache is not None |
61 |
| - cache.set("test/broken", []) |
62 |
| - finally: |
63 |
| - cache_dir.chmod(mode) |
| 53 | + if os.access(cache_dir, os.W_OK): |
| 54 | + pytest.skip("Failed to make cache dir unwritable") |
| 55 | + |
| 56 | + yield cache_dir |
| 57 | + cache_dir.chmod(mode) |
| 58 | + |
| 59 | + @pytest.mark.filterwarnings( |
| 60 | + "ignore:could not create cache path:pytest.PytestWarning" |
| 61 | + ) |
| 62 | + def test_cache_writefail_permissions( |
| 63 | + self, unwritable_cache_dir: Path, pytester: Pytester |
| 64 | + ) -> None: |
| 65 | + pytester.makeini("[pytest]") |
| 66 | + config = pytester.parseconfigure() |
| 67 | + cache = config.cache |
| 68 | + assert cache is not None |
| 69 | + cache.set("test/broken", []) |
64 | 70 |
|
65 |
| - @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") |
66 | 71 | @pytest.mark.filterwarnings("default")
|
67 | 72 | def test_cache_failure_warns(
|
68 |
| - self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 73 | + self, |
| 74 | + pytester: Pytester, |
| 75 | + monkeypatch: MonkeyPatch, |
| 76 | + unwritable_cache_dir: Path, |
69 | 77 | ) -> None:
|
70 | 78 | monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
71 |
| - cache_dir = pytester.path.joinpath(".pytest_cache") |
72 |
| - cache_dir.mkdir() |
73 |
| - mode = cache_dir.stat().st_mode |
74 |
| - cache_dir.chmod(0) |
75 |
| - try: |
76 |
| - pytester.makepyfile("def test_error(): raise Exception") |
77 |
| - result = pytester.runpytest() |
78 |
| - assert result.ret == 1 |
79 |
| - # warnings from nodeids, lastfailed, and stepwise |
80 |
| - result.stdout.fnmatch_lines( |
81 |
| - [ |
82 |
| - # Validate location/stacklevel of warning from cacheprovider. |
83 |
| - "*= warnings summary =*", |
84 |
| - "*/cacheprovider.py:*", |
85 |
| - " */cacheprovider.py:*: PytestCacheWarning: could not create cache path " |
86 |
| - "{}/v/cache/nodeids".format(cache_dir), |
87 |
| - ' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))', |
88 |
| - "*1 failed, 3 warnings in*", |
89 |
| - ] |
90 |
| - ) |
91 |
| - finally: |
92 |
| - cache_dir.chmod(mode) |
| 79 | + |
| 80 | + pytester.makepyfile("def test_error(): raise Exception") |
| 81 | + result = pytester.runpytest() |
| 82 | + assert result.ret == 1 |
| 83 | + # warnings from nodeids, lastfailed, and stepwise |
| 84 | + result.stdout.fnmatch_lines( |
| 85 | + [ |
| 86 | + # Validate location/stacklevel of warning from cacheprovider. |
| 87 | + "*= warnings summary =*", |
| 88 | + "*/cacheprovider.py:*", |
| 89 | + " */cacheprovider.py:*: PytestCacheWarning: could not create cache path " |
| 90 | + f"{unwritable_cache_dir}/v/cache/nodeids", |
| 91 | + ' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))', |
| 92 | + "*1 failed, 3 warnings in*", |
| 93 | + ] |
| 94 | + ) |
93 | 95 |
|
94 | 96 | def test_config_cache(self, pytester: Pytester) -> None:
|
95 | 97 | pytester.makeconftest(
|
|
0 commit comments