Skip to content

Commit

Permalink
Extract getfixturedefs compatibility function
Browse files Browse the repository at this point in the history
  • Loading branch information
youtux committed Jan 21, 2024
1 parent cc86d99 commit 67d708f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ parse = "*"
parse-type = "*"
pytest = ">=6.2.0"
typing-extensions = "*"
packaging = "*"

[tool.poetry.group.dev.dependencies]
tox = ">=4.11.3"
Expand Down
21 changes: 21 additions & 0 deletions src/pytest_bdd/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections.abc import Sequence
from importlib.metadata import version
from typing import Optional

from _pytest.fixtures import FixtureDef, FixtureManager
from _pytest.nodes import Node
from packaging.version import Version
from packaging.version import parse as parse_version

pytest_version = parse_version(version("pytest"))


if pytest_version >= Version("8.1"):

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Optional[Sequence[FixtureDef]]:
return fixturemanager.getfixturedefs(fixturename, node)

else:

def getfixturedefs(fixturemanager: FixtureManager, fixturename: str, node: Node) -> Optional[Sequence[FixtureDef]]:
return fixturemanager.getfixturedefs(fixturename, node.nodeid)
7 changes: 2 additions & 5 deletions src/pytest_bdd/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import os.path
from typing import TYPE_CHECKING, cast

import pytest
from _pytest._io import TerminalWriter
from mako.lookup import TemplateLookup

from .compat import getfixturedefs
from .feature import get_features
from .scenario import inject_fixturedefs_for_step, make_python_docstring, make_python_name, make_string_literal
from .steps import get_step_fixture_name
Expand Down Expand Up @@ -130,10 +130,7 @@ def _find_step_fixturedef(
"""Find step fixturedef."""
with inject_fixturedefs_for_step(step=step, fixturemanager=fixturemanager, node=item):
bdd_name = get_step_fixture_name(step=step)
if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (8, 1):
return fixturemanager.getfixturedefs(bdd_name, item)
else:
return fixturemanager.getfixturedefs(bdd_name, item.nodeid)
return getfixturedefs(fixturemanager, bdd_name, item)


def parse_feature_files(paths: list[str], **kwargs: Any) -> tuple[list[Feature], list[ScenarioTemplate], list[Step]]:
Expand Down
11 changes: 5 additions & 6 deletions src/pytest_bdd/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
from typing_extensions import ParamSpec

from . import exceptions
from .compat import getfixturedefs
from .feature import get_feature, get_features
from .steps import StepFunctionContext, get_step_fixture_name, inject_fixture
from .utils import CONFIG_STACK, get_args, get_caller_module_locals, get_caller_module_path

if TYPE_CHECKING:
from _pytest.mark.structures import ParameterSet
from _pytest.nodes import Node

from .parser import Feature, Scenario, ScenarioTemplate, Step

Expand All @@ -42,7 +44,7 @@
ALPHA_REGEX = re.compile(r"^\d+_*")


def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node) -> Iterable[FixtureDef[Any]]:
def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node: Node) -> Iterable[FixtureDef[Any]]:
"""Find the fixture defs that can parse a step."""
# happens to be that _arg2fixturedefs is changed during the iteration so we use a copy
fixture_def_by_name = list(fixturemanager._arg2fixturedefs.items())
Expand All @@ -59,10 +61,7 @@ def find_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node)
if not match:
continue

if hasattr(pytest, "version_tuple") and pytest.version_tuple >= (8, 1):
fixturedefs = fixturemanager.getfixturedefs(fixturename, node)
else:
fixturedefs = fixturemanager.getfixturedefs(fixturename, node.nodeid)
fixturedefs = getfixturedefs(fixturemanager, fixturename, node)
if fixturedef not in (fixturedefs or []):
continue

Expand Down Expand Up @@ -117,7 +116,7 @@ def iterparentnodeids(nodeid: str) -> Iterator[str]:


@contextlib.contextmanager
def inject_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node) -> Iterator[None]:
def inject_fixturedefs_for_step(step: Step, fixturemanager: FixtureManager, node: Node) -> Iterator[None]:
"""Inject fixture definitions that can parse a step.
We fist iterate over all the fixturedefs that can parse the step.
Expand Down

0 comments on commit 67d708f

Please sign in to comment.