Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cache supporting files still exist after --cache-clear #6296

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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