Merge pull request #11825 from woutdenolf/fix_missing_fixture_issue
avoid using __file__ in pytest_plugin_registered as can be wrong on Windows
This commit is contained in:
commit
0f5aa5a7d2
|
@ -64,6 +64,7 @@ repos:
|
||||||
additional_dependencies:
|
additional_dependencies:
|
||||||
- iniconfig>=1.1.0
|
- iniconfig>=1.1.0
|
||||||
- attrs>=19.2.0
|
- attrs>=19.2.0
|
||||||
|
- pluggy
|
||||||
- packaging
|
- packaging
|
||||||
- tomli
|
- tomli
|
||||||
- types-pkg_resources
|
- types-pkg_resources
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
The :hook:`pytest_plugin_registered` hook has a new ``plugin_name`` parameter containing the name by which ``plugin`` is registered.
|
|
@ -490,15 +490,19 @@ class PytestPluginManager(PluginManager):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
ret: Optional[str] = super().register(plugin, name)
|
plugin_name = super().register(plugin, name)
|
||||||
if ret:
|
if plugin_name is not None:
|
||||||
self.hook.pytest_plugin_registered.call_historic(
|
self.hook.pytest_plugin_registered.call_historic(
|
||||||
kwargs=dict(plugin=plugin, manager=self)
|
kwargs=dict(
|
||||||
|
plugin=plugin,
|
||||||
|
plugin_name=plugin_name,
|
||||||
|
manager=self,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if isinstance(plugin, types.ModuleType):
|
if isinstance(plugin, types.ModuleType):
|
||||||
self.consider_module(plugin)
|
self.consider_module(plugin)
|
||||||
return ret
|
return plugin_name
|
||||||
|
|
||||||
def getplugin(self, name: str):
|
def getplugin(self, name: str):
|
||||||
# Support deprecated naming because plugins (xdist e.g.) use it.
|
# Support deprecated naming because plugins (xdist e.g.) use it.
|
||||||
|
|
|
@ -1483,25 +1483,27 @@ class FixtureManager:
|
||||||
|
|
||||||
return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
|
return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
|
||||||
|
|
||||||
def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
|
def pytest_plugin_registered(self, plugin: _PluggyPlugin, plugin_name: str) -> None:
|
||||||
nodeid = None
|
# Fixtures defined in conftest plugins are only visible to within the
|
||||||
try:
|
# conftest's directory. This is unlike fixtures in non-conftest plugins
|
||||||
p = absolutepath(plugin.__file__) # type: ignore[attr-defined]
|
# which have global visibility. So for conftests, construct the base
|
||||||
except AttributeError:
|
# nodeid from the plugin name (which is the conftest path).
|
||||||
pass
|
if plugin_name and plugin_name.endswith("conftest.py"):
|
||||||
|
# Note: we explicitly do *not* use `plugin.__file__` here -- The
|
||||||
|
# difference is that plugin_name has the correct capitalization on
|
||||||
|
# case-insensitive systems (Windows) and other normalization issues
|
||||||
|
# (issue #11816).
|
||||||
|
conftestpath = absolutepath(plugin_name)
|
||||||
|
try:
|
||||||
|
nodeid = str(conftestpath.parent.relative_to(self.config.rootpath))
|
||||||
|
except ValueError:
|
||||||
|
nodeid = ""
|
||||||
|
if nodeid == ".":
|
||||||
|
nodeid = ""
|
||||||
|
if os.sep != nodes.SEP:
|
||||||
|
nodeid = nodeid.replace(os.sep, nodes.SEP)
|
||||||
else:
|
else:
|
||||||
# Construct the base nodeid which is later used to check
|
nodeid = None
|
||||||
# what fixtures are visible for particular tests (as denoted
|
|
||||||
# by their test id).
|
|
||||||
if p.name == "conftest.py":
|
|
||||||
try:
|
|
||||||
nodeid = str(p.parent.relative_to(self.config.rootpath))
|
|
||||||
except ValueError:
|
|
||||||
nodeid = ""
|
|
||||||
if nodeid == ".":
|
|
||||||
nodeid = ""
|
|
||||||
if os.sep != nodes.SEP:
|
|
||||||
nodeid = nodeid.replace(os.sep, nodes.SEP)
|
|
||||||
|
|
||||||
self.parsefactories(plugin, nodeid)
|
self.parsefactories(plugin, nodeid)
|
||||||
|
|
||||||
|
|
|
@ -63,12 +63,15 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
|
||||||
|
|
||||||
@hookspec(historic=True)
|
@hookspec(historic=True)
|
||||||
def pytest_plugin_registered(
|
def pytest_plugin_registered(
|
||||||
plugin: "_PluggyPlugin", manager: "PytestPluginManager"
|
plugin: "_PluggyPlugin",
|
||||||
|
plugin_name: str,
|
||||||
|
manager: "PytestPluginManager",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""A new pytest plugin got registered.
|
"""A new pytest plugin got registered.
|
||||||
|
|
||||||
:param plugin: The plugin module or instance.
|
:param plugin: The plugin module or instance.
|
||||||
:param manager: pytest plugin manager.
|
:param plugin_name: The name by which the plugin is registered.
|
||||||
|
:param manager: The pytest plugin manager.
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
This hook is incompatible with hook wrappers.
|
This hook is incompatible with hook wrappers.
|
||||||
|
|
Loading…
Reference in New Issue