|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import importlib |
3 | 4 | import sys |
4 | 5 | import textwrap |
| 6 | +import warnings |
5 | 7 | from types import SimpleNamespace |
| 8 | +from typing import Any |
6 | 9 |
|
7 | 10 | import pytest |
8 | 11 |
|
| 12 | +from _pytask.pluginmanager import hookimpl |
9 | 13 | from _pytask.warnings_utils import _resolve_warning_category |
10 | 14 | from pytask import ExitCode |
11 | 15 | from pytask import build |
12 | 16 | from pytask import cli |
13 | 17 | from tests.conftest import run_in_subprocess |
14 | 18 |
|
15 | 19 |
|
| 20 | +def _register_plugin_for_build(monkeypatch: pytest.MonkeyPatch, plugin: object) -> None: |
| 21 | + """Add a plugin to the fresh plugin manager created by ``build``.""" |
| 22 | + build_module = importlib.import_module("_pytask.build") |
| 23 | + get_plugin_manager = build_module.get_plugin_manager |
| 24 | + |
| 25 | + def get_plugin_manager_with_plugin(): |
| 26 | + plugin_manager = get_plugin_manager() |
| 27 | + plugin_manager.register(plugin) |
| 28 | + return plugin_manager |
| 29 | + |
| 30 | + monkeypatch.setattr( |
| 31 | + build_module, "get_plugin_manager", get_plugin_manager_with_plugin |
| 32 | + ) |
| 33 | + |
| 34 | + |
16 | 35 | def test_resolve_warning_category_uses_import_module(monkeypatch): |
17 | 36 | class CustomWarning(Warning): |
18 | 37 | pass |
@@ -236,3 +255,52 @@ def test_wrong_value_in_config_in_filterwarnings(tmp_path, runner): |
236 | 255 | result = runner.invoke(cli, [tmp_path.as_posix()]) |
237 | 256 | assert result.exit_code == ExitCode.CONFIGURATION_FAILED |
238 | 257 | assert "'filterwarnings' must be a str, list[str] or None." in result.output |
| 258 | + |
| 259 | + |
| 260 | +@pytest.mark.parametrize("hook_name", ["pytask_parse_config", "pytask_post_parse"]) |
| 261 | +@pytest.mark.parametrize("tryfirst", [True, False]) |
| 262 | +def test_filterwarnings_apply_to_configuration_hooks( |
| 263 | + monkeypatch: pytest.MonkeyPatch, |
| 264 | + tmp_path, |
| 265 | + hook_name: str, |
| 266 | + tryfirst: bool, |
| 267 | +) -> None: |
| 268 | + """Configured filters apply to early and regular configuration hooks.""" |
| 269 | + |
| 270 | + def emit_warning(config: dict[str, Any]) -> None: # noqa: ARG001 |
| 271 | + warnings.warn("from configuration hook", UserWarning, stacklevel=1) |
| 272 | + |
| 273 | + plugin = SimpleNamespace() |
| 274 | + setattr(plugin, hook_name, hookimpl(tryfirst=tryfirst)(emit_warning)) |
| 275 | + _register_plugin_for_build(monkeypatch, plugin) |
| 276 | + |
| 277 | + with warnings.catch_warnings(record=True) as records: |
| 278 | + warnings.simplefilter("always") |
| 279 | + session = build( |
| 280 | + paths=tmp_path, |
| 281 | + filterwarnings=["ignore::UserWarning"], |
| 282 | + ) |
| 283 | + |
| 284 | + assert session.exit_code == ExitCode.OK |
| 285 | + assert not any( |
| 286 | + str(record.message) == "from configuration hook" for record in records |
| 287 | + ) |
| 288 | + |
| 289 | + |
| 290 | +def test_error_filter_applies_to_configuration_hooks( |
| 291 | + monkeypatch: pytest.MonkeyPatch, tmp_path |
| 292 | +) -> None: |
| 293 | + """An error filter turns configuration warnings into configuration failures.""" |
| 294 | + |
| 295 | + def emit_warning(config: dict[str, Any]) -> None: # noqa: ARG001 |
| 296 | + warnings.warn("from configuration hook", UserWarning, stacklevel=1) |
| 297 | + |
| 298 | + plugin = SimpleNamespace(pytask_post_parse=hookimpl(emit_warning)) |
| 299 | + _register_plugin_for_build(monkeypatch, plugin) |
| 300 | + |
| 301 | + session = build( |
| 302 | + paths=tmp_path, |
| 303 | + filterwarnings=["error::UserWarning"], |
| 304 | + ) |
| 305 | + |
| 306 | + assert session.exit_code == ExitCode.CONFIGURATION_FAILED |
0 commit comments