From 63899c76a40a5feabc312eb37e869386faf316f8 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 8 Nov 2021 21:53:06 +0200 Subject: [PATCH] legacypath: support late-loading of pytester plugin Fixes #9280. Previous approach didn't support pytester being loaded using `pytest_plugins = "pytester"` in a conftest. --- src/_pytest/legacypath.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/_pytest/legacypath.py b/src/_pytest/legacypath.py index 15bb98fb0..743c06d55 100644 --- a/src/_pytest/legacypath.py +++ b/src/_pytest/legacypath.py @@ -400,13 +400,11 @@ def Node_fspath_set(self: Node, value: LEGACY_PATH) -> None: self.path = Path(value) +@pytest.hookimpl def pytest_configure(config: pytest.Config) -> None: mp = pytest.MonkeyPatch() config.add_cleanup(mp.undo) - if config.pluginmanager.has_plugin("pytester"): - config.pluginmanager.register(LegacyTestdirPlugin, "legacypath-pytester") - if config.pluginmanager.has_plugin("tmpdir"): # Create TmpdirFactory and attach it to the config object. # @@ -452,3 +450,14 @@ def pytest_configure(config: pytest.Config) -> None: # Add Node.fspath property. mp.setattr(Node, "fspath", property(Node_fspath, Node_fspath_set), raising=False) + + +@pytest.hookimpl +def pytest_plugin_registered( + plugin: object, manager: pytest.PytestPluginManager +) -> None: + # pytester is not loaded by default and is commonly loaded from a conftest, + # so checking for it in `pytest_configure` is not enough. + is_pytester = plugin is manager.get_plugin("pytester") + if is_pytester and not manager.is_registered(LegacyTestdirPlugin): + manager.register(LegacyTestdirPlugin, "legacypath-pytester")