Debian Codegraph @

testing/web-platform/tests/tools/third_party/pytest/src/_pytest/setuponly.py

raw
1 from typing import Generator
2 from typing import Optional
3 from typing import Union
4
5 from _pytest._io.saferepr import saferepr
6 from _pytest.config import Config
7 from _pytest.config import ExitCode
8 from _pytest.config.argparsing import Parser
9 from _pytest.fixtures import FixtureDef
10 from _pytest.fixtures import SubRequest
11 from _pytest.scope import Scope
12 import pytest
13
14
15 def pytest_addoption(parser: Parser) -> None:
16 group = parser.getgroup("debugconfig")
17 group.addoption(
18 "--setuponly",
19 "--setup-only",
20 action="store_true",
21 help="Only setup fixtures, do not execute tests",
22 )
23 group.addoption(
24 "--setupshow",
25 "--setup-show",
26 action="store_true",
27 help="Show setup of fixtures while executing tests",
28 )
29
30
31 @pytest.hookimpl(wrapper=True)
32 def pytest_fixture_setup(
33 fixturedef: FixtureDef[object], request: SubRequest
34 ) -> Generator[None, object, object]:
35 try:
36 return (yield)
37 finally:
38 if request.config.option.setupshow:
39 if hasattr(request, "param"):
40 # Save the fixture parameter so ._show_fixture_action() can
41 # display it now and during the teardown (in .finish()).
42 if fixturedef.ids:
43 if callable(fixturedef.ids):
44 param = fixturedef.ids(request.param)
45 else:
46 param = fixturedef.ids[request.param_index]
47 else:
48 param = request.param
49 fixturedef.cached_param = param # type: ignore[attr-defined]
50 _show_fixture_action(fixturedef, request.config, "SETUP")
51
52
53 def pytest_fixture_post_finalizer(
54 fixturedef: FixtureDef[object], request: SubRequest
55 ) -> None:
56 if fixturedef.cached_result is not None:
57 config = request.config
58 if config.option.setupshow:
59 _show_fixture_action(fixturedef, request.config, "TEARDOWN")
60 if hasattr(fixturedef, "cached_param"):
61 del fixturedef.cached_param
62
63
64 def _show_fixture_action(
65 fixturedef: FixtureDef[object], config: Config, msg: str
66 ) -> None:
67 capman = config.pluginmanager.getplugin("capturemanager")
68 if capman:
69 capman.suspend_global_capture()
70
71 tw = config.get_terminal_writer()
72 tw.line()
73 # Use smaller indentation the higher the scope: Session = 0, Package = 1, etc.
74 scope_indent = list(reversed(Scope)).index(fixturedef._scope)
75 tw.write(" " * 2 * scope_indent)
76 tw.write(
77 "{step} {scope} {fixture}".format( # noqa: UP032 (Readability)
78 step=msg.ljust(8), # align the output to TEARDOWN
79 scope=fixturedef.scope[0].upper(),
80 fixture=fixturedef.argname,
81 )
82 )
83
84 if msg == "SETUP":
85 deps = sorted(arg for arg in fixturedef.argnames if arg != "request")
86 if deps:
87 tw.write(" (fixtures used: {})".format(", ".join(deps)))
88
89 if hasattr(fixturedef, "cached_param"):
90 tw.write(f"[{saferepr(fixturedef.cached_param, maxsize=42)}]")
91
92 tw.flush()
93
94 if capman:
95 capman.resume_global_capture()
96
97
98 @pytest.hookimpl(tryfirst=True)
99 def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
100 if config.option.setuponly:
101 config.option.setupshow = True
102 return None