Skip to content

Commit 5f97711

Browse files
authored
Merge pull request #3787 from hsoft/no-plugin-autoload
Add option to disable plugin auto-loading
2 parents a76cc8f + 126896f commit 5f97711

File tree

6 files changed

+36
-1
lines changed

6 files changed

+36
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ Tzu-ping Chung
207207
Vasily Kuznetsov
208208
Victor Uriarte
209209
Vidar T. Fauske
210+
Virgil Dupras
210211
Vitaly Lashmanov
211212
Vlad Dragos
212213
Wil Cooley

changelog/3784.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add option to disable plugin auto-loading.

doc/en/reference.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,11 @@ Contains comma-separated list of modules that should be loaded as plugins:
866866
867867
export PYTEST_PLUGINS=mymodule.plugin,xdist
868868
869+
PYTEST_DISABLE_PLUGIN_AUTOLOAD
870+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
871+
872+
When set, disables plugin auto-loading through setuptools entrypoints. Only explicitly specified plugins will be
873+
loaded.
869874

870875
PYTEST_CURRENT_TEST
871876
~~~~~~~~~~~~~~~~~~~

src/_pytest/config/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,10 @@ def _mark_plugins_for_rewrite(self, hook):
701701

702702
self.pluginmanager.rewrite_hook = hook
703703

704+
if os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
705+
# We don't autoload from setuptools entry points, no need to continue.
706+
return
707+
704708
# 'RECORD' available for plugins installed normally (pip install)
705709
# 'SOURCES.txt' available for plugins installed in dev mode (pip install -e)
706710
# for installed plugins 'SOURCES.txt' returns an empty list, and vice-versa
@@ -726,7 +730,10 @@ def _preparse(self, args, addopts=True):
726730
self._checkversion()
727731
self._consider_importhook(args)
728732
self.pluginmanager.consider_preparse(args)
729-
self.pluginmanager.load_setuptools_entrypoints("pytest11")
733+
if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
734+
# Don't autoload from setuptools entry point. Only explicitly specified
735+
# plugins are going to be loaded.
736+
self.pluginmanager.load_setuptools_entrypoints("pytest11")
730737
self.pluginmanager.consider_env()
731738
self.known_args_namespace = ns = self._parser.parse_known_args(
732739
args, namespace=copy.copy(self.option)

src/_pytest/helpconfig.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def showhelp(config):
156156
vars = [
157157
("PYTEST_ADDOPTS", "extra command line options"),
158158
("PYTEST_PLUGINS", "comma-separated plugins to load during startup"),
159+
("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "set to disable plugin auto-loading"),
159160
("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals"),
160161
]
161162
for name, help in vars:

testing/test_config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,26 @@ def load(self):
605605
)
606606

607607

608+
@pytest.mark.parametrize(
609+
"parse_args,should_load", [(("-p", "mytestplugin"), True), ((), False)]
610+
)
611+
def test_disable_plugin_autoload(testdir, monkeypatch, parse_args, should_load):
612+
pkg_resources = pytest.importorskip("pkg_resources")
613+
614+
def my_iter(name):
615+
raise AssertionError("Should not be called")
616+
617+
class PseudoPlugin(object):
618+
x = 42
619+
620+
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
621+
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
622+
monkeypatch.setitem(sys.modules, "mytestplugin", PseudoPlugin())
623+
config = testdir.parseconfig(*parse_args)
624+
has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None
625+
assert has_loaded == should_load
626+
627+
608628
def test_cmdline_processargs_simple(testdir):
609629
testdir.makeconftest(
610630
"""

0 commit comments

Comments
 (0)