diff --git a/changelog/4278.trivial.rst b/changelog/4278.trivial.rst new file mode 100644 index 00000000000..126cabea859 --- /dev/null +++ b/changelog/4278.trivial.rst @@ -0,0 +1 @@ +A CACHEDIR.TAG file gets added to the cache directory. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 22ce578fc1d..59265ad85f6 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -33,6 +33,13 @@ See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. """ +CACHEDIR_TAG_CONTENT = b"""\ +Signature: 8a477f597d28d172789f06886806bc55 +# This file is a cache directory tag created by pytest. +# For information about cache directory tags, see: +# http://www.bford.info/cachedir/spec.html +""" + @attr.s class Cache(object): @@ -140,6 +147,10 @@ def _ensure_supporting_files(self): msg = u"# Created by pytest automatically.\n*" gitignore_path.write_text(msg, encoding="UTF-8") + cachedir_tag_path = self._cachedir.joinpath("CACHEDIR.TAG") + if not cachedir_tag_path.is_file(): + cachedir_tag_path.write_bytes(CACHEDIR_TAG_CONTENT) + class LFPlugin(object): """ Plugin which implements the --lf (run last-failing) option """ diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 30fe23aeb5e..29c2d8a1d27 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -925,3 +925,15 @@ def test_does_not_create_boilerplate_in_existing_dirs(testdir): assert os.path.isdir("v") # cache contents assert not os.path.exists(".gitignore") assert not os.path.exists("README.md") + + +def test_cachedir_tag(testdir): + """Ensure we automatically create CACHEDIR.TAG file in the pytest_cache directory (#4278).""" + from _pytest.cacheprovider import Cache + from _pytest.cacheprovider import CACHEDIR_TAG_CONTENT + + config = testdir.parseconfig() + cache = Cache.for_config(config) + cache.set("foo", "bar") + cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG") + assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT