remove defaultconfest.py and make PluginManager directly do early initialization of default plugins.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-12-29 10:26:51 +01:00
parent 27bcd2dbda
commit 0361b73d75
10 changed files with 38 additions and 42 deletions

View File

@ -8,7 +8,7 @@ import py
def configproperty(name):
def fget(self):
#print "retrieving %r property from %s" %(name, self.fspath)
return self.config.getvalue(name, self.fspath)
return self.config._getcollectclass(name, self.fspath)
return property(fget)
class Node(object):

View File

@ -107,6 +107,7 @@ class Config(object):
# warning global side effects:
# * registering to py lib plugins
# * setting py.test.config
py._com.comregistry = py._com.Registry()
self.__init__(
pluginmanager=py.test._PluginManager(py._com.comregistry),
topdir=py.path.local(),
@ -155,11 +156,17 @@ class Config(object):
pkgpath = path.pypkgpath()
if pkgpath is None:
pkgpath = path.check(file=1) and path.dirpath() or path
Dir = self._conftest.rget("Directory", pkgpath)
Dir = self._getcollectclass("Directory", pkgpath)
col = Dir(pkgpath)
col.config = self
return col._getfsnode(path)
def _getcollectclass(self, name, path):
try:
return self.getvalue(name, path)
except KeyError:
return getattr(py.test.collect, name)
def getconftest_pathlist(self, name, path=None):
""" return a matching value, which needs to be sequence
of filenames that will be returned as a list of Path

View File

@ -1,5 +1,4 @@
import py
from py.impl.test import defaultconftest
class Conftest(object):
""" the single place for accessing values and interacting
@ -40,7 +39,7 @@ class Conftest(object):
raise ValueError("missing default conftest.")
dp = path.dirpath()
if dp == path:
clist = [self._postimport(defaultconftest)]
clist = self._path2confmods[path] = []
else:
clist = self.getconftestmodules(dp)
conftestpath = path.join("conftest.py")

View File

@ -1,14 +0,0 @@
import py
Module = py.test.collect.Module
Directory = py.test.collect.Directory
File = py.test.collect.File
# python collectors
Class = py.test.collect.Class
Generator = py.test.collect.Generator
Function = py.test.collect.Function
Instance = py.test.collect.Instance
pytest_plugins = "default runner capture terminal mark skipping tmpdir monkeypatch recwarn pdb pastebin unittest helpconfig nose assertion".split()

View File

@ -5,6 +5,10 @@ import py
from py.plugin import hookspec
from py.impl.test.outcome import Skipped
default_plugins = (
"default runner capture terminal mark skipping tmpdir monkeypatch "
"recwarn pdb pastebin unittest helpconfig nose assertion").split()
def check_old_use(mod, modname):
clsname = modname[len('pytest_'):].capitalize() + "Plugin"
assert not hasattr(mod, clsname), (mod, clsname)
@ -21,6 +25,9 @@ class PluginManager(object):
self.hook = py._com.HookRelay(
hookspecs=hookspec,
registry=self.comregistry)
self.register(self)
for spec in default_plugins:
self.import_plugin(spec)
def _getpluginname(self, plugin, name):
if name is None:
@ -31,7 +38,8 @@ class PluginManager(object):
return name
def register(self, plugin, name=None):
assert not self.isregistered(plugin)
assert not self.isregistered(plugin), plugin
assert not self.comregistry.isregistered(plugin), plugin
name = self._getpluginname(plugin, name)
if name in self._name2plugin:
return False
@ -191,31 +199,24 @@ class PluginManager(object):
mc.execute()
def pytest_plugin_registered(self, plugin):
dic = self.call_plugin(plugin, "pytest_namespace", {}) or {}
for name, value in dic.items():
setattr(py.test, name, value)
if hasattr(self, '_config'):
self.call_plugin(plugin, "pytest_addoption",
{'parser': self._config._parser})
self.call_plugin(plugin, "pytest_configure",
{'config': self._config})
#dic = self.call_plugin(plugin, "pytest_namespace")
#self._updateext(dic)
def call_plugin(self, plugin, methname, kwargs):
return py._com.MultiCall(
methods=self.listattr(methname, plugins=[plugin]),
kwargs=kwargs, firstresult=True).execute()
def _updateext(self, dic):
if dic:
for name, value in dic.items():
setattr(py.test, name, value)
def do_configure(self, config):
assert not hasattr(self, '_config')
config.pluginmanager.register(self)
self._config = config
config.hook.pytest_configure(config=self._config)
for dic in config.hook.pytest_namespace() or []:
self._updateext(dic)
def do_unconfigure(self, config):
config = self._config

View File

@ -40,7 +40,7 @@ def pytest_collect_directory(path, parent):
break
else:
return
Directory = parent.config.getvalue('Directory', path)
Directory = parent.config._getcollectclass('Directory', path)
return Directory(path, parent=parent)
def pytest_report_iteminfo(item):

View File

@ -3,7 +3,7 @@ import py
pytest_plugins = "pytester"
import py.plugin
plugindir = py.path.local(py.plugin.__file__).dirpath()
from py.impl.test.defaultconftest import pytest_plugins as default_plugins
from py.impl.test.pluginmanager import default_plugins
def pytest_collect_file(path, parent):
if path.basename.startswith("pytest_") and path.ext == ".py":

View File

@ -32,10 +32,10 @@ class TestConftestValueAccessGlobal:
l = []
conftest = Conftest(onimport=l.append)
conftest.setinitial([basedir.join("adir")])
assert len(l) == 2 # default + the one
assert len(l) == 1
assert conftest.rget("a") == 1
assert conftest.rget("b", basedir.join("adir", "b")) == 2
assert len(l) == 3
assert len(l) == 2
def test_immediate_initialiation_and_incremental_are_the_same(self, basedir):
conftest = Conftest()
@ -48,11 +48,11 @@ class TestConftestValueAccessGlobal:
conftest.getconftestmodules(basedir.join('b'))
assert len(conftest._path2confmods) == snap1 + 2
def test_default_Module_setting_is_visible_always(self, basedir):
for path in basedir.parts():
conftest = ConftestWithSetinitial(path)
#assert conftest.lget("Module") == py.test.collect.Module
assert conftest.rget("Module") == py.test.collect.Module
def test_default_Module_setting_is_visible_always(self, basedir, testdir):
basedir.copy(testdir.tmpdir)
config = testdir.Config()
colclass = config._getcollectclass("Module", testdir.tmpdir)
assert colclass == py.test.collect.Module
def test_default_has_lower_prio(self, basedir):
conftest = ConftestWithSetinitial(basedir.join("adir"))

View File

@ -184,7 +184,7 @@ class TestConfigPickling:
def test_config__setstate__wired_correctly_in_childprocess(testdir):
execnet = py.test.importorskip("execnet")
from py.impl.test.dist.mypickle import PickleChannel
gw = execnet.PopenGateway()
gw = execnet.makegateway()
channel = gw.remote_exec("""
import py
from py.impl.test.dist.mypickle import PickleChannel

View File

@ -148,7 +148,9 @@ class TestBootstrapping:
assert pp.isregistered(a1)
pp.register(a2, "hello")
assert pp.isregistered(a2)
assert pp.getplugins() == [a1, a2]
l = pp.getplugins()
assert a1 in l
assert a2 in l
assert pp.getplugin('hello') == a2
pp.unregister(a1)
assert not pp.isregistered(a1)
@ -160,13 +162,14 @@ class TestBootstrapping:
mod = py.std.types.ModuleType("x.y.pytest_hello")
pp.register(mod)
assert pp.isregistered(mod)
assert pp.getplugins() == [mod]
l = pp.getplugins()
assert mod in l
py.test.raises(AssertionError, "pp.register(mod)")
mod2 = py.std.types.ModuleType("pytest_hello")
#pp.register(mod2) # double registry
py.test.raises(AssertionError, "pp.register(mod)")
#assert not pp.isregistered(mod2)
assert pp.getplugins() == [mod] # does not actually modify plugins
assert pp.getplugins() == l
def test_canonical_import(self, monkeypatch):
mod = py.std.types.ModuleType("pytest_xyz")