(experimental) allow cmdline arguments to deep-point to a test, also remove virtually redundant session.getinitialitems() calls

--HG--
branch : trunk
This commit is contained in:
holger krekel
2010-01-10 21:29:36 +01:00
parent 3029aa6558
commit 99301a0dae
13 changed files with 110 additions and 83 deletions

View File

@@ -13,7 +13,8 @@ def main(args=None):
config.parse(args)
config.pluginmanager.do_configure(config)
session = config.initsession()
exitstatus = session.main()
colitems = config.getinitialnodes()
exitstatus = session.main(colitems)
config.pluginmanager.do_unconfigure(config)
raise SystemExit(exitstatus)
except config.Error:

View File

@@ -383,24 +383,10 @@ class RootCollector(Directory):
Directory.__init__(self, config.topdir, parent=None, config=config)
self.name = None
def getfsnode(self, path):
path = py.path.local(path)
if not path.check():
raise self.config.Error("file not found: %s" %(path,))
topdir = self.config.topdir
if path != topdir and not path.relto(topdir):
raise self.config.Error("path %r is not relative to %r" %
(str(path), str(self.fspath)))
# assumtion: pytest's fs-collector tree follows the filesystem tree
basenames = filter(None, path.relto(topdir).split(path.sep))
try:
return self.getbynames(basenames)
except ValueError:
raise self.config.Error("can't collect: %s" % str(path))
def getbynames(self, names):
current = self.consider(self.config.topdir)
for name in names:
while names:
name = names.pop(0)
if name == ".": # special "identity" name
continue
l = []
@@ -409,8 +395,12 @@ class RootCollector(Directory):
l.append(x)
elif x.fspath == current.fspath.join(name):
l.append(x)
elif x.name == "()":
names.insert(0, name)
l.append(x)
break
if not l:
raise ValueError("no node named %r in %r" %(name, current))
raise ValueError("no node named %r below %r" %(name, current))
current = l[0]
return current

View File

