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: https://github.com/pytest-dev/pytest/issues/6681.
This commit is contained in:
		
							parent
							
								
									67e69a7e49
								
							
						
					
					
						commit
						02aa8adae1
					
				|  | @ -71,10 +71,10 @@ class Cache: | ||||||
|         return resolve_from_str(config.getini("cache_dir"), config.rootdir) |         return resolve_from_str(config.getini("cache_dir"), config.rootdir) | ||||||
| 
 | 
 | ||||||
|     def warn(self, fmt, **args): |     def warn(self, fmt, **args): | ||||||
|         from _pytest.warnings import _issue_warning_captured |         import warnings | ||||||
|         from _pytest.warning_types import PytestCacheWarning |         from _pytest.warning_types import PytestCacheWarning | ||||||
| 
 | 
 | ||||||
|         _issue_warning_captured( |         warnings.warn( | ||||||
|             PytestCacheWarning(fmt.format(**args) if args else fmt), |             PytestCacheWarning(fmt.format(**args) if args else fmt), | ||||||
|             self._config.hook, |             self._config.hook, | ||||||
|             stacklevel=3, |             stacklevel=3, | ||||||
|  |  | ||||||
|  | @ -136,6 +136,15 @@ def pytest_terminal_summary(terminalreporter): | ||||||
|         yield |         yield | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @pytest.hookimpl(hookwrapper=True) | ||||||
|  | def pytest_sessionfinish(session): | ||||||
|  |     config = session.config | ||||||
|  |     with catch_warnings_for_item( | ||||||
|  |         config=config, ihook=config.hook, when="config", item=None | ||||||
|  |     ): | ||||||
|  |         yield | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| def _issue_warning_captured(warning, hook, stacklevel): | def _issue_warning_captured(warning, hook, stacklevel): | ||||||
|     """ |     """ | ||||||
|     This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: |     This function should be used instead of calling ``warnings.warn`` directly when we are in the "configure" stage: | ||||||
|  |  | ||||||
|  | @ -56,9 +56,7 @@ class TestNewAPI: | ||||||
|             testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode) |             testdir.tmpdir.ensure_dir(".pytest_cache").chmod(mode) | ||||||
| 
 | 
 | ||||||
|     @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") |     @pytest.mark.skipif(sys.platform.startswith("win"), reason="no chmod on windows") | ||||||
|     @pytest.mark.filterwarnings( |     @pytest.mark.filterwarnings("default") | ||||||
|         "ignore:could not create cache path:pytest.PytestWarning" |  | ||||||
|     ) |  | ||||||
|     def test_cache_failure_warns(self, testdir, monkeypatch): |     def test_cache_failure_warns(self, testdir, monkeypatch): | ||||||
|         monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") |         monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1") | ||||||
|         cache_dir = str(testdir.tmpdir.ensure_dir(".pytest_cache")) |         cache_dir = str(testdir.tmpdir.ensure_dir(".pytest_cache")) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue