Skip to content

Commit 9d1ae0a

Browse files
committed
Merge pull request #1048 from RonnyPfannschmidt/pytest-1029
fixes #1029 by handling access errors for cache dirs as pytest warning
2 parents ca460e1 + 7f776fe commit 9d1ae0a

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- fix issue 877: properly handle assertion explanations with non-ascii repr
1616
Thanks Mathieu Agopian for the report and Ronny Pfannschmidt for the PR.
1717

18+
- fix issue 1029: transform errors when writing cache values into pytest-warnings
1819

1920
2.8.0
2021
-----------------------------

_pytest/cacheprovider.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,22 @@ def set(self, key, value):
6969
like e. g. lists of dictionaries.
7070
"""
7171
path = self._getvaluepath(key)
72-
path.dirpath().ensure_dir()
73-
with path.open("w") as f:
74-
self.trace("cache-write %s: %r" % (key, value,))
75-
json.dump(value, f, indent=2, sort_keys=True)
72+
try:
73+
path.dirpath().ensure_dir()
74+
except (py.error.EEXIST, py.error.EACCES):
75+
self.config.warn(
76+
code='I9', message='could not create cache path %s' % (path,)
77+
)
78+
return
79+
try:
80+
f = path.open('w')
81+
except py.error.ENOTDIR:
82+
self.config.warn(
83+
code='I9', message='cache could not write path %s' % (path,))
84+
else:
85+
with f:
86+
self.trace("cache-write %s: %r" % (key, value,))
87+
json.dump(value, f, indent=2, sort_keys=True)
7688

7789

7890
class LFPlugin:

testing/test_cache.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import pytest
23
import os
34
import shutil
@@ -25,6 +26,36 @@ def test_config_cache_dataerror(self, testdir):
2526
val = config.cache.get("key/name", -2)
2627
assert val == -2
2728

29+
def test_cache_writefail_cachfile_silent(self, testdir):
30+
testdir.makeini("[pytest]")
31+
testdir.tmpdir.join('.cache').write('gone wrong')
32+
config = testdir.parseconfigure()
33+
cache = config.cache
34+
cache.set('test/broken', [])
35+
36+
@pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows')
37+
def test_cache_writefail_permissions(self, testdir):
38+
testdir.makeini("[pytest]")
39+
testdir.tmpdir.ensure_dir('.cache').chmod(0)
40+
config = testdir.parseconfigure()
41+
cache = config.cache
42+
cache.set('test/broken', [])
43+
44+
@pytest.mark.skipif(sys.platform.startswith('win'), reason='no chmod on windows')
45+
def test_cache_failure_warns(self, testdir):
46+
testdir.tmpdir.ensure_dir('.cache').chmod(0)
47+
testdir.makepyfile("""
48+
def test_pass():
49+
pass
50+
51+
""")
52+
result = testdir.runpytest('-rw')
53+
assert result.ret == 0
54+
result.stdout.fnmatch_lines([
55+
"*could not create cache path*",
56+
"*1 pytest-warnings*",
57+
])
58+
2859
def test_config_cache(self, testdir):
2960
testdir.makeconftest("""
3061
def pytest_configure(config):

0 commit comments

Comments
 (0)