From 9aa6b0903bd0f2a5ecb83cdecbe0c533f8e93938 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 14 Sep 2018 15:15:12 -0300 Subject: [PATCH 1/2] .pytest_cache is now automatically ignored by Git --- changelog/3286.bugfix.rst | 1 + src/_pytest/cacheprovider.py | 9 ++++++--- testing/test_cacheprovider.py | 11 +++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 changelog/3286.bugfix.rst diff --git a/changelog/3286.bugfix.rst b/changelog/3286.bugfix.rst new file mode 100644 index 000000000..d69e08531 --- /dev/null +++ b/changelog/3286.bugfix.rst @@ -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. diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 791cf3a33..87e24894b 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -115,15 +115,18 @@ class Cache(object): 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 """ diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 6d425f95b..5d73dc846 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -884,3 +884,14 @@ class TestReadme(object): ) 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 From 87ddb2dbd5c35cc1aaaa3da025f46abe408bf5cc Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Fri, 14 Sep 2018 15:25:45 -0300 Subject: [PATCH 2/2] Change flaky test_request_garbage to provide more debug information This test fails *very* rarely when running in xdist. --- testing/python/fixture.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/testing/python/fixture.py b/testing/python/fixture.py index fc3eee42b..33c040e31 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import sys import textwrap import pytest @@ -488,6 +489,10 @@ class TestRequestBasic(object): 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): testdir.makepyfile( """ @@ -498,33 +503,32 @@ class TestRequestBasic(object): @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(