Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4393.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not create ``.gitignore``/``README.md`` files in existing cache directories.
11 changes: 9 additions & 2 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ def set(self, key, value):
"""
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)
Expand All @@ -119,6 +123,7 @@ def set(self, key, value):
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):
Expand All @@ -128,8 +133,10 @@ def _ensure_supporting_files(self):
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):
Expand Down
28 changes: 26 additions & 2 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this to ensure . is the actual cache dir (just to be safe).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

assert not os.path.exists(".gitignore")
assert not os.path.exists("README.md")