From 29f4da93d4137862129101e8bd1f649a33b8d290 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Tue, 22 Sep 2015 16:28:19 +0200 Subject: [PATCH] handle access errors when writing cache files silently as pytest warning, fixes #1039 --- _pytest/cacheprovider.py | 20 ++++++++++++++++---- testing/test_cache.py | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/_pytest/cacheprovider.py b/_pytest/cacheprovider.py index 977647fee..6fa92e59d 100755 --- a/_pytest/cacheprovider.py +++ b/_pytest/cacheprovider.py @@ -69,10 +69,22 @@ class Cache(object): like e. g. lists of dictionaries. """ path = self._getvaluepath(key) - path.dirpath().ensure_dir() - with path.open("w") as f: - self.trace("cache-write %s: %r" % (key, value,)) - json.dump(value, f, indent=2, sort_keys=True) + try: + path.dirpath().ensure_dir() + except (py.error.EEXIST, py.error.EACCES): + self.config.warn( + code='I9', message='cache could not create cache path %s' % (path,) + ) + return + try: + f = path.open('w') + except py.error.ENOTDIR: + self.config.warn( + code='I9', message='cache could not write path %s' % (path,)) + else: + with f: + self.trace("cache-write %s: %r" % (key, value,)) + json.dump(value, f, indent=2, sort_keys=True) class LFPlugin: diff --git a/testing/test_cache.py b/testing/test_cache.py index 8eac4e8e0..5205014e3 100755 --- a/testing/test_cache.py +++ b/testing/test_cache.py @@ -25,6 +25,21 @@ class TestNewAPI: val = config.cache.get("key/name", -2) assert val == -2 + def test_cache_writefail_cachfile_silent(self, testdir): + testdir.makeini("[pytest]") + testdir.tmpdir.join('.cache').write('gone wrong') + config = testdir.parseconfigure() + cache = config.cache + cache.set('test/broken', []) + + def test_cache_writefail_permissions(selfself, testdir): + testdir.makeini("[pytest]") + testdir.tmpdir.ensure_dir('.cache').chmod(0) + config = testdir.parseconfigure() + cache = config.cache + cache.set('test/broken', []) + + def test_config_cache(self, testdir): testdir.makeconftest(""" def pytest_configure(config):