Skip to content

Commit

Permalink
Ensure cache supporting files still exist after --cache-clear
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus authored and vinaycalastry committed Dec 11, 2019
1 parent 141624e commit 0f500de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/6290.bugfix.rst
@@ -0,0 +1 @@
The supporting files in the ``.pytest_cache`` directory are kept with ``--cache-clear``, which only clears cached values now.
27 changes: 20 additions & 7 deletions src/_pytest/cacheprovider.py
Expand Up @@ -44,14 +44,27 @@ class Cache:
_cachedir = attr.ib(repr=False)
_config = attr.ib(repr=False)

# sub-directory under cache-dir for directories created by "makedir"
_CACHE_PREFIX_DIRS = "d"

# sub-directory under cache-dir for values created by "set"
_CACHE_PREFIX_VALUES = "v"

@classmethod
def for_config(cls, config):
cachedir = cls.cache_dir_from_config(config)
if config.getoption("cacheclear") and cachedir.exists():
rm_rf(cachedir)
cachedir.mkdir()
if config.getoption("cacheclear") and cachedir.is_dir():
cls.clear_cache(cachedir)
return cls(cachedir, config)

@classmethod
def clear_cache(cls, cachedir: Path):
"""Clears the sub-directories used to hold cached directories and values."""
for prefix in (cls._CACHE_PREFIX_DIRS, cls._CACHE_PREFIX_VALUES):
d = cachedir / prefix
if d.is_dir():
rm_rf(d)

@staticmethod
def cache_dir_from_config(config):
return resolve_from_str(config.getini("cache_dir"), config.rootdir)
Expand Down Expand Up @@ -79,12 +92,12 @@ def makedir(self, name):
name = Path(name)
if len(name.parts) > 1:
raise ValueError("name is not allowed to contain path separators")
res = self._cachedir.joinpath("d", name)
res = self._cachedir.joinpath(self._CACHE_PREFIX_DIRS, name)
res.mkdir(exist_ok=True, parents=True)
return py.path.local(res)

def _getvaluepath(self, key):
return self._cachedir.joinpath("v", Path(key))
return self._cachedir.joinpath(self._CACHE_PREFIX_VALUES, Path(key))

def get(self, key, default):
""" return cached value for the given key. If no value
Expand Down Expand Up @@ -417,7 +430,7 @@ def cacheshow(config, session):

dummy = object()
basedir = config.cache._cachedir
vdir = basedir / "v"
vdir = basedir / Cache._CACHE_PREFIX_VALUES
tw.sep("-", "cache values for %r" % glob)
for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
key = valpath.relative_to(vdir)
Expand All @@ -429,7 +442,7 @@ def cacheshow(config, session):
for line in pformat(val).splitlines():
tw.line(" " + line)

ddir = basedir / "d"
ddir = basedir / Cache._CACHE_PREFIX_DIRS
if ddir.is_dir():
contents = sorted(ddir.rglob(glob))
tw.sep("-", "cache directories for %r" % glob)
Expand Down
1 change: 1 addition & 0 deletions testing/test_cacheprovider.py
Expand Up @@ -270,6 +270,7 @@ def test_3(): assert 0
)
result = testdir.runpytest(str(p), "--lf", "--cache-clear")
result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
assert testdir.tmpdir.join(".pytest_cache", "README.md").isfile()

# Run this again to make sure clear-cache is robust
if os.path.isdir(".pytest_cache"):
Expand Down

0 comments on commit 0f500de

Please sign in to comment.