Skip to content

Commit 02aa8ad

Browse files
committed
cacheprovider: use warnings directly
Allows for filtering of PytestCacheWarning. Using `_issue_warning_captured` is not necessary here, and was probably only used because the cacheprovider misses warnings during `pytest_sessionfinish`, which is also fixed here. I think the usage of `_issue_warning_captured` can be removed/reduced further, but also that this is good enough for now. Ref: #6681.
1 parent 67e69a7 commit 02aa8ad

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/_pytest/cacheprovider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def cache_dir_from_config(config):
7171
return resolve_from_str(config.getini("cache_dir"), config.rootdir)
7272

7373
def warn(self, fmt, **args):
74-
from _pytest.warnings import _issue_warning_captured
74+
import warnings
7575
from _pytest.warning_types import PytestCacheWarning
7676

77-
_issue_warning_captured(
77+
warnings.warn(
7878
PytestCacheWarning(fmt.format(**args) if args else fmt),
7979
self._config.hook,
8080
stacklevel=3,

src/_pytest/warnings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def pytest_terminal_summary(terminalreporter):
136136
yield
137137

138138

139+
@pytest.hookimpl(hookwrapper=True)
140+
def pytest_sessionfinish(session):
141+
config = session.config
142+
with catch_warnings_for_item(
143+
config=config, ihook=config.hook, when="config", item=None
144+
):
145+
yield
146+
147+
139148
def _issue_warning_captured(warning, hook, stacklevel):
140149
"""
141150
This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage:

testing/test_cacheprovider.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ def test_cache_writefail_permissions(self, testdir):
5656
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode)
5757

5858
@pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows")
59-
@pytest.mark.filterwarnings(
60-
"ignore:could not create cache path:pytest.PytestWarning"
61-
)
59+
@pytest.mark.filterwarnings("default")
6260
def test_cache_failure_warns(self, testdir, monkeypatch):
6361
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
6462
cache_dir = str(testdir.tmpdir.ensure_dir(".pytest_cache"))

0 commit comments

Comments
 (0)