Skip to content

Commit

Permalink
Include reason in cache path warnings to aid debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
scop committed May 15, 2023
1 parent 383774d commit 18c6488
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/_pytest/cacheprovider.py
Expand Up @@ -179,16 +179,26 @@ def set(self, key: str, value: object) -> None:
else:
cache_dir_exists_already = self._cachedir.exists()
path.parent.mkdir(exist_ok=True, parents=True)
except OSError:
self.warn("could not create cache path {path}", path=path, _ispytest=True)
except OSError as exc:
self.warn(
"could not create cache path {path}: {exc}",
path=path,
exc=exc,
_ispytest=True,
)
return
if not cache_dir_exists_already:
self._ensure_supporting_files()
data = json.dumps(value, ensure_ascii=False, indent=2)
try:
f = path.open("w", encoding="UTF-8")
except OSError:
self.warn("cache could not write path {path}", path=path, _ispytest=True)
except OSError as exc:
self.warn(

Check warning on line 196 in src/_pytest/cacheprovider.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/cacheprovider.py#L195-L196

Added lines #L195 - L196 were not covered by tests
"cache could not write path {path}: {exc}",
path=path,
exc=exc,
_ispytest=True,
)
else:
with f:
f.write(data)
Expand Down
2 changes: 1 addition & 1 deletion testing/test_cacheprovider.py
Expand Up @@ -87,7 +87,7 @@ def test_cache_failure_warns(
"*= warnings summary =*",
"*/cacheprovider.py:*",
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path "
f"{unwritable_cache_dir}/v/cache/nodeids",
f"{unwritable_cache_dir}/v/cache/nodeids: *",
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
"*1 failed, 3 warnings in*",
]
Expand Down

0 comments on commit 18c6488

Please sign in to comment.