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.
This commit is contained in:
Bruno Oliveira 2020-05-17 11:20:56 -03:00
parent 9e1e7fcabe
commit c26f389c09
1 changed files with 19 additions and 19 deletions

View File

@ -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 <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
#