avoid using __file__ in pytest_plugin_registered as can be wrong on Windows

This commit is contained in:
woutdenolf 2024-01-16 15:41:01 +01:00 committed by Ran Benita
parent a6708b9254
commit 6e9f566d79
1 changed files with 19 additions and 17 deletions

View File

@ -1485,23 +1485,25 @@ class FixtureManager:
def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
nodeid = None
try:
p = absolutepath(plugin.__file__) # type: ignore[attr-defined]
except AttributeError:
pass
else:
# Construct the base nodeid which is later used to check
# 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)
plugin_name = self.config.pluginmanager.get_name(plugin)
# Construct the base nodeid which is later used to check
# what fixtures are visible for particular tests (as denoted
# by their test id).
if plugin_name and plugin_name.endswith("conftest.py"):
# The plugin name is assumed to be equal to plugin.__file__
# for conftest plugins. The difference is that plugin_name
# has the correct capitalization on capital-insensitive
# systems (Windows).
p = absolutepath(plugin_name)
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)