From c8641f879f3dac0ce92338a6b87b687b7efe25d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 18 May 2023 16:11:47 +0300 Subject: [PATCH] Include reason in cache path warnings to aid debugging (#11005) Co-authored-by: Bruno Oliveira --- changelog/11005.improvement.rst | 1 + src/_pytest/cacheprovider.py | 14 ++++++++++---- testing/test_cacheprovider.py | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 changelog/11005.improvement.rst diff --git a/changelog/11005.improvement.rst b/changelog/11005.improvement.rst new file mode 100644 index 000000000..295252514 --- /dev/null +++ b/changelog/11005.improvement.rst @@ -0,0 +1 @@ +Added underlying exception to cache provider path creation and write warning messages. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 719b32f7e..89a4a55f8 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -179,16 +179,22 @@ class Cache: 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( + f"could not create cache path {path}: {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( + f"cache could not write path {path}: {exc}", + _ispytest=True, + ) else: with f: f.write(data) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 2f8517f99..ee2fe1845 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -87,7 +87,7 @@ class TestNewAPI: "*= 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*", ]