Skip to content

Commit cbc07ab

Browse files
committed
WIP: -p attempts to load setuptools first
1 parent 4cd268d commit cbc07ab

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/_pytest/config/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,22 +521,29 @@ def import_plugin(self, modname):
521521
modname = str(modname)
522522
if self.is_blocked(modname) or self.get_plugin(modname) is not None:
523523
return
524-
if modname in builtin_plugins:
525-
importspec = "_pytest." + modname
526-
else:
527-
importspec = modname
524+
525+
importspec = "_pytest." + modname if modname in builtin_plugins else modname
528526
self.rewrite_hook.mark_rewrite(importspec)
527+
528+
attempt_entry_point = modname not in builtin_plugins
529+
if attempt_entry_point:
530+
infos = len(self.list_plugin_distinfo())
531+
new_infos = self.load_setuptools_entrypoints("pytest11", name=modname)
532+
loaded = new_infos > infos
533+
if loaded:
534+
return
535+
529536
try:
530537
__import__(importspec)
531538
except ImportError as e:
532-
new_exc_type = ImportError
533539
new_exc_message = 'Error importing plugin "%s": %s' % (
534540
modname,
535541
safe_str(e.args[0]),
536542
)
537-
new_exc = new_exc_type(new_exc_message)
543+
new_exc = ImportError(new_exc_message)
544+
tb = sys.exc_info()[2]
538545

539-
six.reraise(new_exc_type, new_exc, sys.exc_info()[2])
546+
six.reraise(ImportError, new_exc, tb)
540547

541548
except Skipped as e:
542549
from _pytest.warnings import _issue_warning_captured

testing/acceptance_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import textwrap
99
import types
1010

11+
import attr
1112
import py
1213
import six
1314

@@ -108,6 +109,60 @@ def test_option(pytestconfig):
108109
assert result.ret == 0
109110
result.stdout.fnmatch_lines(["*1 passed*"])
110111

112+
@pytest.mark.parametrize("load_cov_early", [True, False])
113+
def test_early_load_setuptools_name(self, testdir, monkeypatch, load_cov_early):
114+
pkg_resources = pytest.importorskip("pkg_resources")
115+
116+
testdir.makepyfile(mytestplugin1_module="")
117+
testdir.makepyfile(mytestplugin2_module="")
118+
testdir.makepyfile(mycov_module="")
119+
testdir.syspathinsert()
120+
121+
loaded = []
122+
123+
@attr.s
124+
class DummyEntryPoint(object):
125+
name = attr.ib()
126+
module = attr.ib()
127+
version = "1.0"
128+
129+
@property
130+
def project_name(self):
131+
return self.name
132+
133+
def load(self):
134+
__import__(self.module)
135+
loaded.append(self.name)
136+
return sys.modules[self.module]
137+
138+
@property
139+
def dist(self):
140+
return self
141+
142+
def _get_metadata(self, *args):
143+
return []
144+
145+
entry_points = [
146+
DummyEntryPoint("myplugin1", "mytestplugin1_module"),
147+
DummyEntryPoint("myplugin2", "mytestplugin2_module"),
148+
DummyEntryPoint("mycov", "mycov_module"),
149+
]
150+
151+
def my_iter(group, name=None):
152+
assert group == "pytest11"
153+
for ep in entry_points:
154+
if name is not None and ep.name != name:
155+
continue
156+
yield ep
157+
158+
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
159+
params = ("-p", "mycov") if load_cov_early else ()
160+
testdir.runpytest_inprocess(*params)
161+
if load_cov_early:
162+
assert loaded == ["mycov", "myplugin1", "myplugin2"]
163+
else:
164+
assert loaded == ["myplugin1", "myplugin2", "mycov"]
165+
111166
def test_assertion_magic(self, testdir):
112167
p = testdir.makepyfile(
113168
"""

0 commit comments

Comments
 (0)