Skip to content

Commit f573b56

Browse files
Improve cache test and fix it in Docker (#8785)
* cache: Move repetitive code to fixture * cache: Explicitly test for chmod result * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e44300d commit f573b56

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

testing/test_cacheprovider.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import shutil
3-
import sys
43
from pathlib import Path
4+
from typing import Generator
55
from typing import List
66

77
import pytest
@@ -44,52 +44,54 @@ def test_cache_writefail_cachfile_silent(self, pytester: Pytester) -> None:
4444
assert cache is not None
4545
cache.set("test/broken", [])
4646

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]:
5349
cache_dir = pytester.path.joinpath(".pytest_cache")
5450
cache_dir.mkdir()
5551
mode = cache_dir.stat().st_mode
5652
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", [])
6470

65-
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
6671
@pytest.mark.filterwarnings("default")
6772
def test_cache_failure_warns(
68-
self, pytester: Pytester, monkeypatch: MonkeyPatch
73+
self,
74+
pytester: Pytester,
75+
monkeypatch: MonkeyPatch,
76+
unwritable_cache_dir: Path,
6977
) -> None:
7078
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+
)
9395

9496
def test_config_cache(self, pytester: Pytester) -> None:
9597
pytester.makeconftest(

0 commit comments

Comments
 (0)