Skip to content

Commit 2861f69

Browse files
committed
pluginmanager.consider_preparse: add exclude_only kwarg
* fix/adjust test_disable_plugin_autoload * adjust test_plugin_loading_order
1 parent 1586653 commit 2861f69

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

changelog/6443.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Plugins specified with ``-p`` are now loaded before internal ones.

src/_pytest/config/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def get_config(args=None, plugins=None):
193193

194194
if args is not None:
195195
# Handle any "-p no:plugin" args.
196-
pluginmanager.consider_preparse(args)
196+
pluginmanager.consider_preparse(args, exclude_only=True)
197197

198198
for spec in default_plugins:
199199
pluginmanager.import_plugin(spec)
@@ -499,7 +499,7 @@ def _importconftest(self, conftestpath):
499499
#
500500
#
501501

502-
def consider_preparse(self, args):
502+
def consider_preparse(self, args, *, exclude_only=False):
503503
i = 0
504504
n = len(args)
505505
while i < n:
@@ -516,6 +516,8 @@ def consider_preparse(self, args):
516516
parg = opt[2:]
517517
else:
518518
continue
519+
if exclude_only and not parg.startswith("no:"):
520+
continue
519521
self.consider_pluginarg(parg)
520522

521523
def consider_pluginarg(self, arg):
@@ -951,7 +953,7 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
951953

952954
self._checkversion()
953955
self._consider_importhook(args)
954-
self.pluginmanager.consider_preparse(args)
956+
self.pluginmanager.consider_preparse(args, exclude_only=False)
955957
if not os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"):
956958
# Don't autoload from setuptools entry point. Only explicitly specified
957959
# plugins are going to be loaded.

testing/test_config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,13 @@ class Distribution:
659659
class PseudoPlugin:
660660
x = 42
661661

662+
attrs_used = []
663+
664+
def __getattr__(self, name):
665+
assert name == "__loader__"
666+
self.attrs_used.append(name)
667+
return object()
668+
662669
def distributions():
663670
return (Distribution(),)
664671

@@ -668,6 +675,10 @@ def distributions():
668675
config = testdir.parseconfig(*parse_args)
669676
has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None
670677
assert has_loaded == should_load
678+
if should_load:
679+
assert PseudoPlugin.attrs_used == ["__loader__"]
680+
else:
681+
assert PseudoPlugin.attrs_used == []
671682

672683

673684
def test_plugin_loading_order(testdir):
@@ -676,7 +687,7 @@ def test_plugin_loading_order(testdir):
676687
"""
677688
def test_terminal_plugin(request):
678689
import myplugin
679-
assert myplugin.terminal_plugin == [True, True]
690+
assert myplugin.terminal_plugin == [False, True]
680691
""",
681692
**{
682693
"myplugin": """

testing/test_warnings.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,7 @@ def test_issue4445_import_plugin(self, testdir, capwarn):
679679
# with stacklevel=2 the warning should originate from
680680
# config.PytestPluginManager.import_plugin is thrown by a skipped plugin
681681

682-
# During config parsing the the pluginargs are checked in a while loop
683-
# that as a result of the argument count runs import_plugin twice, hence
684-
# two identical warnings are captured (is this intentional?).
685-
assert len(capwarn.captured) == 2
682+
assert len(capwarn.captured) == 1
686683
warning, location = capwarn.captured.pop()
687684
file, _, func = location
688685

0 commit comments

Comments
 (0)