Support glob argument with ``--cache-show``

This commit is contained in:
Daniel Hahler 2019-04-03 15:56:42 +02:00
parent e04936fc29
commit eb5b2e0db5
4 changed files with 62 additions and 15 deletions

View File

@ -0,0 +1 @@
The ``--cache-show`` option/action accepts an optional glob to show only matching cache entries.

View File

@ -247,7 +247,7 @@ See the :ref:`cache-api` for more details.
Inspecting Cache content
-------------------------------
------------------------
You can always peek at the content of the cache using the
``--cache-show`` command line option:
@ -260,7 +260,7 @@ You can always peek at the content of the cache using the
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: /home/sweet/project
cachedir: $PYTHON_PREFIX/.pytest_cache
------------------------------- cache values -------------------------------
--------------------------- cache values for '*' ---------------------------
cache/lastfailed contains:
{'test_50.py::test_num[17]': True,
'test_50.py::test_num[25]': True,
@ -277,8 +277,25 @@ You can always peek at the content of the cache using the
======================= no tests ran in 0.12 seconds =======================
``--cache-show`` takes an optional argument to specify a glob pattern for
filtering:
.. code-block:: pytest
$ pytest --cache-show example/*
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR, inifile:
cachedir: $PYTHON_PREFIX/.pytest_cache
----------------------- cache values for 'example/*' -----------------------
example/value contains:
42
======================= no tests ran in 0.12 seconds =======================
Clearing Cache content
-------------------------------
----------------------
You can instruct pytest to clear all cache files and values
by adding the ``--cache-clear`` option like this:

View File

@ -292,9 +292,13 @@ def pytest_addoption(parser):
)
group.addoption(
"--cache-show",
action="store_true",
action="append",
nargs="?",
dest="cacheshow",
help="show cache contents, don't perform collection or tests",
help=(
"show cache contents, don't perform collection or tests. "
"Optional argument: glob (default: '*')."
),
)
group.addoption(
"--cache-clear",
@ -369,11 +373,16 @@ def cacheshow(config, session):
if not config.cache._cachedir.is_dir():
tw.line("cache is empty")
return 0
glob = config.option.cacheshow[0]
if glob is None:
glob = "*"
dummy = object()
basedir = config.cache._cachedir
vdir = basedir / "v"
tw.sep("-", "cache values")
for valpath in sorted(x for x in vdir.rglob("*") if x.is_file()):
tw.sep("-", "cache values for %r" % glob)
for valpath in sorted(x for x in vdir.rglob(glob) if x.is_file()):
key = valpath.relative_to(vdir)
val = config.cache.get(key, dummy)
if val is dummy:
@ -385,8 +394,8 @@ def cacheshow(config, session):
ddir = basedir / "d"
if ddir.is_dir():
contents = sorted(ddir.rglob("*"))
tw.sep("-", "cache directories")
contents = sorted(ddir.rglob(glob))
tw.sep("-", "cache directories for %r" % glob)
for p in contents:
# if p.check(dir=1):
# print("%s/" % p.relto(basedir))

View File

@ -196,6 +196,7 @@ def test_cache_show(testdir):
"""
def pytest_configure(config):
config.cache.set("my/name", [1,2,3])
config.cache.set("my/hello", "world")
config.cache.set("other/some", {1:2})
dp = config.cache.makedir("mydb")
dp.ensure("hello")
@ -204,20 +205,39 @@ def test_cache_show(testdir):
)
result = testdir.runpytest()
assert result.ret == 5 # no tests executed
result = testdir.runpytest("--cache-show")
result.stdout.fnmatch_lines_random(
result.stdout.fnmatch_lines(
[
"*cachedir:*",
"-*cache values*-",
"*my/name contains:",
"*- cache values for '[*]' -*",
"cache/nodeids contains:",
"my/name contains:",
" [1, 2, 3]",
"*other/some contains*",
" {*1*: 2}",
"-*cache directories*-",
"other/some contains:",
" {*'1': 2}",
"*- cache directories for '[*]' -*",
"*mydb/hello*length 0*",
"*mydb/world*length 0*",
]
)
assert result.ret == 0
result = testdir.runpytest("--cache-show", "*/hello")
result.stdout.fnmatch_lines(
[
"*cachedir:*",
"*- cache values for '[*]/hello' -*",
"my/hello contains:",
" *'world'",
"*- cache directories for '[*]/hello' -*",
"d/mydb/hello*length 0*",
]
)
stdout = result.stdout.str()
assert "other/some" not in stdout
assert "d/mydb/world" not in stdout
assert result.ret == 0
class TestLastFailed(object):