diff --git a/py/test/config.py b/py/test/config.py index e53ea3495..9b869e6a7 100644 --- a/py/test/config.py +++ b/py/test/config.py @@ -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: diff --git a/py/test/conftesthandle.py b/py/test/conftesthandle.py index d62421c45..35a4aa149 100644 --- a/py/test/conftesthandle.py +++ b/py/test/conftesthandle.py @@ -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 diff --git a/py/test/testing/test_config.py b/py/test/testing/test_config.py index 62acc7cb0..8ea115780 100644 --- a/py/test/testing/test_config.py +++ b/py/test/testing/test_config.py @@ -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): diff --git a/py/test/testing/test_conftesthandle.py b/py/test/testing/test_conftesthandle.py index c94a2ccbd..69ed7f407 100644 --- a/py/test/testing/test_conftesthandle.py +++ b/py/test/testing/test_conftesthandle.py @@ -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):