@@ -99,7 +99,11 @@ class Config(object):
args.append(py.std.os.getcwd())
self.topdir = gettopdir(args)
self._rootcol = RootCollector(config=self)
self.args = [py.path.local(x) for x in args]
self._setargs(args)
def _setargs(self, args):
self.args = list(args)
self._argfspaths = [py.path.local(decodearg(x)[0]) for x in args]
# config objects are usually pickled across system
# barriers but they contain filesystem paths.
@@ -121,10 +125,10 @@ class Config(object):
self.__init__(topdir=py.path.local())
self._rootcol = RootCollector(config=self)
args, cmdlineopts = repr
args = [self.topdir.join(x) for x in args]
args = [str(self.topdir.join(x)) for x in args]
self.option = cmdlineopts
self._preparse(args)
self.args = args
self._setargs(args)
def ensuretemp(self, string, dir=True):
return self.getbasetemp().ensure(string, dir=dir)
@@ -149,11 +153,26 @@ class Config(object):
return py.path.local.make_numbered_dir(prefix=basename + "-",
keep=0, rootdir=basetemp, lock_timeout=None)
def getcolitems(self):
return [self.getfsnode(arg) for arg in self.args]
def getinitialnodes(self):
return [self.getnode(arg) for arg in self.args]
def getfsnode(self, path):
return self._rootcol.getfsnode(path)
def getnode(self, arg):
parts = decodearg(arg)
path = py.path.local(parts.pop(0))
if not path.check():
raise self.Error("file not found: %s" %(path,))
topdir = self.topdir
if path != topdir and not path.relto(topdir):
raise self.Error("path %r is not relative to %r" %
(str(path), str(topdir)))
# assumtion: pytest's fs-collector tree follows the filesystem tree
names = filter(None, path.relto(topdir).split(path.sep))
names.extend(parts)
try:
return self._rootcol.getbynames(names)
except ValueError:
e = py.std.sys.exc_info()[1]
raise self.Error("can't collect: %s\n%s" % (arg, e.args[0]))
def _getcollectclass(self, name, path):
try:
@@ -282,11 +301,11 @@ def gettopdir(args):
if the common base dir resides in a python package
parent directory of the root package is returned.
"""
args = [py.path.local(arg) for arg in args]
p = args and args[0] or None
for x in args[1:]:
fsargs = [py.path.local(decodearg(arg)[0]) for arg in args]
p = fsargs and fsargs[0] or None
for x in fsargs[1:]:
p = p.common(x)
assert p, "cannot determine common basedir of %s" %(args,)
assert p, "cannot determine common basedir of %s" %(fsargs,)
pkgdir = p.pypkgpath()
if pkgdir is None:
if p.check(file=1):
@@ -295,6 +314,10 @@ def gettopdir(args):
else:
return pkgdir.dirpath()
def decodearg(arg):
arg = str(arg)
return arg.split("::")
def onpytestaccess():
# it's enough to have our containing module loaded as
# it initializes a per-process config instance

View File

@@ -85,8 +85,7 @@ class DSession(Session):
# raise config.Error("dist mode %r needs test execution environments, "
# "none found." %(config.option.dist))
def main(self, colitems=None):
colitems = self.getinitialitems(colitems)
def main(self, colitems):
self.sessionstarts()
self.setup()
exitstatus = self.loop(colitems)

View File

@@ -142,7 +142,7 @@ def slave_runsession(channel, config, fullwidth, hasmarkup):
continue
colitems.append(colitem)
else:
colitems = None
colitems = config.getinitialnodes()
session.shouldclose = channel.isclosed
class Failures(list):

View File

@@ -93,15 +93,8 @@ class Session(object):
exitstatus=exitstatus,
)
def getinitialitems(self, colitems):
if colitems is None:
colitems = [self.config.getfsnode(arg)
for arg in self.config.args]
return colitems
def main(self, colitems=None):
def main(self, colitems):
""" main loop for running tests. """
colitems = self.getinitialitems(colitems)
self.shouldstop = False
self.sessionstarts()
exitstatus = outcome.EXIT_OK

View File

@@ -26,7 +26,7 @@ def pytest_collect_file(path, parent):
ext = path.ext
pb = path.purebasename
if pb.startswith("test_") or pb.endswith("_test") or \
path in parent.config.args:
path in parent.config._argfspaths:
if ext == ".py":
return parent.Module(path, parent=parent)
@@ -41,7 +41,7 @@ def pytest_collect_directory(path, parent):
# define Directory(dir) already
if not parent.recfilter(path): # by default special ".cvs", ...
# check if cmdline specified this dir or a subdir directly
for arg in parent.config.args:
for arg in parent.config._argfspaths:
if path == arg or arg.relto(path):
break
else:

View File

@@ -158,7 +158,7 @@ class TmpTestdir:
config = self.parseconfig(*args)
session = config.initsession()
rec = self.getreportrecorder(config)
colitems = [config.getfsnode(arg) for arg in config.args]
colitems = [config.getnode(arg) for arg in config.args]
items = list(session.genitems(colitems))
return items, rec
@@ -190,7 +190,8 @@ class TmpTestdir:
config.pluginmanager.do_configure(config)
session = config.initsession()
reprec = self.getreportrecorder(config)
session.main()
colitems = config.getinitialnodes()
session.main(colitems)
config.pluginmanager.do_unconfigure(config)
return reprec
@@ -251,7 +252,7 @@ class TmpTestdir:
def getfscol(self, path, configargs=()):
self.config = self.parseconfig(path, *configargs)
self.session = self.config.initsession()
return self.config.getfsnode(path)
return self.config.getnode(path)
def getmodulecol(self, source, configargs=(), withinit=False):
kw = {self.request.function.__name__: py.code.Source(source).strip()}
@@ -266,7 +267,7 @@ class TmpTestdir:
plugin = self.config.pluginmanager.getplugin("runner")
plugin.pytest_configure(config=self.config)
return self.config.getfsnode(path)
return self.config.getnode(path)
def popen(self, cmdargs, stdout, stderr, **kw):
if not hasattr(py.std, 'subprocess'):