[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

@ -85,6 +85,19 @@ class Config(object):
col._config = self col._config = self
return col 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): def addoptions(self, groupname, *specs):
""" add a named group of options to the current testing session. """ add a named group of options to the current testing session.
This function gets invoked during testing session initialization. This function gets invoked during testing session initialization.
@ -163,6 +176,7 @@ class Config(object):
def _reparse(self, args): def _reparse(self, args):
""" this is used from tests that want to re-invoke parse(). """ """ this is used from tests that want to re-invoke parse(). """
#assert args # XXX should not be empty
global config_per_process global config_per_process
oldconfig = py.test.config oldconfig = py.test.config
try: try:

View File

@ -41,58 +41,34 @@ class Conftest(object):
except KeyError: except KeyError:
dp = path.dirpath() dp = path.dirpath()
if dp == path: if dp == path:
return [self.importconfig(defaultconftestpath)] return [importconfig(defaultconftestpath)]
clist = self.getconftestmodules(dp) clist = self.getconftestmodules(dp)
conftestpath = path.join("conftest.py") conftestpath = path.join("conftest.py")
if conftestpath.check(file=1): if conftestpath.check(file=1):
clist.append(self.importconfig(conftestpath)) clist.append(importconfig(conftestpath))
self._path2confmods[path] = clist self._path2confmods[path] = clist
# be defensive: avoid changes from caller side to # be defensive: avoid changes from caller side to
# affect us by always returning a copy of the actual list # affect us by always returning a copy of the actual list
return clist[:] 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): 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 = self.getconftestmodules(path)
modules.reverse() 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: for mod in modules:
try: try:
return getattr(mod, name) return mod, getattr(mod, name)
except AttributeError: except AttributeError:
continue continue
raise KeyError, name raise KeyError, name
def importconfig(self, configpath): def importconfig(configpath):
# We could have used caching here, but it's redundant since # 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 # 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): if not configpath.dirpath('__init__.py').check(file=1):
# HACK: we don't want any "globally" imported conftest.py, # HACK: we don't want any "globally" imported conftest.py,
# prone to conflicts and subtle problems # prone to conflicts and subtle problems

View File

@ -257,6 +257,18 @@ class TestSessionAndOptions:
config = py.test.config._reparse([tmpdir, '--box']) config = py.test.config._reparse([tmpdir, '--box'])
assert config.is_boxed() 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: class TestConfigColitems:
def setup_class(cls): def setup_class(cls):

View File

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