Skip to content

Commit 7fc9d4c

Browse files
authored
Use warnings module directly with cacheprovider (#6740)
2 parents 67e69a7 + 2b5adc8 commit 7fc9d4c

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
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: 10 additions & 4 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"))
@@ -70,7 +68,15 @@ def test_cache_failure_warns(self, testdir, monkeypatch):
7068
assert result.ret == 1
7169
# warnings from nodeids, lastfailed, and stepwise
7270
result.stdout.fnmatch_lines(
73-
["*could not create cache path*", "*3 warnings*"]
71+
[
72+
# Validate location/stacklevel of warning from cacheprovider.
73+
"*= warnings summary =*",
74+
"*/cacheprovider.py:314",
75+
" */cacheprovider.py:314: PytestCacheWarning: could not create cache path "
76+
"{}/v/cache/nodeids".format(cache_dir),
77+
' config.cache.set("cache/nodeids", self.cached_nodeids)',
78+
"*1 failed, 3 warnings in*",
79+
]
7480
)
7581
finally:
7682
testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode)

testing/test_warnings.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -712,27 +712,6 @@ def test_dummy():
712712
assert "resultlog.py" in file
713713
assert func == "pytest_configure"
714714

715-
def test_issue4445_cacheprovider_set(self, testdir, capwarn):
716-
"""#4445: Make sure the warning points to a reasonable location
717-
See origin of _issue_warning_captured at: _pytest.cacheprovider.py:59
718-
"""
719-
testdir.tmpdir.join(".pytest_cache").write("something wrong")
720-
testdir.runpytest(plugins=[capwarn()])
721-
722-
# with stacklevel=3 the warning originates from one stacklevel above
723-
# _issue_warning_captured in cacheprovider.Cache.set and is thrown
724-
# when there are errors during cache folder creation
725-
726-
# set is called twice (in module stepwise and in cacheprovider) so emits
727-
# two warnings when there are errors during cache folder creation. (is this intentional?)
728-
assert len(capwarn.captured) == 2
729-
warning, location = capwarn.captured.pop()
730-
file, lineno, func = location
731-
732-
assert "could not create cache path" in str(warning.message)
733-
assert "cacheprovider.py" in file
734-
assert func == "set"
735-
736715
def test_issue4445_issue5928_mark_generator(self, testdir):
737716
"""#4445 and #5928: Make sure the warning from an unknown mark points to
738717
the test file where this mark is used.

0 commit comments

Comments
 (0)