Merge pull request #5035 from blueyed/cache-glob

Support glob argument with ``--cache-show``
This commit is contained in:
Daniel Hahler 2019-04-05 10:25:19 +02:00 committed by GitHub
commit 8ad99c5cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 Inspecting Cache content
------------------------------- ------------------------
You can always peek at the content of the cache using the You can always peek at the content of the cache using the
``--cache-show`` command line option: ``--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 cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: /home/sweet/project rootdir: /home/sweet/project
cachedir: $PYTHON_PREFIX/.pytest_cache cachedir: $PYTHON_PREFIX/.pytest_cache
------------------------------- cache values ------------------------------- --------------------------- cache values for '*' ---------------------------
cache/lastfailed contains: cache/lastfailed contains:
{'test_50.py::test_num[17]': True, {'test_50.py::test_num[17]': True,
'test_50.py::test_num[25]': 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 ======================= ======================= 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 Clearing Cache content
------------------------------- ----------------------
You can instruct pytest to clear all cache files and values You can instruct pytest to clear all cache files and values
by adding the ``--cache-clear`` option like this: by adding the ``--cache-clear`` option like this:

View File

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

View File

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