Skip to content

Commit

Permalink
Support glob argument with --cache-show
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Apr 3, 2019
1 parent 1410d3d commit 0723685
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog/5035.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``--cache-show`` option/action accepts an optional glob to show only matching cache entries.
21 changes: 15 additions & 6 deletions src/_pytest/cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,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",
Expand Down Expand Up @@ -360,11 +364,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:
Expand All @@ -376,8 +385,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))
Expand Down
32 changes: 26 additions & 6 deletions testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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")
Expand All @@ -203,20 +204,39 @@ def pytest_configure(config):
)
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):
Expand Down

0 comments on commit 0723685

Please sign in to comment.