Refactor plugin specs handling into an isolated function
This commit is contained in:
parent
043aadeaf2
commit
7cd7c283dd
|
@ -399,30 +399,22 @@ class PytestPluginManager(PluginManager):
|
||||||
self.consider_module(conftestmodule)
|
self.consider_module(conftestmodule)
|
||||||
|
|
||||||
def consider_env(self):
|
def consider_env(self):
|
||||||
specs = os.environ.get("PYTEST_PLUGINS")
|
self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS"))
|
||||||
if specs:
|
|
||||||
plugins = specs.split(',')
|
|
||||||
self._import_plugin_specs(plugins)
|
|
||||||
|
|
||||||
def consider_module(self, mod):
|
def consider_module(self, mod):
|
||||||
plugins = getattr(mod, 'pytest_plugins', [])
|
self._import_plugin_specs(getattr(mod, 'pytest_plugins', []))
|
||||||
if isinstance(plugins, str):
|
|
||||||
plugins = [plugins]
|
|
||||||
self._import_plugin_specs(plugins)
|
|
||||||
|
|
||||||
def _import_plugin_specs(self, spec):
|
def _import_plugin_specs(self, spec):
|
||||||
if spec:
|
plugins = _get_plugin_specs_as_list(spec)
|
||||||
if isinstance(spec, str):
|
for import_spec in plugins:
|
||||||
spec = spec.split(",")
|
self.import_plugin(import_spec)
|
||||||
for import_spec in spec:
|
|
||||||
self.import_plugin(import_spec)
|
|
||||||
|
|
||||||
def import_plugin(self, modname):
|
def import_plugin(self, modname):
|
||||||
# most often modname refers to builtin modules, e.g. "pytester",
|
# most often modname refers to builtin modules, e.g. "pytester",
|
||||||
# "terminal" or "capture". Those plugins are registered under their
|
# "terminal" or "capture". Those plugins are registered under their
|
||||||
# basename for historic purposes but must be imported with the
|
# basename for historic purposes but must be imported with the
|
||||||
# _pytest prefix.
|
# _pytest prefix.
|
||||||
assert isinstance(modname, str)
|
assert isinstance(modname, str), "module name as string required, got %r" % modname
|
||||||
if self.get_plugin(modname) is not None:
|
if self.get_plugin(modname) is not None:
|
||||||
return
|
return
|
||||||
if modname in builtin_plugins:
|
if modname in builtin_plugins:
|
||||||
|
@ -450,6 +442,24 @@ class PytestPluginManager(PluginManager):
|
||||||
self.consider_module(mod)
|
self.consider_module(mod)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_plugin_specs_as_list(specs):
|
||||||
|
"""
|
||||||
|
Parses a list of "plugin specs" and returns a list of plugin names.
|
||||||
|
|
||||||
|
Plugin specs can be given as a list of strings separated by "," or already as a list/tuple in
|
||||||
|
which case it is returned as a list. Specs can also be `None` in which case an
|
||||||
|
empty list is returned.
|
||||||
|
"""
|
||||||
|
if specs is not None:
|
||||||
|
if isinstance(specs, str):
|
||||||
|
specs = specs.split(',') if specs else []
|
||||||
|
if not isinstance(specs, (list, tuple)):
|
||||||
|
raise UsageError("Plugin specs must be a ','-separated string or a "
|
||||||
|
"list/tuple of strings for plugin names. Given: %r" % specs)
|
||||||
|
return list(specs)
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
""" Parser for command line arguments and ini-file values.
|
""" Parser for command line arguments and ini-file values.
|
||||||
|
|
||||||
|
|
|
@ -587,6 +587,21 @@ def test_load_initial_conftest_last_ordering(testdir):
|
||||||
assert [x.function.__module__ for x in l] == expected
|
assert [x.function.__module__ for x in l] == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_plugin_specs_as_list():
|
||||||
|
from _pytest.config import _get_plugin_specs_as_list
|
||||||
|
with pytest.raises(pytest.UsageError):
|
||||||
|
_get_plugin_specs_as_list(set(['foo']))
|
||||||
|
with pytest.raises(pytest.UsageError):
|
||||||
|
_get_plugin_specs_as_list(dict())
|
||||||
|
|
||||||
|
assert _get_plugin_specs_as_list(None) == []
|
||||||
|
assert _get_plugin_specs_as_list('') == []
|
||||||
|
assert _get_plugin_specs_as_list('foo') == ['foo']
|
||||||
|
assert _get_plugin_specs_as_list('foo,bar') == ['foo', 'bar']
|
||||||
|
assert _get_plugin_specs_as_list(['foo', 'bar']) == ['foo', 'bar']
|
||||||
|
assert _get_plugin_specs_as_list(('foo', 'bar')) == ['foo', 'bar']
|
||||||
|
|
||||||
|
|
||||||
class TestWarning:
|
class TestWarning:
|
||||||
def test_warn_config(self, testdir):
|
def test_warn_config(self, testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
|
|
Loading…
Reference in New Issue