Skip to content

Commit d3c9d77

Browse files
committed
WIP: need to change pluggy as well
1 parent a1fcd6e commit d3c9d77

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

src/_pytest/config/__init__.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ def import_plugin(self, modname):
518518
assert isinstance(modname, six.string_types), (
519519
"module name as text required, got %r" % modname
520520
)
521+
from pkg_resources import iter_entry_points, DistributionNotFound
522+
521523
modname = str(modname)
522524
if self.is_blocked(modname) or self.get_plugin(modname) is not None:
523525
return
@@ -526,29 +528,40 @@ def import_plugin(self, modname):
526528
else:
527529
importspec = modname
528530
self.rewrite_hook.mark_rewrite(importspec)
529-
try:
530-
__import__(importspec)
531-
except ImportError as e:
532-
new_exc_type = ImportError
533-
new_exc_message = 'Error importing plugin "%s": %s' % (
534-
modname,
535-
safe_str(e.args[0]),
536-
)
537-
new_exc = new_exc_type(new_exc_message)
531+
mod = None
532+
# TODO: this needs to be moved to pluggy so it can update its _plugin_distinfo attribute
533+
eps = list(iter_entry_points("pytest11", modname))
534+
if eps:
535+
try:
536+
mod = eps[0].load()
537+
except DistributionNotFound:
538+
pass
539+
else:
540+
try:
541+
__import__(importspec)
542+
mod = sys.modules[importspec]
543+
except ImportError as e:
544+
new_exc_message = 'Error importing plugin "%s": %s' % (
545+
modname,
546+
safe_str(e.args[0]),
547+
)
548+
new_exc = ImportError(new_exc_message)
549+
tb = sys.exc_info()[2]
538550

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

541-
except Skipped as e:
542-
from _pytest.warnings import _issue_warning_captured
553+
except Skipped as e:
554+
from _pytest.warnings import _issue_warning_captured
543555

544-
_issue_warning_captured(
545-
PytestWarning("skipped plugin %r: %s" % (modname, e.msg)),
546-
self.hook,
547-
stacklevel=1,
548-
)
549-
else:
550-
mod = sys.modules[importspec]
551-
self.register(mod, modname)
556+
_issue_warning_captured(
557+
PytestWarning("skipped plugin %r: %s" % (modname, e.msg)),
558+
self.hook,
559+
stacklevel=1,
560+
)
561+
return
562+
563+
assert mod is not None
564+
self.register(mod, modname)
552565

553566

554567
def _get_plugin_specs_as_list(specs):

testing/acceptance_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ def test_option(pytestconfig):
108108
assert result.ret == 0
109109
result.stdout.fnmatch_lines(["*1 passed*"])
110110

111+
def test_early_load_setuptools_name(self, testdir, monkeypatch):
112+
# WIP
113+
pkg_resources = pytest.importorskip("pkg_resources")
114+
115+
testdir.makepyfile(mytestplugin_module="")
116+
testdir.syspathinsert()
117+
118+
def my_iter(group, name=None):
119+
if group == "pytest11" and name == EntryPoint.name:
120+
return [EntryPoint()]
121+
return []
122+
123+
class EntryPoint(object):
124+
name = "myplugin"
125+
loaded = False
126+
127+
def load(self):
128+
type(self).loaded = True
129+
__import__("mytestplugin_module")
130+
return sys.modules["mytestplugin_module"]
131+
132+
@property
133+
def dist(self):
134+
return self
135+
136+
def _get_metadata(self, *args):
137+
return []
138+
139+
monkeypatch.setattr(pkg_resources, "iter_entry_points", my_iter)
140+
testdir.runpytest("-p", "myplugin")
141+
assert EntryPoint.loaded
142+
111143
def test_assertion_magic(self, testdir):
112144
p = testdir.makepyfile(
113145
"""

0 commit comments

Comments
 (0)