always import defaultconftest by python import path. strike some redundant code.

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-12-28 17:49:46 +01:00
parent cc82f1601c
commit 27bcd2dbda
2 changed files with 25 additions and 23 deletions

View File

@ -1,19 +1,17 @@
import py import py
defaultconftestpath = py.path.local(__file__).dirpath("defaultconftest.py") from py.impl.test import defaultconftest
class Conftest(object): class Conftest(object):
""" the single place for accessing values and interacting """ the single place for accessing values and interacting
towards conftest modules from py.test objects. towards conftest modules from py.test objects.
(deprecated)
Note that triggering Conftest instances to import Note that triggering Conftest instances to import
conftest.py files may result in added cmdline options. conftest.py files may result in added cmdline options.
XXX
""" """
def __init__(self, path=None, onimport=None): def __init__(self, onimport=None):
self._path2confmods = {} self._path2confmods = {}
self._onimport = onimport self._onimport = onimport
if path is not None:
self.setinitial([path])
def setinitial(self, args): def setinitial(self, args):
""" try to find a first anchor path for looking up global values """ try to find a first anchor path for looking up global values
@ -42,17 +40,13 @@ class Conftest(object):
raise ValueError("missing default conftest.") raise ValueError("missing default conftest.")
dp = path.dirpath() dp = path.dirpath()
if dp == path: if dp == path:
if not defaultconftestpath.check(): # zip file, single-file py.test? clist = [self._postimport(defaultconftest)]
import defaultconftest else:
if self._onimport: clist = self.getconftestmodules(dp)
self._onimport(defaultconftest) conftestpath = path.join("conftest.py")
return [defaultconftest] if conftestpath.check(file=1):
return [self.importconftest(defaultconftestpath)] clist.append(self.importconftest(conftestpath))
clist = self.getconftestmodules(dp) self._path2confmods[path] = clist
conftestpath = path.join("conftest.py")
if conftestpath.check(file=1):
clist.append(self.importconftest(conftestpath))
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[:]
@ -82,6 +76,9 @@ class Conftest(object):
mod = conftestpath.pyimport(modname=modname) mod = conftestpath.pyimport(modname=modname)
else: else:
mod = conftestpath.pyimport() mod = conftestpath.pyimport()
return self._postimport(mod)
def _postimport(self, mod):
if self._onimport: if self._onimport:
self._onimport(mod) self._onimport(mod)
return mod return mod

View File

@ -17,6 +17,11 @@ def pytest_funcarg__basedir(request):
return d return d
return request.cached_setup(lambda: basedirmaker(request), extrakey=request.param) return request.cached_setup(lambda: basedirmaker(request), extrakey=request.param)
def ConftestWithSetinitial(path):
conftest = Conftest()
conftest.setinitial([path])
return conftest
class TestConftestValueAccessGlobal: class TestConftestValueAccessGlobal:
def test_basic_init(self, basedir): def test_basic_init(self, basedir):
conftest = Conftest() conftest = Conftest()
@ -45,22 +50,22 @@ class TestConftestValueAccessGlobal:
def test_default_Module_setting_is_visible_always(self, basedir): def test_default_Module_setting_is_visible_always(self, basedir):
for path in basedir.parts(): for path in basedir.parts():
conftest = Conftest(path) conftest = ConftestWithSetinitial(path)
#assert conftest.lget("Module") == py.test.collect.Module #assert conftest.lget("Module") == py.test.collect.Module
assert conftest.rget("Module") == py.test.collect.Module assert conftest.rget("Module") == py.test.collect.Module
def test_default_has_lower_prio(self, basedir): def test_default_has_lower_prio(self, basedir):
conftest = Conftest(basedir.join("adir")) conftest = ConftestWithSetinitial(basedir.join("adir"))
assert conftest.rget('Directory') == 3 assert conftest.rget('Directory') == 3
#assert conftest.lget('Directory') == py.test.collect.Directory #assert conftest.lget('Directory') == py.test.collect.Directory
def test_value_access_not_existing(self, basedir): def test_value_access_not_existing(self, basedir):
conftest = Conftest(basedir) conftest = ConftestWithSetinitial(basedir)
py.test.raises(KeyError, "conftest.rget('a')") py.test.raises(KeyError, "conftest.rget('a')")
#py.test.raises(KeyError, "conftest.lget('a')") #py.test.raises(KeyError, "conftest.lget('a')")
def test_value_access_by_path(self, basedir): def test_value_access_by_path(self, basedir):
conftest = Conftest(basedir) conftest = ConftestWithSetinitial(basedir)
assert conftest.rget("a", basedir.join('adir')) == 1 assert conftest.rget("a", basedir.join('adir')) == 1
#assert conftest.lget("a", basedir.join('adir')) == 1 #assert conftest.lget("a", basedir.join('adir')) == 1
assert conftest.rget("a", basedir.join('adir', 'b')) == 1.5 assert conftest.rget("a", basedir.join('adir', 'b')) == 1.5
@ -71,12 +76,12 @@ class TestConftestValueAccessGlobal:
#) #)
def test_value_access_with_init_one_conftest(self, basedir): def test_value_access_with_init_one_conftest(self, basedir):
conftest = Conftest(basedir.join('adir')) conftest = ConftestWithSetinitial(basedir.join('adir'))
assert conftest.rget("a") == 1 assert conftest.rget("a") == 1
#assert conftest.lget("a") == 1 #assert conftest.lget("a") == 1
def test_value_access_with_init_two_conftests(self, basedir): def test_value_access_with_init_two_conftests(self, basedir):
conftest = Conftest(basedir.join("adir", "b")) conftest = ConftestWithSetinitial(basedir.join("adir", "b"))
conftest.rget("a") == 1.5 conftest.rget("a") == 1.5
#conftest.lget("a") == 1 #conftest.lget("a") == 1
#conftest.lget("b") == 1 #conftest.lget("b") == 1
@ -84,7 +89,7 @@ class TestConftestValueAccessGlobal:
def test_value_access_with_confmod(self, basedir): def test_value_access_with_confmod(self, basedir):
topdir = basedir.join("adir", "b") topdir = basedir.join("adir", "b")
topdir.ensure("xx", dir=True) topdir.ensure("xx", dir=True)
conftest = Conftest(topdir) conftest = ConftestWithSetinitial(topdir)
mod, value = conftest.rget_with_confmod("a", topdir) mod, value = conftest.rget_with_confmod("a", topdir)
assert value == 1.5 assert value == 1.5
path = py.path.local(mod.__file__) path = py.path.local(mod.__file__)