From c26f389c09479bc34a0ddb8a531089fa58f9bd80 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sun, 17 May 2020 11:20:56 -0300 Subject: [PATCH] Refactor handling of non-top-level pytest_plugins handling Decided to move the 'if' logic together with the error message, as this leaves the _importconftest method cleaner. --- src/_pytest/config/__init__.py | 38 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index a4fc6e7c1..68c3822d0 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -286,19 +286,6 @@ def _prepareconfig( raise -def _fail_on_non_top_pytest_plugins(conftestpath, confcutdir): - msg = ( - "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:\n" - "It affects the entire test suite instead of just below the conftest as expected.\n" - " {}\n" - "Please move it to a top level conftest file at the rootdir:\n" - " {}\n" - "For more information, visit:\n" - " https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files" - ) - fail(msg.format(conftestpath, confcutdir), pytrace=False) - - class PytestPluginManager(PluginManager): """ Overwrites :py:class:`pluggy.PluginManager ` to add pytest-specific @@ -527,15 +514,11 @@ class PytestPluginManager(PluginManager): try: mod = conftestpath.pyimport() - if ( - hasattr(mod, "pytest_plugins") - and self._configured - and not self._using_pyargs - ): - _fail_on_non_top_pytest_plugins(conftestpath, self._confcutdir) except Exception as e: raise ConftestImportFailure(conftestpath, sys.exc_info()) from e + self._check_non_top_pytest_plugins(mod, conftestpath) + self._conftest_plugins.add(mod) self._conftestpath2mod[key] = mod dirpath = conftestpath.dirpath() @@ -548,6 +531,23 @@ class PytestPluginManager(PluginManager): self.consider_conftest(mod) return mod + def _check_non_top_pytest_plugins(self, mod, conftestpath): + if ( + hasattr(mod, "pytest_plugins") + and self._configured + and not self._using_pyargs + ): + msg = ( + "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:\n" + "It affects the entire test suite instead of just below the conftest as expected.\n" + " {}\n" + "Please move it to a top level conftest file at the rootdir:\n" + " {}\n" + "For more information, visit:\n" + " https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files" + ) + fail(msg.format(conftestpath, self._confcutdir), pytrace=False) + # # API for bootstrapping plugin loading #