Merge pull request #4394 from blueyed/cache-ensure-files

cacheprovider: do not write README/.gitignore to existing dir
This commit is contained in:
Bruno Oliveira 2018-11-23 09:07:49 -02:00 committed by GitHub
commit e4ae33d592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

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

View File

@ -110,6 +110,10 @@ class Cache(object):
"""
path = self._getvaluepath(key)
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)
except (IOError, OSError):
self.warn("could not create cache path {path}", path=path)
@ -121,6 +125,7 @@ class Cache(object):
else:
with f:
json.dump(value, f, indent=2, sort_keys=True)
if not cache_dir_exists_already:
self._ensure_supporting_files()
def _ensure_supporting_files(self):
@ -130,8 +135,10 @@ class Cache(object):
if not readme_path.is_file():
readme_path.write_text(README_CONTENT)
msg = u"# created by pytest automatically, do not change\n*"
self._cachedir.joinpath(".gitignore").write_text(msg, encoding="UTF-8")
gitignore_path = self._cachedir.joinpath(".gitignore")
if not gitignore_path.is_file():
msg = u"# Created by pytest automatically.\n*"
gitignore_path.write_text(msg, encoding="UTF-8")
class LFPlugin(object):

View File

@ -899,5 +899,29 @@ def test_gitignore(testdir):
config = testdir.parseconfig()
cache = Cache.for_config(config)
cache.set("foo", "bar")
msg = "# created by pytest automatically, do not change\n*"
assert cache._cachedir.joinpath(".gitignore").read_text(encoding="UTF-8") == msg
msg = "# Created by pytest automatically.\n*"
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(u"custom")
cache.set("something", "else")
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
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 os.path.isdir("v") # cache contents
assert not os.path.exists(".gitignore")
assert not os.path.exists("README.md")