Skip to content
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/3286.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``.pytest_cache`` directory is now automatically ignored by Git. Users who would like to contribute a solution for other SCMs please consult/comment on this issue.
9 changes: 6 additions & 3 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ def set(self, key, value):
else:
with f:
json.dump(value, f, indent=2, sort_keys=True)
self._ensure_readme()

def _ensure_readme(self):
self._ensure_supporting_files()

def _ensure_supporting_files(self):
"""Create supporting files in the cache dir that are not really part of the cache."""
if self._cachedir.is_dir():
readme_path = self._cachedir / "README.md"
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")


class LFPlugin(object):
""" Plugin which implements the --lf (run last-failing) option """
Expand Down
32 changes: 18 additions & 14 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import sys
import textwrap

import pytest
Expand Down Expand Up @@ -488,6 +489,10 @@ def test_method(self, something):
assert len(arg2fixturedefs) == 1
assert arg2fixturedefs["something"][0].argname == "something"

@pytest.mark.skipif(
hasattr(sys, "pypy_version_info"),
reason="this method of test doesn't work on pypy",
)
def test_request_garbage(self, testdir):
Copy link
Member Author

Choose a reason for hiding this comment

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

Change flaky test_request_garbage to provide more debug information

This test fails very rarely when running in xdist.

Not a part of the purpose of this PR, but I didn't feel like making a new PR just for that.

testdir.makepyfile(
"""
Expand All @@ -498,33 +503,32 @@ def test_request_garbage(self, testdir):

@pytest.fixture(autouse=True)
def something(request):
# this method of test doesn't work on pypy
if hasattr(sys, "pypy_version_info"):
yield
else:
original = gc.get_debug()
gc.set_debug(gc.DEBUG_SAVEALL)
gc.collect()
original = gc.get_debug()
gc.set_debug(gc.DEBUG_SAVEALL)
gc.collect()

yield
yield

try:
gc.collect()
leaked_types = sum(1 for _ in gc.garbage
if isinstance(_, PseudoFixtureDef))

# debug leaked types if the test fails
print(leaked_types)

gc.garbage[:] = []

try:
assert leaked_types == 0
finally:
gc.set_debug(original)
assert leaked_types == 0
finally:
gc.set_debug(original)

def test_func():
pass
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
result = testdir.runpytest()
result.stdout.fnmatch_lines("* 1 passed in *")

def test_getfixturevalue_recursive(self, testdir):
testdir.makeconftest(
Expand Down
11 changes: 11 additions & 0 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,3 +884,14 @@ def test_always_passes():
)
testdir.runpytest()
assert self.check_readme(testdir) is True


def test_gitignore(testdir):
"""Ensure we automatically create .gitignore file in the pytest_cache directory (#3286)."""
from _pytest.cacheprovider import Cache

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