fixtures: extract a `_check_fixturedef` method
This stuff is less interesting when reading `_get_active_fixturedef`.
This commit is contained in:
parent
acf2971f46
commit
2e8fb9f140
|
@ -560,7 +560,6 @@ class FixtureRequest(abc.ABC):
|
||||||
# The are no fixtures with this name applicable for the function.
|
# The are no fixtures with this name applicable for the function.
|
||||||
if not fixturedefs:
|
if not fixturedefs:
|
||||||
raise FixtureLookupError(argname, self)
|
raise FixtureLookupError(argname, self)
|
||||||
|
|
||||||
# A fixture may override another fixture with the same name, e.g. a
|
# A fixture may override another fixture with the same name, e.g. a
|
||||||
# fixture in a module can override a fixture in a conftest, a fixture in
|
# fixture in a module can override a fixture in a conftest, a fixture in
|
||||||
# a class can override a fixture in the module, and so on.
|
# a class can override a fixture in the module, and so on.
|
||||||
|
@ -577,13 +576,11 @@ class FixtureRequest(abc.ABC):
|
||||||
# If already consumed all of the available levels, fail.
|
# If already consumed all of the available levels, fail.
|
||||||
if -index > len(fixturedefs):
|
if -index > len(fixturedefs):
|
||||||
raise FixtureLookupError(argname, self)
|
raise FixtureLookupError(argname, self)
|
||||||
|
|
||||||
fixturedef = fixturedefs[index]
|
fixturedef = fixturedefs[index]
|
||||||
|
|
||||||
# Prepare a SubRequest object for calling the fixture.
|
# Prepare a SubRequest object for calling the fixture.
|
||||||
funcitem = self._pyfuncitem
|
|
||||||
try:
|
try:
|
||||||
callspec = funcitem.callspec
|
callspec = self._pyfuncitem.callspec
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
callspec = None
|
callspec = None
|
||||||
if callspec is not None and argname in callspec.params:
|
if callspec is not None and argname in callspec.params:
|
||||||
|
@ -595,7 +592,22 @@ class FixtureRequest(abc.ABC):
|
||||||
param = NOTSET
|
param = NOTSET
|
||||||
param_index = 0
|
param_index = 0
|
||||||
scope = fixturedef._scope
|
scope = fixturedef._scope
|
||||||
|
self._check_fixturedef_without_param(fixturedef)
|
||||||
|
self._check_scope(fixturedef, scope)
|
||||||
|
subrequest = SubRequest(
|
||||||
|
self, scope, param, param_index, fixturedef, _ispytest=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Make sure the fixture value is cached, running it if it isn't
|
||||||
|
fixturedef.execute(request=subrequest)
|
||||||
|
|
||||||
|
self._fixture_defs[argname] = fixturedef
|
||||||
|
return fixturedef
|
||||||
|
|
||||||
|
def _check_fixturedef_without_param(self, fixturedef: "FixtureDef[object]") -> None:
|
||||||
|
"""Check that this request is allowed to execute this fixturedef without
|
||||||
|
a param."""
|
||||||
|
funcitem = self._pyfuncitem
|
||||||
has_params = fixturedef.params is not None
|
has_params = fixturedef.params is not None
|
||||||
fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
|
fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
|
||||||
if has_params and fixtures_not_supported:
|
if has_params and fixtures_not_supported:
|
||||||
|
@ -606,14 +618,12 @@ class FixtureRequest(abc.ABC):
|
||||||
)
|
)
|
||||||
fail(msg, pytrace=False)
|
fail(msg, pytrace=False)
|
||||||
if has_params:
|
if has_params:
|
||||||
frame = inspect.stack()[2]
|
frame = inspect.stack()[3]
|
||||||
frameinfo = inspect.getframeinfo(frame[0])
|
frameinfo = inspect.getframeinfo(frame[0])
|
||||||
source_path = absolutepath(frameinfo.filename)
|
source_path = absolutepath(frameinfo.filename)
|
||||||
source_lineno = frameinfo.lineno
|
source_lineno = frameinfo.lineno
|
||||||
try:
|
try:
|
||||||
source_path_str = str(
|
source_path_str = str(source_path.relative_to(funcitem.config.rootpath))
|
||||||
source_path.relative_to(funcitem.config.rootpath)
|
|
||||||
)
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
source_path_str = str(source_path)
|
source_path_str = str(source_path)
|
||||||
location = getlocation(fixturedef.func, funcitem.config.rootpath)
|
location = getlocation(fixturedef.func, funcitem.config.rootpath)
|
||||||
|
@ -627,19 +637,6 @@ class FixtureRequest(abc.ABC):
|
||||||
)
|
)
|
||||||
fail(msg, pytrace=False)
|
fail(msg, pytrace=False)
|
||||||
|
|
||||||
# Check if a higher-level scoped fixture accesses a lower level one.
|
|
||||||
self._check_scope(fixturedef, scope)
|
|
||||||
|
|
||||||
subrequest = SubRequest(
|
|
||||||
self, scope, param, param_index, fixturedef, _ispytest=True
|
|
||||||
)
|
|
||||||
|
|
||||||
# Make sure the fixture value is cached, running it if it isn't
|
|
||||||
fixturedef.execute(request=subrequest)
|
|
||||||
|
|
||||||
self._fixture_defs[argname] = fixturedef
|
|
||||||
return fixturedef
|
|
||||||
|
|
||||||
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
|
def _get_fixturestack(self) -> List["FixtureDef[Any]"]:
|
||||||
values = [request._fixturedef for request in self._iter_chain()]
|
values = [request._fixturedef for request in self._iter_chain()]
|
||||||
values.reverse()
|
values.reverse()
|
||||||
|
|
Loading…
Reference in New Issue