2008-08-16 23:26:59 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
class Conftest(object):
|
|
|
|
""" the single place for accessing values and interacting
|
|
|
|
towards conftest modules from py.test objects.
|
|
|
|
|
2009-12-29 00:49:46 +08:00
|
|
|
(deprecated)
|
2008-08-16 23:26:59 +08:00
|
|
|
Note that triggering Conftest instances to import
|
|
|
|
conftest.py files may result in added cmdline options.
|
|
|
|
"""
|
2009-12-31 22:10:32 +08:00
|
|
|
def __init__(self, onimport=None, confcutdir=None):
|
2008-08-16 23:26:59 +08:00
|
|
|
self._path2confmods = {}
|
2009-02-27 18:18:27 +08:00
|
|
|
self._onimport = onimport
|
2009-12-30 09:36:58 +08:00
|
|
|
self._conftestpath2mod = {}
|
2009-12-31 22:10:32 +08:00
|
|
|
self._confcutdir = confcutdir
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def setinitial(self, args):
|
|
|
|
""" try to find a first anchor path for looking up global values
|
|
|
|
from conftests. This function is usually called _before_
|
|
|
|
argument parsing. conftest files may add command line options
|
|
|
|
and we thus have no completely safe way of determining
|
|
|
|
which parts of the arguments are actually related to options
|
|
|
|
and which are file system paths. We just try here to get
|
|
|
|
bootstrapped ...
|
|
|
|
"""
|
|
|
|
current = py.path.local()
|
2009-12-31 22:10:32 +08:00
|
|
|
opt = '--confcutdir'
|
|
|
|
for i in range(len(args)):
|
|
|
|
opt1 = str(args[i])
|
|
|
|
if opt1.startswith(opt):
|
|
|
|
if opt1 == opt:
|
|
|
|
if len(args) > i:
|
|
|
|
p = current.join(args[i+1], abs=True)
|
|
|
|
elif opt1.startswith(opt + "="):
|
|
|
|
p = current.join(opt1[len(opt)+1:], abs=1)
|
|
|
|
self._confcutdir = p
|
|
|
|
break
|
2008-08-16 23:26:59 +08:00
|
|
|
for arg in args + [current]:
|
|
|
|
anchor = current.join(arg, abs=1)
|
|
|
|
if anchor.check(): # we found some file object
|
|
|
|
self._path2confmods[None] = self.getconftestmodules(anchor)
|
2010-02-04 19:26:53 +08:00
|
|
|
# let's also consider test* dirs
|
|
|
|
if anchor.check(dir=1):
|
2010-02-07 05:37:04 +08:00
|
|
|
for x in anchor.listdir(lambda x: x.check(dir=1, dotfile=0)):
|
2010-02-04 19:26:53 +08:00
|
|
|
self.getconftestmodules(x)
|
2008-08-16 23:26:59 +08:00
|
|
|
break
|
2009-03-01 21:16:14 +08:00
|
|
|
else:
|
|
|
|
assert 0, "no root of filesystem?"
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def getconftestmodules(self, path):
|
|
|
|
""" return a list of imported conftest modules for the given path. """
|
|
|
|
try:
|
|
|
|
clist = self._path2confmods[path]
|
|
|
|
except KeyError:
|
2009-03-01 21:16:14 +08:00
|
|
|
if path is None:
|
2009-12-31 22:10:32 +08:00
|
|
|
raise ValueError("missing default confest.")
|
2008-08-16 23:26:59 +08:00
|
|
|
dp = path.dirpath()
|
2009-12-28 06:03:04 +08:00
|
|
|
if dp == path:
|
2009-12-31 22:10:32 +08:00
|
|
|
clist = []
|
2009-12-29 00:49:46 +08:00
|
|
|
else:
|
2009-12-31 22:10:32 +08:00
|
|
|
cutdir = self._confcutdir
|
2009-12-29 00:49:46 +08:00
|
|
|
clist = self.getconftestmodules(dp)
|
2009-12-31 22:10:32 +08:00
|
|
|
if cutdir and path != cutdir and not path.relto(cutdir):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
conftestpath = path.join("conftest.py")
|
|
|
|
if conftestpath.check(file=1):
|
|
|
|
clist.append(self.importconftest(conftestpath))
|
|
|
|
self._path2confmods[path] = clist
|
2008-08-16 23:26:59 +08:00
|
|
|
# be defensive: avoid changes from caller side to
|
|
|
|
# affect us by always returning a copy of the actual list
|
|
|
|
return clist[:]
|
|
|
|
|
|
|
|
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()
|
|
|
|
for mod in modules:
|
|
|
|
try:
|
|
|
|
return mod, getattr(mod, name)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2009-08-29 01:16:15 +08:00
|
|
|
raise KeyError(name)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def importconftest(self, conftestpath):
|
|
|
|
assert conftestpath.check(), conftestpath
|
2009-12-30 09:36:58 +08:00
|
|
|
try:
|
|
|
|
return self._conftestpath2mod[conftestpath]
|
|
|
|
except KeyError:
|
|
|
|
if not conftestpath.dirpath('__init__.py').check(file=1):
|
|
|
|
# HACK: we don't want any "globally" imported conftest.py,
|
|
|
|
# prone to conflicts and subtle problems
|
|
|
|
modname = str(conftestpath).replace('.', conftestpath.sep)
|
|
|
|
mod = conftestpath.pyimport(modname=modname)
|
|
|
|
else:
|
|
|
|
mod = conftestpath.pyimport()
|
|
|
|
self._conftestpath2mod[conftestpath] = mod
|
2009-12-31 22:10:32 +08:00
|
|
|
dirpath = conftestpath.dirpath()
|
|
|
|
if dirpath in self._path2confmods:
|
|
|
|
for path, mods in self._path2confmods.items():
|
|
|
|
if path and path.relto(dirpath) or path == dirpath:
|
|
|
|
assert mod not in mods
|
|
|
|
mods.append(mod)
|
|
|
|
self._postimport(mod)
|
2009-12-30 09:36:58 +08:00
|
|
|
return mod
|
2009-12-29 00:49:46 +08:00
|
|
|
|
|
|
|
def _postimport(self, mod):
|
2009-02-27 18:18:27 +08:00
|
|
|
if self._onimport:
|
|
|
|
self._onimport(mod)
|
|
|
|
return mod
|