cacheprovider: do not write README/.gitignore to existing dir

Fixes https://github.com/pytest-dev/pytest/issues/4393.
This commit is contained in:
Daniel Hahler 2018-11-14 21:55:37 +01:00
parent 9dec146edf
commit 0385c27343
3 changed files with 35 additions and 4 deletions

View File

@ -0,0 +1 @@
Do not create ``.gitignore``/``README.md`` files in existing cache directories.

View File

@ -108,6 +108,10 @@ class Cache(object):
""" """
path = self._getvaluepath(key) path = self._getvaluepath(key)
try: try:
if path.parent.is_dir():
cache_dir_exists_already = True
else:
cache_dir_exists_already = self._cachedir.exists()
path.parent.mkdir(exist_ok=True, parents=True) path.parent.mkdir(exist_ok=True, parents=True)
except (IOError, OSError): except (IOError, OSError):
self.warn("could not create cache path {path}", path=path) self.warn("could not create cache path {path}", path=path)
@ -119,6 +123,7 @@ class Cache(object):
else: else:
with f: with f:
json.dump(value, f, indent=2, sort_keys=True) json.dump(value, f, indent=2, sort_keys=True)
if not cache_dir_exists_already:
self._ensure_supporting_files() self._ensure_supporting_files()
def _ensure_supporting_files(self): def _ensure_supporting_files(self):
@ -128,8 +133,10 @@ class Cache(object):
if not readme_path.is_file(): if not readme_path.is_file():
readme_path.write_text(README_CONTENT) readme_path.write_text(README_CONTENT)
msg = u"# created by pytest automatically, do not change\n*" gitignore_path = self._cachedir.joinpath(".gitignore")
self._cachedir.joinpath(".gitignore").write_text(msg, encoding="UTF-8") if not gitignore_path.is_file():
msg = u"# Created by pytest automatically.\n*"
gitignore_path.write_text(msg, encoding="UTF-8")
class LFPlugin(object): class LFPlugin(object):

View File

@ -899,5 +899,28 @@ def test_gitignore(testdir):
config = testdir.parseconfig() config = testdir.parseconfig()
cache = Cache.for_config(config) cache = Cache.for_config(config)
cache.set("foo", "bar") cache.set("foo", "bar")
msg = "# created by pytest automatically, do not change\n*" msg = "# Created by pytest automatically.\n*"
assert cache._cachedir.joinpath(".gitignore").read_text(encoding="UTF-8") == msg gitignore_path = cache._cachedir.joinpath(".gitignore")
assert gitignore_path.read_text(encoding="UTF-8") == msg
# Does not overwrite existing/custom one.
gitignore_path.write_text("")
cache.set("something", "else")
assert gitignore_path.read_text(encoding="UTF-8") == ""
def test_does_not_create_boilerplate_in_existing_dirs(testdir):
from _pytest.cacheprovider import Cache
testdir.makeini(
"""
[pytest]
cache_dir = .
"""
)
config = testdir.parseconfig()
cache = Cache.for_config(config)
cache.set("foo", "bar")
assert not os.path.exists(".gitignore")
assert not os.path.exists("README.md")