Merge pull request #3982 from nicoddemus/ignore-pytest-cache

Ignore pytest cache
This commit is contained in:
Ronny Pfannschmidt 2018-09-15 07:21:45 +02:00 committed by GitHub
commit f53eff93db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 17 deletions

View File

@ -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.

View File

@ -115,15 +115,18 @@ 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)
self._ensure_readme() self._ensure_supporting_files()
def _ensure_readme(self):
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(): if self._cachedir.is_dir():
readme_path = self._cachedir / "README.md" readme_path = self._cachedir / "README.md"
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*"
self._cachedir.joinpath(".gitignore").write_text(msg, encoding="UTF-8")
class LFPlugin(object): class LFPlugin(object):
""" Plugin which implements the --lf (run last-failing) option """ """ Plugin which implements the --lf (run last-failing) option """

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import sys
import textwrap import textwrap
import pytest import pytest
@ -488,6 +489,10 @@ class TestRequestBasic(object):
assert len(arg2fixturedefs) == 1 assert len(arg2fixturedefs) == 1
assert arg2fixturedefs["something"][0].argname == "something" 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): def test_request_garbage(self, testdir):
testdir.makepyfile( testdir.makepyfile(
""" """
@ -498,33 +503,32 @@ class TestRequestBasic(object):
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def something(request): def something(request):
# this method of test doesn't work on pypy original = gc.get_debug()
if hasattr(sys, "pypy_version_info"): gc.set_debug(gc.DEBUG_SAVEALL)
yield gc.collect()
else:
original = gc.get_debug()
gc.set_debug(gc.DEBUG_SAVEALL)
gc.collect()
yield yield
try:
gc.collect() gc.collect()
leaked_types = sum(1 for _ in gc.garbage leaked_types = sum(1 for _ in gc.garbage
if isinstance(_, PseudoFixtureDef)) if isinstance(_, PseudoFixtureDef))
# debug leaked types if the test fails
print(leaked_types)
gc.garbage[:] = [] gc.garbage[:] = []
try: assert leaked_types == 0
assert leaked_types == 0 finally:
finally: gc.set_debug(original)
gc.set_debug(original)
def test_func(): def test_func():
pass pass
""" """
) )
reprec = testdir.inline_run() result = testdir.runpytest()
reprec.assertoutcome(passed=1) result.stdout.fnmatch_lines("* 1 passed in *")
def test_getfixturevalue_recursive(self, testdir): def test_getfixturevalue_recursive(self, testdir):
testdir.makeconftest( testdir.makeconftest(

View File

@ -884,3 +884,14 @@ class TestReadme(object):
) )
testdir.runpytest() testdir.runpytest()
assert self.check_readme(testdir) is True 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