From d237197de34bed39854251989126777fc9941c18 Mon Sep 17 00:00:00 2001 From: feuillemorte Date: Tue, 4 Dec 2018 13:49:08 +0100 Subject: [PATCH] #4278 Added a CACHEDIR.TAG file to the cache directory --- changelog/4278.trivial.rst | 1 + src/_pytest/cacheprovider.py | 11 +++++++++++ testing/test_cacheprovider.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 changelog/4278.trivial.rst diff --git a/changelog/4278.trivial.rst b/changelog/4278.trivial.rst new file mode 100644 index 000000000..09c016639 --- /dev/null +++ b/changelog/4278.trivial.rst @@ -0,0 +1 @@ +Added a CACHEDIR.TAG file to the cache directory diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 22ce578fc..59265ad85 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -33,6 +33,13 @@ which provides the `--lf` and `--ff` options, as well as the `cache` fixture. 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 @@ class Cache(object): 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 30fe23aeb..29c2d8a1d 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