[svn r37898] provide a high-level helper for getting

at a pathlist specified in a conftest (and
the paths can be relative to the conftest.py file they
are contained in)

--HG--
branch : trunk
This commit is contained in:
hpk 2007-02-04 13:29:26 +01:00
parent 95f38694cb
commit 41526ad622
4 changed files with 51 additions and 49 deletions

View File

@ -84,6 +84,19 @@ class Config(object):
col = self.conftest.rget("Directory", pkgpath)(pkgpath)
col._config = self
return col
def getvalue_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
objects (they can be relative to the location
where they were found).
"""
try:
mod, relroots = self.conftest.rget_with_confmod(name, path)
except KeyError:
return None
modpath = py.path.local(mod.__file__).dirpath()
return [modpath.join(x, abs=True) for x in relroots]
def addoptions(self, groupname, *specs):
""" add a named group of options to the current testing session.
@ -163,6 +176,7 @@ class Config(object):
def _reparse(self, args):
""" this is used from tests that want to re-invoke parse(). """
#assert args # XXX should not be empty
global config_per_process
oldconfig = py.test.config
try:

View File

@ -41,63 +41,39 @@ class Conftest(object):
except KeyError:
dp = path.dirpath()
if dp == path:
return [self.importconfig(defaultconftestpath)]
return [importconfig(defaultconftestpath)]
clist = self.getconftestmodules(dp)
conftestpath = path.join("conftest.py")
if conftestpath.check(file=1):
clist.append(self.importconfig(conftestpath))
clist.append(importconfig(conftestpath))
self._path2confmods[path] = clist
# be defensive: avoid changes from caller side to
# affect us by always returning a copy of the actual list
return clist[:]
def getconftest(self, path):
""" Return a direct module of that path
"""
if isinstance(path, str):
path = py.path.local(path)
try:
conftestmod = self.getconftestmodules(path)[-1]
if py.path.local(conftestmod.__file__).dirpath() != path:
raise AttributeError
return conftestmod
except KeyError:
raise AttributeError
# we raise here AttributeError to unify error reporting in case
# of lack of variable in conftest or lack of file, but we do not want to
# hide ImportError
# XXX no real use case, may probably go
#def lget(self, name, path=None):
# modules = self.getconftestmodules(path)
# return self._get(name, modules)
def rget(self, name, path=None):
mod, value = self.rget_with_confmod(name, path)
return value
def rget_with_confmod(self, name, path=None):
modules = self.getconftestmodules(path)
modules.reverse()
return self._get(name, modules)
def rget_path(self, name, path):
""" can raise AttributeError
"""
return getattr(self.getconftest(path), name)
def _get(self, name, modules):
for mod in modules:
try:
return getattr(mod, name)
return mod, getattr(mod, name)
except AttributeError:
continue
raise KeyError, name
def importconfig(self, configpath):
# We could have used caching here, but it's redundant since
# they're cached on path anyway, so we use it only when doing rget_path
if not configpath.dirpath('__init__.py').check(file=1):
# HACK: we don't want any "globally" imported conftest.py,
# prone to conflicts and subtle problems
modname = str(configpath).replace('.', configpath.sep)
mod = configpath.pyimport(modname=modname)
else:
mod = configpath.pyimport()
return mod
def importconfig(configpath):
# We could have used caching here, but it's redundant since
# they're cached on path anyway, so we use it only when doing rget_path
assert configpath.check(), configpath
if not configpath.dirpath('__init__.py').check(file=1):
# HACK: we don't want any "globally" imported conftest.py,
# prone to conflicts and subtle problems
modname = str(configpath).replace('.', configpath.sep)
mod = configpath.pyimport(modname=modname)
else:
mod = configpath.pyimport()
return mod

View File

@ -256,7 +256,19 @@ class TestSessionAndOptions:
assert not config.is_boxed()
config = py.test.config._reparse([tmpdir, '--box'])
assert config.is_boxed()
def test_getvalue_pathlist(self):
tmpdir = self.tmpdir
somepath = tmpdir.join("x", "y", "z")
p = tmpdir.join("conftest.py")
p.write("pathlist = ['.', %r]" % str(somepath))
config = py.test.config._reparse([p])
assert config.getvalue_pathlist('notexist') is None
pl = config.getvalue_pathlist('pathlist')
print pl
assert len(pl) == 2
assert pl[0] == tmpdir
assert pl[1] == somepath
class TestConfigColitems:
def setup_class(cls):

View File

@ -65,14 +65,14 @@ class TestConftestValueAccessGlobal:
#conftest.lget("a") == 1
#conftest.lget("b") == 1
def test_value_access_path(self):
def test_value_access_with_confmod(self):
topdir = self.basedir.join("adir", "b")
topdir.ensure("xx", dir=True)
conftest = Conftest(topdir)
assert conftest.rget_path("a", topdir) == 1.5
assert conftest.rget_path("a", topdir.dirpath()) == 1
py.test.raises(AttributeError, "conftest.rget_path('a', topdir.join('xx'))")
#assert py.path.local(mod.__file__).dirpath() == topdir
mod, value = conftest.rget_with_confmod("a", topdir)
assert value == 1.5
path = py.path.local(mod.__file__)
assert path == self.basedir.join("adir", "b", "conftest.py")
class TestConftestValueAccessInPackage(TestConftestValueAccessGlobal):
def setup_class(cls):