Ensure cache supporting files still exist after --cache-clear (#6296)
Ensure cache supporting files still exist after --cache-clear
This commit is contained in:
		
						commit
						23f6adc760
					
				| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					The supporting files in the ``.pytest_cache`` directory are kept with ``--cache-clear``, which only clears cached values now.
 | 
				
			||||||
| 
						 | 
					@ -44,14 +44,27 @@ class Cache:
 | 
				
			||||||
    _cachedir = attr.ib(repr=False)
 | 
					    _cachedir = attr.ib(repr=False)
 | 
				
			||||||
    _config = 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
 | 
					    @classmethod
 | 
				
			||||||
    def for_config(cls, config):
 | 
					    def for_config(cls, config):
 | 
				
			||||||
        cachedir = cls.cache_dir_from_config(config)
 | 
					        cachedir = cls.cache_dir_from_config(config)
 | 
				
			||||||
        if config.getoption("cacheclear") and cachedir.exists():
 | 
					        if config.getoption("cacheclear") and cachedir.is_dir():
 | 
				
			||||||
            rm_rf(cachedir)
 | 
					            cls.clear_cache(cachedir)
 | 
				
			||||||
            cachedir.mkdir()
 | 
					 | 
				
			||||||
        return cls(cachedir, config)
 | 
					        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
 | 
					    @staticmethod
 | 
				
			||||||
    def cache_dir_from_config(config):
 | 
					    def cache_dir_from_config(config):
 | 
				
			||||||
        return resolve_from_str(config.getini("cache_dir"), config.rootdir)
 | 
					        return resolve_from_str(config.getini("cache_dir"), config.rootdir)
 | 
				
			||||||
| 
						 | 
					@ -79,12 +92,12 @@ class Cache:
 | 
				
			||||||
        name = Path(name)
 | 
					        name = Path(name)
 | 
				
			||||||
        if len(name.parts) > 1:
 | 
					        if len(name.parts) > 1:
 | 
				
			||||||
            raise ValueError("name is not allowed to contain path separators")
 | 
					            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)
 | 
					        res.mkdir(exist_ok=True, parents=True)
 | 
				
			||||||
        return py.path.local(res)
 | 
					        return py.path.local(res)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _getvaluepath(self, key):
 | 
					    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):
 | 
					    def get(self, key, default):
 | 
				
			||||||
        """ return cached value for the given key.  If no value
 | 
					        """ return cached value for the given key.  If no value
 | 
				
			||||||
| 
						 | 
					@ -417,7 +430,7 @@ def cacheshow(config, session):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    dummy = object()
 | 
					    dummy = object()
 | 
				
			||||||
    basedir = config.cache._cachedir
 | 
					    basedir = config.cache._cachedir
 | 
				
			||||||
    vdir = basedir / "v"
 | 
					    vdir = basedir / Cache._CACHE_PREFIX_VALUES
 | 
				
			||||||
    tw.sep("-", "cache values for %r" % glob)
 | 
					    tw.sep("-", "cache values for %r" % glob)
 | 
				
			||||||
    for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
 | 
					    for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
 | 
				
			||||||
        key = valpath.relative_to(vdir)
 | 
					        key = valpath.relative_to(vdir)
 | 
				
			||||||
| 
						 | 
					@ -429,7 +442,7 @@ def cacheshow(config, session):
 | 
				
			||||||
            for line in pformat(val).splitlines():
 | 
					            for line in pformat(val).splitlines():
 | 
				
			||||||
                tw.line("  " + line)
 | 
					                tw.line("  " + line)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ddir = basedir / "d"
 | 
					    ddir = basedir / Cache._CACHE_PREFIX_DIRS
 | 
				
			||||||
    if ddir.is_dir():
 | 
					    if ddir.is_dir():
 | 
				
			||||||
        contents = sorted(ddir.rglob(glob))
 | 
					        contents = sorted(ddir.rglob(glob))
 | 
				
			||||||
        tw.sep("-", "cache directories for %r" % glob)
 | 
					        tw.sep("-", "cache directories for %r" % glob)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -270,6 +270,7 @@ class TestLastFailed:
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        result = testdir.runpytest(str(p), "--lf", "--cache-clear")
 | 
					        result = testdir.runpytest(str(p), "--lf", "--cache-clear")
 | 
				
			||||||
        result.stdout.fnmatch_lines(["*1 failed*2 passed*"])
 | 
					        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
 | 
					        # Run this again to make sure clear-cache is robust
 | 
				
			||||||
        if os.path.isdir(".pytest_cache"):
 | 
					        if os.path.isdir(".pytest_cache"):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue