2010-11-07 17:19:58 +08:00
|
|
|
""" core implementation of testing process: init, session, runtest loop. """
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
import py
|
2010-11-13 18:10:45 +08:00
|
|
|
import pytest, _pytest
|
2011-09-01 22:19:16 +08:00
|
|
|
import os, sys, imp
|
2010-11-13 18:10:45 +08:00
|
|
|
tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
# exitcodes for the command line
|
|
|
|
EXIT_OK = 0
|
|
|
|
EXIT_TESTSFAILED = 1
|
|
|
|
EXIT_INTERRUPTED = 2
|
|
|
|
EXIT_INTERNALERROR = 3
|
2012-05-18 05:11:23 +08:00
|
|
|
EXIT_USAGEERROR = 4
|
2010-11-01 07:27:12 +08:00
|
|
|
|
2011-10-27 04:40:08 +08:00
|
|
|
name_re = py.std.re.compile("^[a-zA-Z_]\w*$")
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-11-05 06:21:26 +08:00
|
|
|
parser.addini("norecursedirs", "directory patterns to avoid for recursion",
|
|
|
|
type="args", default=('.*', 'CVS', '_darcs', '{arch}'))
|
2010-11-06 16:58:04 +08:00
|
|
|
#parser.addini("dirpatterns",
|
|
|
|
# "patterns specifying possible locations of test files",
|
|
|
|
# type="linelist", default=["**/test_*.txt",
|
|
|
|
# "**/test_*.py", "**/*_test.py"]
|
|
|
|
#)
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("general", "running and selection options")
|
|
|
|
group._addoption('-x', '--exitfirst', action="store_true", default=False,
|
|
|
|
dest="exitfirst",
|
|
|
|
help="exit instantly on first error or failed test."),
|
|
|
|
group._addoption('--maxfail', metavar="num",
|
|
|
|
action="store", type="int", dest="maxfail", default=0,
|
|
|
|
help="exit after first num failures or errors.")
|
2011-11-12 06:56:11 +08:00
|
|
|
group._addoption('--strict', action="store_true",
|
|
|
|
help="run pytest in strict mode, warnings become errors.")
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("collect", "collection")
|
|
|
|
group.addoption('--collectonly',
|
|
|
|
action="store_true", dest="collectonly",
|
|
|
|
help="only collect tests, don't execute them."),
|
2010-11-07 05:17:33 +08:00
|
|
|
group.addoption('--pyargs', action="store_true",
|
|
|
|
help="try to interpret all arguments as python packages.")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption("--ignore", action="append", metavar="path",
|
|
|
|
help="ignore path during collection (multi-allowed).")
|
|
|
|
group.addoption('--confcutdir', dest="confcutdir", default=None,
|
|
|
|
metavar="dir",
|
|
|
|
help="only load conftest.py's relative to specified dir.")
|
|
|
|
|
|
|
|
group = parser.getgroup("debugconfig",
|
2010-12-06 23:54:42 +08:00
|
|
|
"test session debugging and configuration")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
|
|
|
|
help="base temporary directory for this test run.")
|
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def pytest_namespace():
|
2011-05-27 07:58:31 +08:00
|
|
|
collect = dict(Item=Item, Collector=Collector, File=File, Session=Session)
|
|
|
|
return dict(collect=collect)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_configure(config):
|
2010-10-12 21:34:32 +08:00
|
|
|
py.test.config = config # compatibiltiy
|
2010-11-06 16:58:04 +08:00
|
|
|
if config.option.exitfirst:
|
2010-10-10 19:48:48 +08:00
|
|
|
config.option.maxfail = 1
|
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def wrap_session(config, doit):
|
|
|
|
"""Skeleton command line program"""
|
2010-11-01 07:27:12 +08:00
|
|
|
session = Session(config)
|
|
|
|
session.exitstatus = EXIT_OK
|
2011-06-01 20:54:34 +08:00
|
|
|
initstate = 0
|
2010-11-01 07:27:12 +08:00
|
|
|
try:
|
2012-05-18 05:11:23 +08:00
|
|
|
try:
|
|
|
|
config.pluginmanager.do_configure(config)
|
|
|
|
initstate = 1
|
|
|
|
config.hook.pytest_sessionstart(session=session)
|
|
|
|
initstate = 2
|
|
|
|
doit(config, session)
|
|
|
|
except pytest.UsageError:
|
|
|
|
msg = sys.exc_info()[1].args[0]
|
|
|
|
sys.stderr.write("ERROR: %s\n" %(msg,))
|
|
|
|
session.exitstatus = EXIT_USAGEERROR
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
|
|
|
|
session.exitstatus = EXIT_INTERRUPTED
|
|
|
|
except:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
config.pluginmanager.notify_exception(excinfo, config.option)
|
|
|
|
session.exitstatus = EXIT_INTERNALERROR
|
|
|
|
if excinfo.errisinstance(SystemExit):
|
|
|
|
sys.stderr.write("mainloop: caught Spurious SystemExit!\n")
|
|
|
|
finally:
|
|
|
|
if initstate >= 2:
|
|
|
|
config.hook.pytest_sessionfinish(session=session,
|
|
|
|
exitstatus=session.exitstatus or (session._testsfailed and 1))
|
|
|
|
if not session.exitstatus and session._testsfailed:
|
|
|
|
session.exitstatus = EXIT_TESTSFAILED
|
|
|
|
if initstate >= 1:
|
|
|
|
config.pluginmanager.do_unconfigure(config)
|
2010-11-01 07:27:12 +08:00
|
|
|
return session.exitstatus
|
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
return wrap_session(config, _main)
|
|
|
|
|
|
|
|
def _main(config, session):
|
|
|
|
""" default command line protocol for initialization, session,
|
|
|
|
running tests and reporting. """
|
|
|
|
config.hook.pytest_collection(session=session)
|
|
|
|
config.hook.pytest_runtestloop(session=session)
|
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_collection(session):
|
2011-05-27 08:53:47 +08:00
|
|
|
return session.perform_collect()
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_runtestloop(session):
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.config.option.collectonly:
|
|
|
|
return True
|
2011-12-03 05:00:19 +08:00
|
|
|
for i, item in enumerate(session.items):
|
|
|
|
try:
|
|
|
|
nextitem = session.items[i+1]
|
|
|
|
except IndexError:
|
|
|
|
nextitem = None
|
|
|
|
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.shouldstop:
|
|
|
|
raise session.Interrupted(session.shouldstop)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def pytest_ignore_collect(path, config):
|
|
|
|
p = path.dirpath()
|
2010-11-01 00:41:58 +08:00
|
|
|
ignore_paths = config._getconftest_pathlist("collect_ignore", path=p)
|
2010-10-10 19:48:48 +08:00
|
|
|
ignore_paths = ignore_paths or []
|
|
|
|
excludeopt = config.getvalue("ignore")
|
|
|
|
if excludeopt:
|
|
|
|
ignore_paths.extend([py.path.local(x) for x in excludeopt])
|
|
|
|
return path in ignore_paths
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class HookProxy:
|
2010-11-06 16:58:04 +08:00
|
|
|
def __init__(self, fspath, config):
|
|
|
|
self.fspath = fspath
|
|
|
|
self.config = config
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def __getattr__(self, name):
|
2010-11-06 16:58:04 +08:00
|
|
|
hookmethod = getattr(self.config.hook, name)
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def call_matching_hooks(**kwargs):
|
2010-11-06 16:58:04 +08:00
|
|
|
plugins = self.config._getmatchingplugins(self.fspath)
|
2010-10-12 18:19:53 +08:00
|
|
|
return hookmethod.pcall(plugins, **kwargs)
|
|
|
|
return call_matching_hooks
|
|
|
|
|
2010-10-26 05:09:21 +08:00
|
|
|
def compatproperty(name):
|
|
|
|
def fget(self):
|
2010-11-13 16:05:11 +08:00
|
|
|
return getattr(pytest, name)
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2010-11-13 18:10:45 +08:00
|
|
|
return property(fget, None, None,
|
2012-06-21 17:20:29 +08:00
|
|
|
"deprecated attribute %r, use pytest.%s" % (name, name))
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class Node(object):
|
|
|
|
""" base class for all Nodes in the collection tree.
|
|
|
|
Collector subclasses have children, Items are terminal nodes."""
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
def __init__(self, name, parent=None, config=None, session=None):
|
2010-10-12 18:19:53 +08:00
|
|
|
#: a unique name with the scope of the parent
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
#: the parent collector node.
|
|
|
|
self.parent = parent
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
#: the test config object
|
|
|
|
self.config = config or parent.config
|
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
#: the collection this node is part of
|
2010-11-07 17:19:58 +08:00
|
|
|
self.session = session or parent.session
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-11-06 18:38:53 +08:00
|
|
|
#: filesystem path where this node was collected from
|
2010-10-12 18:19:53 +08:00
|
|
|
self.fspath = getattr(parent, 'fspath', None)
|
2010-11-07 17:19:58 +08:00
|
|
|
self.ihook = self.session.gethookproxy(self.fspath)
|
2010-10-26 05:08:56 +08:00
|
|
|
self.keywords = {self.name: True}
|
2010-10-12 18:19:53 +08:00
|
|
|
|
2010-10-26 05:09:21 +08:00
|
|
|
Module = compatproperty("Module")
|
|
|
|
Class = compatproperty("Class")
|
2011-01-18 19:47:31 +08:00
|
|
|
Instance = compatproperty("Instance")
|
2010-10-26 05:09:21 +08:00
|
|
|
Function = compatproperty("Function")
|
|
|
|
File = compatproperty("File")
|
|
|
|
Item = compatproperty("Item")
|
|
|
|
|
2011-03-05 21:16:27 +08:00
|
|
|
def _getcustomclass(self, name):
|
|
|
|
cls = getattr(self, name)
|
|
|
|
if cls != getattr(pytest, name):
|
|
|
|
py.log._apiwarn("2.0", "use of node.%s is deprecated, "
|
|
|
|
"use pytest_pycollect_makeitem(...) to create custom "
|
|
|
|
"collection nodes" % name)
|
|
|
|
return cls
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def __repr__(self):
|
2012-06-21 17:20:29 +08:00
|
|
|
return "<%s %r>" %(self.__class__.__name__,
|
|
|
|
getattr(self, 'name', None))
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
# methods for ordering nodes
|
2010-11-06 16:58:04 +08:00
|
|
|
@property
|
|
|
|
def nodeid(self):
|
|
|
|
try:
|
|
|
|
return self._nodeid
|
|
|
|
except AttributeError:
|
|
|
|
self._nodeid = x = self._makeid()
|
|
|
|
return x
|
|
|
|
|
|
|
|
def _makeid(self):
|
|
|
|
return self.parent.nodeid + "::" + self.name
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if not isinstance(other, Node):
|
|
|
|
return False
|
2012-06-21 17:20:29 +08:00
|
|
|
return (self.__class__ == other.__class__ and
|
|
|
|
self.name == other.name and self.parent == other.parent)
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self == other
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name, self.parent))
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _memoizedcall(self, attrname, function):
|
|
|
|
exattrname = "_ex_" + attrname
|
|
|
|
failure = getattr(self, exattrname, None)
|
|
|
|
if failure is not None:
|
|
|
|
py.builtin._reraise(failure[0], failure[1], failure[2])
|
|
|
|
if hasattr(self, attrname):
|
|
|
|
return getattr(self, attrname)
|
|
|
|
try:
|
|
|
|
res = function()
|
|
|
|
except py.builtin._sysex:
|
|
|
|
raise
|
|
|
|
except:
|
|
|
|
failure = py.std.sys.exc_info()
|
|
|
|
setattr(self, exattrname, failure)
|
|
|
|
raise
|
|
|
|
setattr(self, attrname, res)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def listchain(self):
|
|
|
|
""" return list of all parent collectors up to self,
|
|
|
|
starting from root of collection tree. """
|
2011-12-02 02:36:44 +08:00
|
|
|
chain = []
|
|
|
|
item = self
|
|
|
|
while item is not None:
|
|
|
|
chain.append(item)
|
|
|
|
item = item.parent
|
|
|
|
chain.reverse()
|
|
|
|
return chain
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def listnames(self):
|
|
|
|
return [x.name for x in self.listchain()]
|
|
|
|
|
2010-11-25 05:22:52 +08:00
|
|
|
def getplugins(self):
|
|
|
|
return self.config._getmatchingplugins(self.fspath)
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def getparent(self, cls):
|
|
|
|
current = self
|
|
|
|
while current and not isinstance(current, cls):
|
|
|
|
current = current.parent
|
|
|
|
return current
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
|
|
|
pass
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def _repr_failure_py(self, excinfo, style=None):
|
|
|
|
if self.config.option.fulltrace:
|
|
|
|
style="long"
|
|
|
|
else:
|
2010-11-06 06:37:31 +08:00
|
|
|
self._prunetraceback(excinfo)
|
2010-10-12 18:19:53 +08:00
|
|
|
# XXX should excinfo.getrepr record all data and toterminal()
|
|
|
|
# process it?
|
|
|
|
if style is None:
|
|
|
|
if self.config.option.tbstyle == "short":
|
|
|
|
style = "short"
|
|
|
|
else:
|
|
|
|
style = "long"
|
|
|
|
return excinfo.getrepr(funcargs=True,
|
|
|
|
showlocals=self.config.option.showlocals,
|
|
|
|
style=style)
|
|
|
|
|
|
|
|
repr_failure = _repr_failure_py
|
|
|
|
|
|
|
|
class Collector(Node):
|
|
|
|
""" Collector instances create children through collect()
|
|
|
|
and thus iteratively build a tree.
|
|
|
|
"""
|
|
|
|
class CollectError(Exception):
|
|
|
|
""" an error during collection, contains a custom message. """
|
|
|
|
|
|
|
|
def collect(self):
|
|
|
|
""" returns a list of children (items and collectors)
|
|
|
|
for this collection node.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("abstract")
|
|
|
|
|
|
|
|
def repr_failure(self, excinfo):
|
2010-11-01 01:01:33 +08:00
|
|
|
""" represent a collection failure. """
|
2010-10-12 18:19:53 +08:00
|
|
|
if excinfo.errisinstance(self.CollectError):
|
|
|
|
exc = excinfo.value
|
|
|
|
return str(exc.args[0])
|
|
|
|
return self._repr_failure_py(excinfo, style="short")
|
|
|
|
|
|
|
|
def _memocollect(self):
|
|
|
|
""" internal helper method to cache results of calling collect(). """
|
2010-11-06 16:58:04 +08:00
|
|
|
return self._memoizedcall('_collected', lambda: list(self.collect()))
|
2010-10-12 18:19:53 +08:00
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
2010-10-12 18:19:53 +08:00
|
|
|
if hasattr(self, 'fspath'):
|
|
|
|
path = self.fspath
|
2010-11-06 06:37:31 +08:00
|
|
|
traceback = excinfo.traceback
|
2010-10-12 18:19:53 +08:00
|
|
|
ntraceback = traceback.cut(path=self.fspath)
|
|
|
|
if ntraceback == traceback:
|
2010-11-06 06:37:31 +08:00
|
|
|
ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
|
|
|
|
excinfo.traceback = ntraceback.filter()
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
class FSCollector(Collector):
|
2010-11-07 17:19:58 +08:00
|
|
|
def __init__(self, fspath, parent=None, config=None, session=None):
|
2010-11-06 16:58:04 +08:00
|
|
|
fspath = py.path.local(fspath) # xxx only for test_resultlog.py?
|
2010-11-07 19:05:32 +08:00
|
|
|
name = fspath.basename
|
|
|
|
if parent is not None:
|
|
|
|
rel = fspath.relto(parent.fspath)
|
|
|
|
if rel:
|
|
|
|
name = rel
|
|
|
|
name = name.replace(os.sep, "/")
|
2010-11-07 17:19:58 +08:00
|
|
|
super(FSCollector, self).__init__(name, parent, config, session)
|
2010-10-12 18:19:53 +08:00
|
|
|
self.fspath = fspath
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def _makeid(self):
|
2010-11-07 17:19:58 +08:00
|
|
|
if self == self.session:
|
2010-11-06 16:58:04 +08:00
|
|
|
return "."
|
2010-11-07 17:19:58 +08:00
|
|
|
relpath = self.session.fspath.bestrelpath(self.fspath)
|
2010-11-06 16:58:04 +08:00
|
|
|
if os.sep != "/":
|
2010-11-07 08:13:40 +08:00
|
|
|
relpath = relpath.replace(os.sep, "/")
|
2010-11-06 16:58:04 +08:00
|
|
|
return relpath
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class File(FSCollector):
|
|
|
|
""" base class for collecting tests from a file. """
|
|
|
|
|
|
|
|
class Item(Node):
|
|
|
|
""" a basic test invocation item. Note that for a single function
|
2010-11-01 01:01:33 +08:00
|
|
|
there might be multiple test invocation items.
|
2010-10-12 18:19:53 +08:00
|
|
|
"""
|
2011-11-19 00:58:21 +08:00
|
|
|
nextitem = None
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def reportinfo(self):
|
|
|
|
return self.fspath, None, ""
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
try:
|
|
|
|
return self._location
|
|
|
|
except AttributeError:
|
|
|
|
location = self.reportinfo()
|
2011-03-08 01:28:45 +08:00
|
|
|
# bestrelpath is a quite slow function
|
|
|
|
cache = self.config.__dict__.setdefault("_bestrelpathcache", {})
|
|
|
|
try:
|
|
|
|
fspath = cache[location[0]]
|
|
|
|
except KeyError:
|
|
|
|
fspath = self.session.fspath.bestrelpath(location[0])
|
|
|
|
cache[location[0]] = fspath
|
2010-11-14 06:33:38 +08:00
|
|
|
location = (fspath, location[1], str(location[2]))
|
2010-11-06 16:58:04 +08:00
|
|
|
self._location = location
|
|
|
|
return location
|
|
|
|
|
2010-11-07 19:05:32 +08:00
|
|
|
class NoMatch(Exception):
|
|
|
|
""" raised if matching cannot locate a matching names. """
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
class Session(FSCollector):
|
|
|
|
class Interrupted(KeyboardInterrupt):
|
|
|
|
""" signals an interrupted test run. """
|
|
|
|
__module__ = 'builtins' # for py3
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def __init__(self, config):
|
2010-11-07 17:19:58 +08:00
|
|
|
super(Session, self).__init__(py.path.local(), parent=None,
|
|
|
|
config=config, session=self)
|
2012-06-21 17:20:29 +08:00
|
|
|
assert self.config.pluginmanager.register(
|
|
|
|
self, name="session", prepend=True)
|
2010-11-07 17:19:58 +08:00
|
|
|
self._testsfailed = 0
|
|
|
|
self.shouldstop = False
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace = config.trace.root.get("collection")
|
|
|
|
self._norecursepatterns = config.getini("norecursedirs")
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_collectstart(self):
|
|
|
|
if self.shouldstop:
|
|
|
|
raise self.Interrupted(self.shouldstop)
|
|
|
|
|
|
|
|
def pytest_runtest_logreport(self, report):
|
2012-06-23 17:32:32 +08:00
|
|
|
if report.failed and not hasattr(report, 'wasxfail'):
|
2010-11-07 17:19:58 +08:00
|
|
|
self._testsfailed += 1
|
|
|
|
maxfail = self.config.getvalue("maxfail")
|
|
|
|
if maxfail and self._testsfailed >= maxfail:
|
|
|
|
self.shouldstop = "stopping after %d failures" % (
|
|
|
|
self._testsfailed)
|
|
|
|
pytest_collectreport = pytest_runtest_logreport
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def isinitpath(self, path):
|
|
|
|
return path in self._initialpaths
|
|
|
|
|
|
|
|
def gethookproxy(self, fspath):
|
|
|
|
return HookProxy(fspath, self.config)
|
|
|
|
|
|
|
|
def perform_collect(self, args=None, genitems=True):
|
2011-05-27 08:53:47 +08:00
|
|
|
hook = self.config.hook
|
|
|
|
try:
|
|
|
|
items = self._perform_collect(args, genitems)
|
|
|
|
hook.pytest_collection_modifyitems(session=self,
|
|
|
|
config=self.config, items=items)
|
|
|
|
finally:
|
|
|
|
hook.pytest_collection_finish(session=self)
|
|
|
|
return items
|
|
|
|
|
|
|
|
def _perform_collect(self, args, genitems):
|
2010-11-06 16:58:04 +08:00
|
|
|
if args is None:
|
|
|
|
args = self.config.args
|
|
|
|
self.trace("perform_collect", self, args)
|
|
|
|
self.trace.root.indent += 1
|
|
|
|
self._notfound = []
|
|
|
|
self._initialpaths = set()
|
2010-11-07 07:22:16 +08:00
|
|
|
self._initialparts = []
|
2012-02-03 23:33:32 +08:00
|
|
|
self.items = items = []
|
2010-11-06 16:58:04 +08:00
|
|
|
for arg in args:
|
|
|
|
parts = self._parsearg(arg)
|
2010-11-07 07:22:16 +08:00
|
|
|
self._initialparts.append(parts)
|
2010-11-06 16:58:04 +08:00
|
|
|
self._initialpaths.add(parts[0])
|
|
|
|
self.ihook.pytest_collectstart(collector=self)
|
2011-05-27 08:43:02 +08:00
|
|
|
rep = self.ihook.pytest_make_collect_report(collector=self)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.ihook.pytest_collectreport(report=rep)
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
if self._notfound:
|
|
|
|
for arg, exc in self._notfound:
|
2010-11-07 07:22:16 +08:00
|
|
|
line = "(no name %r in any of %r)" % (arg, exc.args[0])
|
2010-11-06 16:58:04 +08:00
|
|
|
raise pytest.UsageError("not found: %s\n%s" %(arg, line))
|
|
|
|
if not genitems:
|
|
|
|
return rep.result
|
|
|
|
else:
|
|
|
|
if rep.passed:
|
|
|
|
for node in rep.result:
|
|
|
|
self.items.extend(self.genitems(node))
|
|
|
|
return items
|
|
|
|
|
|
|
|
def collect(self):
|
2010-11-07 07:22:16 +08:00
|
|
|
for parts in self._initialparts:
|
|
|
|
arg = "::".join(map(str, parts))
|
|
|
|
self.trace("processing argument", arg)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace.root.indent += 1
|
|
|
|
try:
|
|
|
|
for x in self._collect(arg):
|
|
|
|
yield x
|
|
|
|
except NoMatch:
|
|
|
|
# we are inside a make_report hook so
|
|
|
|
# we cannot directly pass through the exception
|
|
|
|
self._notfound.append((arg, sys.exc_info()[1]))
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
break
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
|
|
|
|
def _collect(self, arg):
|
|
|
|
names = self._parsearg(arg)
|
|
|
|
path = names.pop(0)
|
|
|
|
if path.check(dir=1):
|
|
|
|
assert not names, "invalid arg %r" %(arg,)
|
2010-11-18 21:56:16 +08:00
|
|
|
for path in path.visit(fil=lambda x: x.check(file=1),
|
2012-06-21 17:20:29 +08:00
|
|
|
rec=self._recurse, bf=True, sort=True):
|
2010-11-06 16:58:04 +08:00
|
|
|
for x in self._collectfile(path):
|
|
|
|
yield x
|
|
|
|
else:
|
|
|
|
assert path.check(file=1)
|
|
|
|
for x in self.matchnodes(self._collectfile(path), names):
|
|
|
|
yield x
|
|
|
|
|
|
|
|
def _collectfile(self, path):
|
|
|
|
ihook = self.gethookproxy(path)
|
2010-11-07 07:22:16 +08:00
|
|
|
if not self.isinitpath(path):
|
|
|
|
if ihook.pytest_ignore_collect(path=path, config=self.config):
|
2012-06-21 17:20:29 +08:00
|
|
|
return ()
|
2010-11-06 16:58:04 +08:00
|
|
|
return ihook.pytest_collect_file(path=path, parent=self)
|
|
|
|
|
|
|
|
def _recurse(self, path):
|
2010-11-18 21:56:16 +08:00
|
|
|
ihook = self.gethookproxy(path.dirpath())
|
2010-11-06 16:58:04 +08:00
|
|
|
if ihook.pytest_ignore_collect(path=path, config=self.config):
|
2012-06-21 17:20:29 +08:00
|
|
|
return
|
2010-11-06 16:58:04 +08:00
|
|
|
for pat in self._norecursepatterns:
|
|
|
|
if path.check(fnmatch=pat):
|
|
|
|
return False
|
2010-11-18 21:56:16 +08:00
|
|
|
ihook = self.gethookproxy(path)
|
2010-11-06 16:58:04 +08:00
|
|
|
ihook.pytest_collect_directory(path=path, parent=self)
|
|
|
|
return True
|
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
def _tryconvertpyarg(self, x):
|
2011-09-01 22:19:16 +08:00
|
|
|
mod = None
|
|
|
|
path = [os.path.abspath('.')] + sys.path
|
|
|
|
for name in x.split('.'):
|
2011-10-27 13:38:44 +08:00
|
|
|
# ignore anything that's not a proper name here
|
2011-10-27 04:40:08 +08:00
|
|
|
# else something like --pyargs will mess up '.'
|
2011-10-27 13:38:44 +08:00
|
|
|
# since imp.find_module will actually sometimes work for it
|
|
|
|
# but it's supposed to be considered a filesystem path
|
2011-10-27 04:40:08 +08:00
|
|
|
# not a package
|
|
|
|
if name_re.match(name) is None:
|
|
|
|
return x
|
2011-09-01 22:19:16 +08:00
|
|
|
try:
|
|
|
|
fd, mod, type_ = imp.find_module(name, path)
|
|
|
|
except ImportError:
|
|
|
|
return x
|
|
|
|
else:
|
|
|
|
if fd is not None:
|
|
|
|
fd.close()
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2011-09-01 22:19:16 +08:00
|
|
|
if type_[2] != imp.PKG_DIRECTORY:
|
|
|
|
path = [os.path.dirname(mod)]
|
|
|
|
else:
|
|
|
|
path = [mod]
|
|
|
|
return mod
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def _parsearg(self, arg):
|
|
|
|
""" return (fspath, names) tuple after checking the file exists. """
|
2010-11-07 05:17:33 +08:00
|
|
|
arg = str(arg)
|
|
|
|
if self.config.option.pyargs:
|
|
|
|
arg = self._tryconvertpyarg(arg)
|
2010-11-06 16:58:04 +08:00
|
|
|
parts = str(arg).split("::")
|
2010-11-07 19:05:32 +08:00
|
|
|
relpath = parts[0].replace("/", os.sep)
|
|
|
|
path = self.fspath.join(relpath, abs=True)
|
2010-11-06 16:58:04 +08:00
|
|
|
if not path.check():
|
2010-11-07 05:17:33 +08:00
|
|
|
if self.config.option.pyargs:
|
|
|
|
msg = "file or package not found: "
|
|
|
|
else:
|
|
|
|
msg = "file not found: "
|
|
|
|
raise pytest.UsageError(msg + arg)
|
2010-11-06 16:58:04 +08:00
|
|
|
parts[0] = path
|
|
|
|
return parts
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def matchnodes(self, matching, names):
|
|
|
|
self.trace("matchnodes", matching, names)
|
|
|
|
self.trace.root.indent += 1
|
|
|
|
nodes = self._matchnodes(matching, names)
|
|
|
|
num = len(nodes)
|
|
|
|
self.trace("matchnodes finished -> ", num, "nodes")
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
if num == 0:
|
|
|
|
raise NoMatch(matching, names[:1])
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
def _matchnodes(self, matching, names):
|
|
|
|
if not matching or not names:
|
|
|
|
return matching
|
|
|
|
name = names[0]
|
|
|
|
assert name
|
|
|
|
nextnames = names[1:]
|
|
|
|
resultnodes = []
|
|
|
|
for node in matching:
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(node, pytest.Item):
|
2010-11-18 01:24:28 +08:00
|
|
|
if not names:
|
|
|
|
resultnodes.append(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
continue
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(node, pytest.Collector)
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_collectstart(collector=node)
|
|
|
|
rep = node.ihook.pytest_make_collect_report(collector=node)
|
|
|
|
if rep.passed:
|
2011-02-07 18:09:42 +08:00
|
|
|
has_matched = False
|
2010-11-06 16:58:04 +08:00
|
|
|
for x in rep.result:
|
|
|
|
if x.name == name:
|
|
|
|
resultnodes.extend(self.matchnodes([x], nextnames))
|
2011-02-07 18:09:42 +08:00
|
|
|
has_matched = True
|
|
|
|
# XXX accept IDs that don't have "()" for class instances
|
|
|
|
if not has_matched and len(rep.result) == 1 and x.name == "()":
|
|
|
|
nextnames.insert(0, name)
|
|
|
|
resultnodes.extend(self.matchnodes([x], nextnames))
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_collectreport(report=rep)
|
|
|
|
return resultnodes
|
|
|
|
|
|
|
|
def genitems(self, node):
|
|
|
|
self.trace("genitems", node)
|
2010-11-13 16:05:11 +08:00
|
|
|
if isinstance(node, pytest.Item):
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_itemcollected(item=node)
|
|
|
|
yield node
|
|
|
|
else:
|
2010-11-13 16:05:11 +08:00
|
|
|
assert isinstance(node, pytest.Collector)
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_collectstart(collector=node)
|
|
|
|
rep = node.ihook.pytest_make_collect_report(collector=node)
|
|
|
|
if rep.passed:
|
|
|
|
for subnode in rep.result:
|
|
|
|
for x in self.genitems(subnode):
|
|
|
|
yield x
|
|
|
|
node.ihook.pytest_collectreport(report=rep)
|