Skip to content

Commit e4ae33d

Browse files
authored
Merge pull request #4394 from blueyed/cache-ensure-files
cacheprovider: do not write README/.gitignore to existing dir
2 parents abaf496 + c5c728c commit e4ae33d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

changelog/4393.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Do not create ``.gitignore``/``README.md`` files in existing cache directories.

src/_pytest/cacheprovider.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def set(self, key, value):
110110
"""
111111
path = self._getvaluepath(key)
112112
try:
113+
if path.parent.is_dir():
114+
cache_dir_exists_already = True
115+
else:
116+
cache_dir_exists_already = self._cachedir.exists()
113117
path.parent.mkdir(exist_ok=True, parents=True)
114118
except (IOError, OSError):
115119
self.warn("could not create cache path {path}", path=path)
@@ -121,6 +125,7 @@ def set(self, key, value):
121125
else:
122126
with f:
123127
json.dump(value, f, indent=2, sort_keys=True)
128+
if not cache_dir_exists_already:
124129
self._ensure_supporting_files()
125130

126131
def _ensure_supporting_files(self):
@@ -130,8 +135,10 @@ def _ensure_supporting_files(self):
130135
if not readme_path.is_file():
131136
readme_path.write_text(README_CONTENT)
132137

133-
msg = u"# created by pytest automatically, do not change\n*"
134-
self._cachedir.joinpath(".gitignore").write_text(msg, encoding="UTF-8")
138+
gitignore_path = self._cachedir.joinpath(".gitignore")
139+
if not gitignore_path.is_file():
140+
msg = u"# Created by pytest automatically.\n*"
141+
gitignore_path.write_text(msg, encoding="UTF-8")
135142

136143

137144
class LFPlugin(object):

testing/test_cacheprovider.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,5 +899,29 @@ def test_gitignore(testdir):
899899
config = testdir.parseconfig()
900900
cache = Cache.for_config(config)
901901
cache.set("foo", "bar")
902-
msg = "# created by pytest automatically, do not change\n*"
903-
assert cache._cachedir.joinpath(".gitignore").read_text(encoding="UTF-8") == msg
902+
msg = "# Created by pytest automatically.\n*"
903+
gitignore_path = cache._cachedir.joinpath(".gitignore")
904+
assert gitignore_path.read_text(encoding="UTF-8") == msg
905+
906+
# Does not overwrite existing/custom one.
907+
gitignore_path.write_text(u"custom")
908+
cache.set("something", "else")
909+
assert gitignore_path.read_text(encoding="UTF-8") == "custom"
910+
911+
912+
def test_does_not_create_boilerplate_in_existing_dirs(testdir):
913+
from _pytest.cacheprovider import Cache
914+
915+
testdir.makeini(
916+
"""
917+
[pytest]
918+
cache_dir = .
919+
"""
920+
)
921+
config = testdir.parseconfig()
922+
cache = Cache.for_config(config)
923+
cache.set("foo", "bar")
924+
925+
assert os.path.isdir("v") # cache contents
926+
assert not os.path.exists(".gitignore")
927+
assert not os.path.exists("README.md")

0 commit comments

Comments
 (0)