Skip to content

Commit 41c8dab

Browse files
authored
Merge pull request #11831 from bluetech/backport-11825-to-8.0.x
[8.0.x] avoid using __file__ in pytest_plugin_registered as can be wrong on Windows
2 parents b0c7f92 + 6f4cbd7 commit 41c8dab

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

Diff for: .pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ repos:
6464
additional_dependencies:
6565
- iniconfig>=1.1.0
6666
- attrs>=19.2.0
67+
- pluggy
6768
- packaging
6869
- tomli
6970
- types-pkg_resources

Diff for: changelog/11825.improvement.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The :hook:`pytest_plugin_registered` hook has a new ``plugin_name`` parameter containing the name by which ``plugin`` is registered.

Diff for: src/_pytest/config/__init__.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -496,15 +496,19 @@ def register(
496496
)
497497
)
498498
return None
499-
ret: Optional[str] = super().register(plugin, name)
500-
if ret:
499+
plugin_name = super().register(plugin, name)
500+
if plugin_name is not None:
501501
self.hook.pytest_plugin_registered.call_historic(
502-
kwargs=dict(plugin=plugin, manager=self)
502+
kwargs=dict(
503+
plugin=plugin,
504+
plugin_name=plugin_name,
505+
manager=self,
506+
)
503507
)
504508

505509
if isinstance(plugin, types.ModuleType):
506510
self.consider_module(plugin)
507-
return ret
511+
return plugin_name
508512

509513
def getplugin(self, name: str):
510514
# Support deprecated naming because plugins (xdist e.g.) use it.

Diff for: src/_pytest/fixtures.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -1495,25 +1495,27 @@ def getfixtureinfo(
14951495

14961496
return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
14971497

1498-
def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
1499-
nodeid = None
1500-
try:
1501-
p = absolutepath(plugin.__file__) # type: ignore[attr-defined]
1502-
except AttributeError:
1503-
pass
1498+
def pytest_plugin_registered(self, plugin: _PluggyPlugin, plugin_name: str) -> None:
1499+
# Fixtures defined in conftest plugins are only visible to within the
1500+
# conftest's directory. This is unlike fixtures in non-conftest plugins
1501+
# which have global visibility. So for conftests, construct the base
1502+
# nodeid from the plugin name (which is the conftest path).
1503+
if plugin_name and plugin_name.endswith("conftest.py"):
1504+
# Note: we explicitly do *not* use `plugin.__file__` here -- The
1505+
# difference is that plugin_name has the correct capitalization on
1506+
# case-insensitive systems (Windows) and other normalization issues
1507+
# (issue #11816).
1508+
conftestpath = absolutepath(plugin_name)
1509+
try:
1510+
nodeid = str(conftestpath.parent.relative_to(self.config.rootpath))
1511+
except ValueError:
1512+
nodeid = ""
1513+
if nodeid == ".":
1514+
nodeid = ""
1515+
if os.sep != nodes.SEP:
1516+
nodeid = nodeid.replace(os.sep, nodes.SEP)
15041517
else:
1505-
# Construct the base nodeid which is later used to check
1506-
# what fixtures are visible for particular tests (as denoted
1507-
# by their test id).
1508-
if p.name == "conftest.py":
1509-
try:
1510-
nodeid = str(p.parent.relative_to(self.config.rootpath))
1511-
except ValueError:
1512-
nodeid = ""
1513-
if nodeid == ".":
1514-
nodeid = ""
1515-
if os.sep != nodes.SEP:
1516-
nodeid = nodeid.replace(os.sep, nodes.SEP)
1518+
nodeid = None
15171519

15181520
self.parsefactories(plugin, nodeid)
15191521

Diff for: src/_pytest/hookspec.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
6666

6767
@hookspec(historic=True)
6868
def pytest_plugin_registered(
69-
plugin: "_PluggyPlugin", manager: "PytestPluginManager"
69+
plugin: "_PluggyPlugin",
70+
plugin_name: str,
71+
manager: "PytestPluginManager",
7072
) -> None:
7173
"""A new pytest plugin got registered.
7274
7375
:param plugin: The plugin module or instance.
74-
:param pytest.PytestPluginManager manager: pytest plugin manager.
76+
:param plugin_name: The name by which the plugin is registered.
77+
:param manager: The pytest plugin manager.
7578
7679
.. note::
7780
This hook is incompatible with hook wrappers.

0 commit comments

Comments
 (0)