mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 12:34:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,7 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Generator
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
import pytest
|
||||
from _pytest._io.saferepr import saferepr
|
||||
from _pytest.config import Config
|
||||
from _pytest.config import ExitCode
|
||||
|
|
@ -9,34 +12,33 @@ from _pytest.config.argparsing import Parser
|
|||
from _pytest.fixtures import FixtureDef
|
||||
from _pytest.fixtures import SubRequest
|
||||
from _pytest.scope import Scope
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_addoption(parser: Parser) -> None:
|
||||
group = parser.getgroup("debugconfig")
|
||||
group = parser.getgroup('debugconfig')
|
||||
group.addoption(
|
||||
"--setuponly",
|
||||
"--setup-only",
|
||||
action="store_true",
|
||||
help="Only setup fixtures, do not execute tests",
|
||||
'--setuponly',
|
||||
'--setup-only',
|
||||
action='store_true',
|
||||
help='Only setup fixtures, do not execute tests',
|
||||
)
|
||||
group.addoption(
|
||||
"--setupshow",
|
||||
"--setup-show",
|
||||
action="store_true",
|
||||
help="Show setup of fixtures while executing tests",
|
||||
'--setupshow',
|
||||
'--setup-show',
|
||||
action='store_true',
|
||||
help='Show setup of fixtures while executing tests',
|
||||
)
|
||||
|
||||
|
||||
@pytest.hookimpl(wrapper=True)
|
||||
def pytest_fixture_setup(
|
||||
fixturedef: FixtureDef[object], request: SubRequest
|
||||
fixturedef: FixtureDef[object], request: SubRequest,
|
||||
) -> Generator[None, object, object]:
|
||||
try:
|
||||
return (yield)
|
||||
finally:
|
||||
if request.config.option.setupshow:
|
||||
if hasattr(request, "param"):
|
||||
if hasattr(request, 'param'):
|
||||
# Save the fixture parameter so ._show_fixture_action() can
|
||||
# display it now and during the teardown (in .finish()).
|
||||
if fixturedef.ids:
|
||||
|
|
@ -47,24 +49,24 @@ def pytest_fixture_setup(
|
|||
else:
|
||||
param = request.param
|
||||
fixturedef.cached_param = param # type: ignore[attr-defined]
|
||||
_show_fixture_action(fixturedef, request.config, "SETUP")
|
||||
_show_fixture_action(fixturedef, request.config, 'SETUP')
|
||||
|
||||
|
||||
def pytest_fixture_post_finalizer(
|
||||
fixturedef: FixtureDef[object], request: SubRequest
|
||||
fixturedef: FixtureDef[object], request: SubRequest,
|
||||
) -> None:
|
||||
if fixturedef.cached_result is not None:
|
||||
config = request.config
|
||||
if config.option.setupshow:
|
||||
_show_fixture_action(fixturedef, request.config, "TEARDOWN")
|
||||
if hasattr(fixturedef, "cached_param"):
|
||||
_show_fixture_action(fixturedef, request.config, 'TEARDOWN')
|
||||
if hasattr(fixturedef, 'cached_param'):
|
||||
del fixturedef.cached_param # type: ignore[attr-defined]
|
||||
|
||||
|
||||
def _show_fixture_action(
|
||||
fixturedef: FixtureDef[object], config: Config, msg: str
|
||||
fixturedef: FixtureDef[object], config: Config, msg: str,
|
||||
) -> None:
|
||||
capman = config.pluginmanager.getplugin("capturemanager")
|
||||
capman = config.pluginmanager.getplugin('capturemanager')
|
||||
if capman:
|
||||
capman.suspend_global_capture()
|
||||
|
||||
|
|
@ -72,22 +74,22 @@ def _show_fixture_action(
|
|||
tw.line()
|
||||
# Use smaller indentation the higher the scope: Session = 0, Package = 1, etc.
|
||||
scope_indent = list(reversed(Scope)).index(fixturedef._scope)
|
||||
tw.write(" " * 2 * scope_indent)
|
||||
tw.write(' ' * 2 * scope_indent)
|
||||
tw.write(
|
||||
"{step} {scope} {fixture}".format( # noqa: UP032 (Readability)
|
||||
'{step} {scope} {fixture}'.format( # noqa: UP032 (Readability)
|
||||
step=msg.ljust(8), # align the output to TEARDOWN
|
||||
scope=fixturedef.scope[0].upper(),
|
||||
fixture=fixturedef.argname,
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
if msg == "SETUP":
|
||||
deps = sorted(arg for arg in fixturedef.argnames if arg != "request")
|
||||
if msg == 'SETUP':
|
||||
deps = sorted(arg for arg in fixturedef.argnames if arg != 'request')
|
||||
if deps:
|
||||
tw.write(" (fixtures used: {})".format(", ".join(deps)))
|
||||
tw.write(' (fixtures used: {})'.format(', '.join(deps)))
|
||||
|
||||
if hasattr(fixturedef, "cached_param"):
|
||||
tw.write(f"[{saferepr(fixturedef.cached_param, maxsize=42)}]") # type: ignore[attr-defined]
|
||||
if hasattr(fixturedef, 'cached_param'):
|
||||
tw.write(f'[{saferepr(fixturedef.cached_param, maxsize=42)}]') # type: ignore[attr-defined]
|
||||
|
||||
tw.flush()
|
||||
|
||||
|
|
@ -96,7 +98,7 @@ def _show_fixture_action(
|
|||
|
||||
|
||||
@pytest.hookimpl(tryfirst=True)
|
||||
def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
|
||||
def pytest_cmdline_main(config: Config) -> int | ExitCode | None:
|
||||
if config.option.setuponly:
|
||||
config.option.setupshow = True
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue