Skip to content

Commit

Permalink
fixtures: avoid slow pm.get_name(plugin) call by using the new `plu…
Browse files Browse the repository at this point in the history
…gin_name` hook parameter
  • Loading branch information
bluetech committed Jan 17, 2024
1 parent 6c8655b commit d0c531d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog/11825.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :hook:`pytest_plugin_registered` hook has a new ``plugin_name`` parameter containing the name by which ``plugin`` is registered.
26 changes: 13 additions & 13 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,27 +1483,27 @@ def getfixtureinfo(

return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)

def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
nodeid = None
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).
def pytest_plugin_registered(self, plugin: _PluggyPlugin, plugin_name: str) -> None:
# Fixtures defined in conftest plugins are only visible to within the
# conftest's directory. This is unlike fixtures in non-conftest plugins
# which have global visibility. So for conftests, construct the base
# nodeid from the plugin name (which is the conftest path).
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)
# 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(p.parent.relative_to(self.config.rootpath))
nodeid = str(conftestpath.parent.relative_to(self.config.rootpath))
except ValueError:
nodeid = ""

Check warning on line 1500 in src/_pytest/fixtures.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/fixtures.py#L1499-L1500

Added lines #L1499 - L1500 were not covered by tests
if nodeid == ".":
nodeid = ""
if os.sep != nodes.SEP:
nodeid = nodeid.replace(os.sep, nodes.SEP)

Check warning on line 1504 in src/_pytest/fixtures.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/fixtures.py#L1504

Added line #L1504 was not covered by tests
else:
nodeid = None

self.parsefactories(plugin, nodeid)

Expand Down

0 comments on commit d0c531d

Please sign in to comment.