diff --git a/changelog/5035.feature.rst b/changelog/5035.feature.rst new file mode 100644 index 000000000..36211f9f4 --- /dev/null +++ b/changelog/5035.feature.rst @@ -0,0 +1 @@ +The ``--cache-show`` option/action accepts an optional glob to show only matching cache entries. diff --git a/doc/en/cache.rst b/doc/en/cache.rst index 8baf88113..e47dce44c 100644 --- a/doc/en/cache.rst +++ b/doc/en/cache.rst @@ -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: diff --git a/src/_pytest/cacheprovider.py b/src/_pytest/cacheprovider.py index 246b8dfd8..a1200ce37 100755 --- a/src/_pytest/cacheprovider.py +++ b/src/_pytest/cacheprovider.py @@ -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)) diff --git a/testing/test_cacheprovider.py b/testing/test_cacheprovider.py index 85603edf4..41e7ffd79 100644 --- a/testing/test_cacheprovider.py +++ b/testing/test_cacheprovider.py @@ -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):