Skip to content

Commit de98939

Browse files
authored
Merge pull request #3608 from avirlrma/features
add reamde for .pytest_cache
2 parents 9d60cf2 + 0d3914b commit de98939

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

changelog/3519.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Now a ``README.md`` file is created in ``.pytest_cache`` to make it clear why the directory exists.

src/_pytest/cacheprovider.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616
import os
1717
from os.path import sep as _sep, altsep as _altsep
18+
from textwrap import dedent
1819

1920

2021
class Cache(object):
@@ -103,6 +104,26 @@ def set(self, key, value):
103104
with f:
104105
self.trace("cache-write %s: %r" % (key, value))
105106
json.dump(value, f, indent=2, sort_keys=True)
107+
self._ensure_readme()
108+
109+
def _ensure_readme(self):
110+
111+
content_readme = dedent(
112+
"""\
113+
# pytest cache directory #
114+
115+
This directory contains data from the pytest's cache plugin,
116+
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.
117+
118+
**Do not** commit this to version control.
119+
120+
See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information.
121+
"""
122+
)
123+
if self._cachedir.check(dir=True):
124+
readme_path = self._cachedir.join("README.md")
125+
if not readme_path.check(file=True):
126+
readme_path.write(content_readme)
106127

107128

108129
class LFPlugin(object):

testing/test_cacheprovider.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import shutil
88

9-
pytest_plugins = "pytester",
9+
pytest_plugins = ("pytester",)
1010

1111

1212
class TestNewAPI(object):
@@ -818,3 +818,31 @@ def test_1(num): assert num
818818
"*test_1/test_1.py::test_1[2*",
819819
]
820820
)
821+
822+
823+
class TestReadme(object):
824+
825+
def check_readme(self, testdir):
826+
config = testdir.parseconfigure()
827+
readme = config.cache._cachedir.join("README.md")
828+
return readme.isfile()
829+
830+
def test_readme_passed(self, testdir):
831+
testdir.makepyfile(
832+
"""
833+
def test_always_passes():
834+
assert 1
835+
"""
836+
)
837+
testdir.runpytest()
838+
assert self.check_readme(testdir) is True
839+
840+
def test_readme_failed(self, testdir):
841+
testdir.makepyfile(
842+
"""
843+
def test_always_passes():
844+
assert 0
845+
"""
846+
)
847+
testdir.runpytest()
848+
assert self.check_readme(testdir) is True

0 commit comments

Comments
 (0)