2010-11-07 17:19:58 +08:00
|
|
|
""" core implementation of testing process: init, session, runtest loop. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2017-12-12 16:27:08 +08:00
|
|
|
import contextlib
|
2016-11-28 07:55:49 +08:00
|
|
|
import functools
|
2015-11-27 22:43:01 +08:00
|
|
|
import os
|
2017-12-12 16:27:08 +08:00
|
|
|
import pkgutil
|
2017-08-03 01:33:52 +08:00
|
|
|
import six
|
2015-11-27 22:43:01 +08:00
|
|
|
import sys
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest
|
2017-10-24 17:42:16 +08:00
|
|
|
from _pytest import nodes
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2012-06-25 23:35:33 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
from _pytest.config import directory_arg, UsageError, hookimpl
|
2015-09-19 07:03:05 +08:00
|
|
|
from _pytest.outcomes import exit
|
2017-10-24 17:42:16 +08:00
|
|
|
from _pytest.runner import collect_one_node
|
2012-07-24 18:10:04 +08:00
|
|
|
|
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
|
2015-07-05 01:42:22 +08:00
|
|
|
EXIT_NOTESTSCOLLECTED = 5
|
2010-11-01 07:27:12 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
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",
|
2017-07-17 07:25:07 +08:00
|
|
|
type="args", default=['.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv'])
|
2017-07-17 07:25:10 +08:00
|
|
|
parser.addini("testpaths", "directories to search for tests when no files or directories are given in the "
|
|
|
|
"command line.",
|
2017-07-17 07:25:07 +08:00
|
|
|
type="args", default=[])
|
2017-07-17 07:25:09 +08:00
|
|
|
# parser.addini("dirpatterns",
|
2010-11-06 16:58:04 +08:00
|
|
|
# "patterns specifying possible locations of test files",
|
|
|
|
# type="linelist", default=["**/test_*.txt",
|
|
|
|
# "**/test_*.py", "**/*_test.py"]
|
2017-07-17 07:25:09 +08:00
|
|
|
# )
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("general", "running and selection options")
|
2016-07-20 23:15:29 +08:00
|
|
|
group._addoption('-x', '--exitfirst', action="store_const",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="maxfail", const=1,
|
|
|
|
help="exit instantly on first error or failed test."),
|
2010-10-10 19:48:48 +08:00
|
|
|
group._addoption('--maxfail', metavar="num",
|
2017-07-17 07:25:07 +08:00
|
|
|
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",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="marks not registered in configuration file raise errors.")
|
2014-06-28 18:03:55 +08:00
|
|
|
group._addoption("-c", metavar="file", type=str, dest="inifilename",
|
2017-07-17 07:25:10 +08:00
|
|
|
help="load configuration from `file` instead of trying to locate one of the implicit "
|
|
|
|
"configuration files.")
|
2016-06-20 21:05:50 +08:00
|
|
|
group._addoption("--continue-on-collection-errors", action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
default=False, dest="continue_on_collection_errors",
|
|
|
|
help="Force test execution even if collection errors occur.")
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("collect", "collection")
|
2013-08-02 15:52:40 +08:00
|
|
|
group.addoption('--collectonly', '--collect-only', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="only collect tests, don't execute them."),
|
2010-11-07 05:17:33 +08:00
|
|
|
group.addoption('--pyargs', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="try to interpret all arguments as python packages.")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption("--ignore", action="append", metavar="path",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="ignore path during collection (multi-allowed).")
|
2013-08-01 22:49:26 +08:00
|
|
|
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
|
|
|
|
# needs upgrading as well
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption('--confcutdir', dest="confcutdir", default=None,
|
2017-07-17 07:25:07 +08:00
|
|
|
metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"),
|
|
|
|
help="only load conftest.py's relative to specified dir.")
|
2015-06-23 13:53:32 +08:00
|
|
|
group.addoption('--noconftest', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="noconftest", default=False,
|
|
|
|
help="Don't load any conftest.py files.")
|
2016-07-25 18:40:57 +08:00
|
|
|
group.addoption('--keepduplicates', '--keep-duplicates', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="keepduplicates", default=False,
|
|
|
|
help="Keep duplicate tests.")
|
2017-07-12 00:52:16 +08:00
|
|
|
group.addoption('--collect-in-virtualenv', action='store_true',
|
2017-07-20 04:09:05 +08:00
|
|
|
dest='collect_in_virtualenv', default=False,
|
|
|
|
help="Don't ignore tests in a local virtualenv directory")
|
2010-10-10 19:48:48 +08:00
|
|
|
|
|
|
|
group = parser.getgroup("debugconfig",
|
2017-07-17 07:25:07 +08:00
|
|
|
"test session debugging and configuration")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="base temporary directory for this test run.")
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_configure(config):
|
2017-07-17 07:25:09 +08:00
|
|
|
__import__('pytest').config = config # compatibiltiy
|
2016-07-20 23:15:29 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
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:
|
2015-04-22 22:33:20 +08:00
|
|
|
config._do_configure()
|
2012-05-18 05:11:23 +08:00
|
|
|
initstate = 1
|
|
|
|
config.hook.pytest_sessionstart(session=session)
|
|
|
|
initstate = 2
|
2015-08-28 01:22:22 +08:00
|
|
|
session.exitstatus = doit(config, session) or 0
|
2017-03-16 01:00:59 +08:00
|
|
|
except UsageError:
|
2015-04-28 17:54:45 +08:00
|
|
|
raise
|
2017-10-17 23:53:47 +08:00
|
|
|
except Failed:
|
|
|
|
session.exitstatus = EXIT_TESTSFAILED
|
2012-05-18 05:11:23 +08:00
|
|
|
except KeyboardInterrupt:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2017-03-16 01:00:59 +08:00
|
|
|
if initstate < 2 and isinstance(excinfo.value, exit.Exception):
|
2016-07-24 20:13:43 +08:00
|
|
|
sys.stderr.write('{0}: {1}\n'.format(
|
2016-07-25 14:16:32 +08:00
|
|
|
excinfo.typename, excinfo.value.msg))
|
2012-05-18 05:11:23 +08:00
|
|
|
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
|
|
|
|
session.exitstatus = EXIT_INTERRUPTED
|
2017-11-04 23:21:34 +08:00
|
|
|
except: # noqa
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2013-09-30 19:14:14 +08:00
|
|
|
config.notify_exception(excinfo, config.option)
|
2012-05-18 05:11:23 +08:00
|
|
|
session.exitstatus = EXIT_INTERNALERROR
|
|
|
|
if excinfo.errisinstance(SystemExit):
|
|
|
|
sys.stderr.write("mainloop: caught Spurious SystemExit!\n")
|
2015-08-28 01:22:22 +08:00
|
|
|
|
2012-05-18 05:11:23 +08:00
|
|
|
finally:
|
2014-04-02 21:34:36 +08:00
|
|
|
excinfo = None # Explicitly break reference cycle.
|
2013-06-10 16:09:28 +08:00
|
|
|
session.startdir.chdir()
|
2012-05-18 05:11:23 +08:00
|
|
|
if initstate >= 2:
|
2013-02-14 19:13:04 +08:00
|
|
|
config.hook.pytest_sessionfinish(
|
|
|
|
session=session,
|
|
|
|
exitstatus=session.exitstatus)
|
2015-04-22 22:33:20 +08:00
|
|
|
config._ensure_unconfigure()
|
2010-11-01 07:27:12 +08:00
|
|
|
return session.exitstatus
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
return wrap_session(config, _main)
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
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)
|
|
|
|
|
2015-08-28 01:22:22 +08:00
|
|
|
if session.testsfailed:
|
|
|
|
return EXIT_TESTSFAILED
|
|
|
|
elif session.testscollected == 0:
|
|
|
|
return EXIT_NOTESTSCOLLECTED
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
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
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_runtestloop(session):
|
2016-06-20 21:05:50 +08:00
|
|
|
if (session.testsfailed and
|
|
|
|
not session.config.option.continue_on_collection_errors):
|
|
|
|
raise session.Interrupted(
|
|
|
|
"%d errors during collection" % session.testsfailed)
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.config.option.collectonly:
|
|
|
|
return True
|
2012-09-15 21:20:49 +08:00
|
|
|
|
|
|
|
for i, item in enumerate(session.items):
|
2017-07-17 07:25:08 +08:00
|
|
|
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
|
2011-12-03 05:00:19 +08:00
|
|
|
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
|
2017-10-17 23:53:47 +08:00
|
|
|
if session.shouldfail:
|
|
|
|
raise session.Failed(session.shouldfail)
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.shouldstop:
|
|
|
|
raise session.Interrupted(session.shouldstop)
|
|
|
|
return True
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2017-07-12 03:32:09 +08:00
|
|
|
def _in_venv(path):
|
|
|
|
"""Attempts to detect if ``path`` is the root of a Virtual Environment by
|
|
|
|
checking for the existence of the appropriate activate script"""
|
|
|
|
bindir = path.join('Scripts' if sys.platform.startswith('win') else 'bin')
|
|
|
|
if not bindir.exists():
|
|
|
|
return False
|
|
|
|
activates = ('activate', 'activate.csh', 'activate.fish',
|
|
|
|
'Activate', 'Activate.bat', 'Activate.ps1')
|
|
|
|
return any([fname.basename in activates for fname in bindir.listdir()])
|
|
|
|
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_ignore_collect(path, config):
|
2017-06-06 10:21:29 +08:00
|
|
|
ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath())
|
2010-10-10 19:48:48 +08:00
|
|
|
ignore_paths = ignore_paths or []
|
2014-04-03 04:30:45 +08:00
|
|
|
excludeopt = config.getoption("ignore")
|
2010-10-10 19:48:48 +08:00
|
|
|
if excludeopt:
|
|
|
|
ignore_paths.extend([py.path.local(x) for x in excludeopt])
|
2016-07-25 18:40:57 +08:00
|
|
|
|
2017-06-06 10:21:29 +08:00
|
|
|
if py.path.local(path) in ignore_paths:
|
2016-07-25 18:40:57 +08:00
|
|
|
return True
|
|
|
|
|
2017-07-12 03:32:09 +08:00
|
|
|
allow_in_venv = config.getoption("collect_in_virtualenv")
|
|
|
|
if _in_venv(path) and not allow_in_venv:
|
2017-07-12 00:52:16 +08:00
|
|
|
return True
|
|
|
|
|
2016-07-25 18:40:57 +08:00
|
|
|
# Skip duplicate paths.
|
|
|
|
keepduplicates = config.getoption("keepduplicates")
|
|
|
|
duplicate_paths = config.pluginmanager._duplicatepaths
|
|
|
|
if not keepduplicates:
|
|
|
|
if path in duplicate_paths:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
duplicate_paths.add(path)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-12-12 17:53:06 +08:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def _patched_find_module():
|
|
|
|
"""Patch bug in pkgutil.ImpImporter.find_module
|
|
|
|
|
|
|
|
When using pkgutil.find_loader on python<3.4 it removes symlinks
|
|
|
|
from the path due to a call to os.path.realpath. This is not consistent
|
|
|
|
with actually doing the import (in these versions, pkgutil and __import__
|
|
|
|
did not share the same underlying code). This can break conftest
|
|
|
|
discovery for pytest where symlinks are involved.
|
|
|
|
|
|
|
|
The only supported python<3.4 by pytest is python 2.7.
|
|
|
|
"""
|
|
|
|
if six.PY2: # python 3.4+ uses importlib instead
|
|
|
|
def find_module_patched(self, fullname, path=None):
|
|
|
|
# Note: we ignore 'path' argument since it is only used via meta_path
|
|
|
|
subname = fullname.split(".")[-1]
|
|
|
|
if subname != fullname and self.path is None:
|
|
|
|
return None
|
|
|
|
if self.path is None:
|
|
|
|
path = None
|
|
|
|
else:
|
|
|
|
# original: path = [os.path.realpath(self.path)]
|
|
|
|
path = [self.path]
|
|
|
|
try:
|
|
|
|
file, filename, etc = pkgutil.imp.find_module(subname,
|
|
|
|
path)
|
|
|
|
except ImportError:
|
|
|
|
return None
|
|
|
|
return pkgutil.ImpLoader(fullname, file, filename, etc)
|
|
|
|
|
|
|
|
old_find_module = pkgutil.ImpImporter.find_module
|
|
|
|
pkgutil.ImpImporter.find_module = find_module_patched
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
pkgutil.ImpImporter.find_module = old_find_module
|
|
|
|
else:
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class FSHookProxy:
|
2015-04-26 06:10:52 +08:00
|
|
|
def __init__(self, fspath, pm, remove_mods):
|
2010-11-06 16:58:04 +08:00
|
|
|
self.fspath = fspath
|
2015-04-26 06:10:52 +08:00
|
|
|
self.pm = pm
|
|
|
|
self.remove_mods = remove_mods
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def __getattr__(self, name):
|
2015-04-26 06:10:52 +08:00
|
|
|
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
|
2014-10-01 18:19:11 +08:00
|
|
|
self.__dict__[name] = x
|
|
|
|
return x
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-11-07 19:05:32 +08:00
|
|
|
class NoMatch(Exception):
|
|
|
|
""" raised if matching cannot locate a matching names. """
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-07-16 05:28:43 +08:00
|
|
|
class Interrupted(KeyboardInterrupt):
|
|
|
|
""" signals an interrupted test run. """
|
2017-07-17 07:25:09 +08:00
|
|
|
__module__ = 'builtins' # for py3
|
2015-07-16 05:28:43 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-10-17 23:53:47 +08:00
|
|
|
class Failed(Exception):
|
|
|
|
""" signals an stop as failed test run. """
|
|
|
|
|
|
|
|
|
2017-12-17 22:19:01 +08:00
|
|
|
class Session(nodes.FSCollector):
|
2015-07-16 05:28:43 +08:00
|
|
|
Interrupted = Interrupted
|
2017-10-17 23:53:47 +08:00
|
|
|
Failed = Failed
|
2010-11-07 17:19:58 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def __init__(self, config):
|
2017-12-17 22:19:01 +08:00
|
|
|
nodes.FSCollector.__init__(
|
|
|
|
self, config.rootdir, parent=None,
|
|
|
|
config=config, session=self)
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed = 0
|
|
|
|
self.testscollected = 0
|
2010-11-07 17:19:58 +08:00
|
|
|
self.shouldstop = False
|
2017-10-17 23:53:47 +08:00
|
|
|
self.shouldfail = False
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace = config.trace.root.get("collection")
|
|
|
|
self._norecursepatterns = config.getini("norecursedirs")
|
2013-06-10 16:09:28 +08:00
|
|
|
self.startdir = py.path.local()
|
2015-04-29 22:40:52 +08:00
|
|
|
self.config.pluginmanager.register(self, name="session")
|
2010-11-06 16:58:04 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
def _makeid(self):
|
|
|
|
return ""
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
@hookimpl(tryfirst=True)
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_collectstart(self):
|
2017-10-17 23:53:47 +08:00
|
|
|
if self.shouldfail:
|
|
|
|
raise self.Failed(self.shouldfail)
|
2010-11-07 17:19:58 +08:00
|
|
|
if self.shouldstop:
|
|
|
|
raise self.Interrupted(self.shouldstop)
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
@hookimpl(tryfirst=True)
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_runtest_logreport(self, report):
|
2012-06-23 17:32:32 +08:00
|
|
|
if report.failed and not hasattr(report, 'wasxfail'):
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed += 1
|
2010-11-07 17:19:58 +08:00
|
|
|
maxfail = self.config.getvalue("maxfail")
|
2015-07-07 07:25:16 +08:00
|
|
|
if maxfail and self.testsfailed >= maxfail:
|
2017-10-17 23:53:47 +08:00
|
|
|
self.shouldfail = "stopping after %d failures" % (
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed)
|
2010-11-07 17:19:58 +08:00
|
|
|
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):
|
2016-11-21 22:50:21 +08:00
|
|
|
# check if we have the common case of running
|
|
|
|
# hooks with all conftest.py filesall conftest.py
|
|
|
|
pm = self.config.pluginmanager
|
|
|
|
my_conftestmodules = pm._getconftestmodules(fspath)
|
|
|
|
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
|
|
|
|
if remove_mods:
|
|
|
|
# one or more conftests are not in use at this fspath
|
|
|
|
proxy = FSHookProxy(fspath, pm, remove_mods)
|
|
|
|
else:
|
|
|
|
# all plugis are active for this fspath
|
|
|
|
proxy = self.config.hook
|
|
|
|
return proxy
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
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)
|
2017-03-08 08:02:52 +08:00
|
|
|
self.config.pluginmanager.check_pending()
|
2011-05-27 08:53:47 +08:00
|
|
|
hook.pytest_collection_modifyitems(session=self,
|
2017-07-17 07:25:07 +08:00
|
|
|
config=self.config, items=items)
|
2011-05-27 08:53:47 +08:00
|
|
|
finally:
|
|
|
|
hook.pytest_collection_finish(session=self)
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testscollected = len(items)
|
2011-05-27 08:53:47 +08:00
|
|
|
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])
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(self)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.ihook.pytest_collectreport(report=rep)
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
if self._notfound:
|
2013-09-09 04:26:51 +08:00
|
|
|
errors = []
|
2010-11-06 16:58:04 +08:00
|
|
|
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])
|
2013-09-09 04:26:51 +08:00
|
|
|
errors.append("not found: %s\n%s" % (arg, line))
|
2017-03-16 01:00:59 +08:00
|
|
|
# XXX: test this
|
|
|
|
raise UsageError(*errors)
|
2010-11-06 16:58:04 +08:00
|
|
|
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]))
|
2013-09-09 04:26:51 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace.root.indent -= 1
|
|
|
|
|
|
|
|
def _collect(self, arg):
|
|
|
|
names = self._parsearg(arg)
|
|
|
|
path = names.pop(0)
|
|
|
|
if path.check(dir=1):
|
2017-03-16 01:00:59 +08:00
|
|
|
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):
|
2016-06-08 21:17:55 +08:00
|
|
|
"""Convert a dotted module name to path.
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2016-06-08 21:17:55 +08:00
|
|
|
"""
|
2017-12-07 05:55:04 +08:00
|
|
|
|
2016-06-08 21:17:55 +08:00
|
|
|
try:
|
2017-12-12 16:27:08 +08:00
|
|
|
with _patched_find_module():
|
|
|
|
loader = pkgutil.find_loader(x)
|
2016-06-08 21:17:55 +08:00
|
|
|
except ImportError:
|
|
|
|
return x
|
|
|
|
if loader is None:
|
|
|
|
return x
|
2016-06-09 14:22:11 +08:00
|
|
|
# This method is sometimes invoked when AssertionRewritingHook, which
|
|
|
|
# does not define a get_filename method, is already in place:
|
2016-06-08 21:17:55 +08:00
|
|
|
try:
|
2017-12-12 16:27:08 +08:00
|
|
|
with _patched_find_module():
|
|
|
|
path = loader.get_filename(x)
|
2016-06-09 14:22:11 +08:00
|
|
|
except AttributeError:
|
|
|
|
# Retrieve path from AssertionRewritingHook:
|
2016-06-08 21:17:55 +08:00
|
|
|
path = loader.modules[x][0].co_filename
|
|
|
|
if loader.is_package(x):
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
return path
|
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. """
|
|
|
|
parts = str(arg).split("::")
|
2016-06-08 21:17:55 +08:00
|
|
|
if self.config.option.pyargs:
|
|
|
|
parts[0] = self._tryconvertpyarg(parts[0])
|
2010-11-07 19:05:32 +08:00
|
|
|
relpath = parts[0].replace("/", os.sep)
|
2015-02-27 04:56:44 +08:00
|
|
|
path = self.config.invocation_dir.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:
|
2017-03-16 01:00:59 +08:00
|
|
|
raise UsageError(
|
|
|
|
"file or package not found: " + arg +
|
|
|
|
" (missing __init__.py?)")
|
2010-11-07 05:17:33 +08:00
|
|
|
else:
|
2017-03-16 01:00:59 +08:00
|
|
|
raise UsageError("file not found: " + 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:
|
2017-12-17 22:19:01 +08:00
|
|
|
if isinstance(node, nodes.Item):
|
2010-11-18 01:24:28 +08:00
|
|
|
if not names:
|
|
|
|
resultnodes.append(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
continue
|
2017-12-17 22:19:01 +08:00
|
|
|
assert isinstance(node, nodes.Collector)
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
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:
|
2016-03-16 05:42:25 +08:00
|
|
|
# TODO: remove parametrized workaround once collection structure contains parametrization
|
2016-03-14 05:37:21 +08:00
|
|
|
if x.name == name or x.name.split("[")[0] == name:
|
2010-11-06 16:58:04 +08:00
|
|
|
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))
|
2017-06-04 05:26:34 +08:00
|
|
|
else:
|
|
|
|
# report collection failures here to avoid failing to run some test
|
|
|
|
# specified in the command line because the module could not be
|
|
|
|
# imported (#134)
|
|
|
|
node.ihook.pytest_collectreport(report=rep)
|
2010-11-06 16:58:04 +08:00
|
|
|
return resultnodes
|
|
|
|
|
|
|
|
def genitems(self, node):
|
|
|
|
self.trace("genitems", node)
|
2017-12-17 22:19:01 +08:00
|
|
|
if isinstance(node, nodes.Item):
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_itemcollected(item=node)
|
|
|
|
yield node
|
|
|
|
else:
|
2017-12-17 22:19:01 +08:00
|
|
|
assert isinstance(node, nodes.Collector)
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
if rep.passed:
|
|
|
|
for subnode in rep.result:
|
|
|
|
for x in self.genitems(subnode):
|
|
|
|
yield x
|
|
|
|
node.ihook.pytest_collectreport(report=rep)
|