Skip to content

Commit 8ad99c5

Browse files
authored
Merge pull request #5035 from blueyed/cache-glob
Support glob argument with ``--cache-show``
2 parents e04936f + eb5b2e0 commit 8ad99c5

File tree

4 files changed

+62
-15
lines changed

4 files changed

+62
-15
lines changed

changelog/5035.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``--cache-show`` option/action accepts an optional glob to show only matching cache entries.

doc/en/cache.rst

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ See the :ref:`cache-api` for more details.
247247

248248

249249
Inspecting Cache content
250-
-------------------------------
250+
------------------------
251251

252252
You can always peek at the content of the cache using the
253253
``--cache-show`` command line option:
@@ -260,7 +260,7 @@ You can always peek at the content of the cache using the
260260
cachedir: $PYTHON_PREFIX/.pytest_cache
261261
rootdir: /home/sweet/project
262262
cachedir: $PYTHON_PREFIX/.pytest_cache
263-
------------------------------- cache values -------------------------------
263+
--------------------------- cache values for '*' ---------------------------
264264
cache/lastfailed contains:
265265
{'test_50.py::test_num[17]': True,
266266
'test_50.py::test_num[25]': True,
@@ -277,8 +277,25 @@ You can always peek at the content of the cache using the
277277
278278
======================= no tests ran in 0.12 seconds =======================
279279
280+
``--cache-show`` takes an optional argument to specify a glob pattern for
281+
filtering:
282+
283+
.. code-block:: pytest
284+
285+
$ pytest --cache-show example/*
286+
=========================== test session starts ============================
287+
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
288+
cachedir: $PYTHON_PREFIX/.pytest_cache
289+
rootdir: $REGENDOC_TMPDIR, inifile:
290+
cachedir: $PYTHON_PREFIX/.pytest_cache
291+
----------------------- cache values for 'example/*' -----------------------
292+
example/value contains:
293+
42
294+
295+
======================= no tests ran in 0.12 seconds =======================
296+
280297
Clearing Cache content
281-
-------------------------------
298+
----------------------
282299

283300
You can instruct pytest to clear all cache files and values
284301
by adding the ``--cache-clear`` option like this:

src/_pytest/cacheprovider.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,13 @@ def pytest_addoption(parser):
292292
)
293293
group.addoption(
294294
"--cache-show",
295-
action="store_true",
295+
action="append",
296+
nargs="?",
296297
dest="cacheshow",
297-
help="show cache contents, don't perform collection or tests",
298+
help=(
299+
"show cache contents, don't perform collection or tests. "
300+
"Optional argument: glob (default: '*')."
301+
),
298302
)
299303
group.addoption(
300304
"--cache-clear",
@@ -369,11 +373,16 @@ def cacheshow(config, session):
369373
if not config.cache._cachedir.is_dir():
370374
tw.line("cache is empty")
371375
return 0
376+
377+
glob = config.option.cacheshow[0]
378+
if glob is None:
379+
glob = "*"
380+
372381
dummy = object()
373382
basedir = config.cache._cachedir
374383
vdir = basedir / "v"
375-
tw.sep("-", "cache values")
376-
for valpath in sorted(x for x in vdir.rglob("*") if x.is_file()):
384+
tw.sep("-", "cache values for %r" % glob)
385+
for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
377386
key = valpath.relative_to(vdir)
378387
val = config.cache.get(key, dummy)
379388
if val is dummy:
@@ -385,8 +394,8 @@ def cacheshow(config, session):
385394

386395
ddir = basedir / "d"
387396
if ddir.is_dir():
388-
contents = sorted(ddir.rglob("*"))
389-
tw.sep("-", "cache directories")
397+
contents = sorted(ddir.rglob(glob))
398+
tw.sep("-", "cache directories for %r" % glob)
390399
for p in contents:
391400
# if p.check(dir=1):
392401
# print("%s/" % p.relto(basedir))

testing/test_cacheprovider.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def test_cache_show(testdir):
196196
"""
197197
def pytest_configure(config):
198198
config.cache.set("my/name", [1,2,3])
199+
config.cache.set("my/hello", "world")
199200
config.cache.set("other/some", {1:2})
200201
dp = config.cache.makedir("mydb")
201202
dp.ensure("hello")
@@ -204,20 +205,39 @@ def pytest_configure(config):
204205
)
205206
result = testdir.runpytest()
206207
assert result.ret == 5 # no tests executed
208+
207209
result = testdir.runpytest("--cache-show")
208-
result.stdout.fnmatch_lines_random(
210+
result.stdout.fnmatch_lines(
209211
[
210212
"*cachedir:*",
211-
"-*cache values*-",
212-
"*my/name contains:",
213+
"*- cache values for '[*]' -*",
214+
"cache/nodeids contains:",
215+
"my/name contains:",
213216
" [1, 2, 3]",
214-
"*other/some contains*",
215-
" {*1*: 2}",
216-
"-*cache directories*-",
217+
"other/some contains:",
218+
" {*'1': 2}",
219+
"*- cache directories for '[*]' -*",
217220
"*mydb/hello*length 0*",
218221
"*mydb/world*length 0*",
219222
]
220223
)
224+
assert result.ret == 0
225+
226+
result = testdir.runpytest("--cache-show", "*/hello")
227+
result.stdout.fnmatch_lines(
228+
[
229+
"*cachedir:*",
230+
"*- cache values for '[*]/hello' -*",
231+
"my/hello contains:",
232+
" *'world'",
233+
"*- cache directories for '[*]/hello' -*",
234+
"d/mydb/hello*length 0*",
235+
]
236+
)
237+
stdout = result.stdout.str()
238+
assert "other/some" not in stdout
239+
assert "d/mydb/world" not in stdout
240+
assert result.ret == 0
221241

222242

223243
class TestLastFailed(object):

0 commit comments

Comments
 (0)