File: test_warnings.py

package info (click to toggle)
python-pluggy 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 464 kB
  • sloc: python: 3,205; sh: 58; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from pathlib import Path

import pytest

from pluggy import HookimplMarker
from pluggy import HookspecMarker
from pluggy import PluggyTeardownRaisedWarning
from pluggy import PluginManager


hookspec = HookspecMarker("example")
hookimpl = HookimplMarker("example")


def test_teardown_raised_warning(pm: PluginManager) -> None:
    class Api:
        @hookspec
        def my_hook(self):
            raise NotImplementedError()

    pm.add_hookspecs(Api)

    class Plugin1:
        @hookimpl
        def my_hook(self):
            pass

    class Plugin2:
        @hookimpl(hookwrapper=True)
        def my_hook(self):
            yield
            1 / 0

    class Plugin3:
        @hookimpl(hookwrapper=True)
        def my_hook(self):
            yield

    pm.register(Plugin1(), "plugin1")
    pm.register(Plugin2(), "plugin2")
    pm.register(Plugin3(), "plugin3")
    with pytest.warns(
        PluggyTeardownRaisedWarning,
        match=r"\bplugin2\b.*\bmy_hook\b.*\n.*ZeroDivisionError",
    ) as wc:
        with pytest.raises(ZeroDivisionError):
            pm.hook.my_hook()
    assert len(wc.list) == 1
    assert Path(wc.list[0].filename).name == "test_warnings.py"