2010-11-22 00:43:18 +08:00
|
|
|
""" command line options, ini-file and conftest.py processing. """
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
import py
|
2010-10-14 00:45:07 +08:00
|
|
|
import sys, os
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.core import PluginManager
|
2010-10-28 01:35:27 +08:00
|
|
|
import pytest
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
def pytest_cmdline_parse(pluginmanager, args):
|
|
|
|
config = Config(pluginmanager)
|
|
|
|
config.parse(args)
|
2010-11-06 06:37:31 +08:00
|
|
|
if config.option.debug:
|
|
|
|
config.trace.root.setwriter(sys.stderr.write)
|
2010-10-12 21:34:32 +08:00
|
|
|
return config
|
|
|
|
|
|
|
|
class Parser:
|
|
|
|
""" Parser for command line arguments. """
|
|
|
|
|
|
|
|
def __init__(self, usage=None, processopt=None):
|
|
|
|
self._anonymous = OptionGroup("custom options", parser=self)
|
|
|
|
self._groups = []
|
|
|
|
self._processopt = processopt
|
|
|
|
self._usage = usage
|
2010-10-31 01:23:50 +08:00
|
|
|
self._inidict = {}
|
2010-11-07 23:10:22 +08:00
|
|
|
self._ininames = []
|
2010-10-12 21:34:32 +08:00
|
|
|
self.hints = []
|
|
|
|
|
|
|
|
def processoption(self, option):
|
|
|
|
if self._processopt:
|
|
|
|
if option.dest:
|
|
|
|
self._processopt(option)
|
|
|
|
|
|
|
|
def addnote(self, note):
|
|
|
|
self._notes.append(note)
|
|
|
|
|
|
|
|
def getgroup(self, name, description="", after=None):
|
2010-10-31 01:23:50 +08:00
|
|
|
""" get (or create) a named option Group.
|
|
|
|
|
|
|
|
:name: unique name of the option group.
|
|
|
|
:description: long description for --help output.
|
|
|
|
:after: name of other group, used for ordering --help output.
|
|
|
|
"""
|
2010-10-12 21:34:32 +08:00
|
|
|
for group in self._groups:
|
|
|
|
if group.name == name:
|
|
|
|
return group
|
|
|
|
group = OptionGroup(name, description, parser=self)
|
|
|
|
i = 0
|
|
|
|
for i, grp in enumerate(self._groups):
|
|
|
|
if grp.name == after:
|
|
|
|
break
|
|
|
|
self._groups.insert(i+1, group)
|
|
|
|
return group
|
|
|
|
|
|
|
|
def addoption(self, *opts, **attrs):
|
|
|
|
""" add an optparse-style option. """
|
|
|
|
self._anonymous.addoption(*opts, **attrs)
|
|
|
|
|
|
|
|
def parse(self, args):
|
2010-10-28 04:29:01 +08:00
|
|
|
self.optparser = optparser = MyOptionParser(self)
|
2010-11-07 23:10:22 +08:00
|
|
|
groups = self._groups + [self._anonymous]
|
2010-10-12 21:34:32 +08:00
|
|
|
for group in groups:
|
|
|
|
if group.options:
|
|
|
|
desc = group.description or group.name
|
|
|
|
optgroup = py.std.optparse.OptionGroup(optparser, desc)
|
2010-11-07 23:10:22 +08:00
|
|
|
optgroup.add_options(group.options)
|
2010-10-12 21:34:32 +08:00
|
|
|
optparser.add_option_group(optgroup)
|
2010-10-28 04:29:01 +08:00
|
|
|
return self.optparser.parse_args([str(x) for x in args])
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
def parse_setoption(self, args, option):
|
|
|
|
parsedoption, args = self.parse(args)
|
|
|
|
for name, value in parsedoption.__dict__.items():
|
|
|
|
setattr(option, name, value)
|
|
|
|
return args
|
|
|
|
|
2010-11-05 06:21:26 +08:00
|
|
|
def addini(self, name, help, type=None, default=None):
|
2010-10-31 01:23:50 +08:00
|
|
|
""" add an ini-file option with the given name and description. """
|
2010-11-06 06:37:31 +08:00
|
|
|
assert type in (None, "pathlist", "args", "linelist")
|
2010-11-05 06:21:26 +08:00
|
|
|
self._inidict[name] = (help, type, default)
|
2010-11-07 23:10:22 +08:00
|
|
|
self._ininames.append(name)
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
class OptionGroup:
|
|
|
|
def __init__(self, name, description="", parser=None):
|
|
|
|
self.name = name
|
|
|
|
self.description = description
|
|
|
|
self.options = []
|
|
|
|
self.parser = parser
|
|
|
|
|
|
|
|
def addoption(self, *optnames, **attrs):
|
|
|
|
""" add an option to this group. """
|
|
|
|
option = py.std.optparse.Option(*optnames, **attrs)
|
|
|
|
self._addoption_instance(option, shortupper=False)
|
|
|
|
|
|
|
|
def _addoption(self, *optnames, **attrs):
|
|
|
|
option = py.std.optparse.Option(*optnames, **attrs)
|
|
|
|
self._addoption_instance(option, shortupper=True)
|
|
|
|
|
|
|
|
def _addoption_instance(self, option, shortupper=False):
|
|
|
|
if not shortupper:
|
|
|
|
for opt in option._short_opts:
|
|
|
|
if opt[0] == '-' and opt[1].islower():
|
|
|
|
raise ValueError("lowercase shortoptions reserved")
|
|
|
|
if self.parser:
|
|
|
|
self.parser.processoption(option)
|
|
|
|
self.options.append(option)
|
|
|
|
|
|
|
|
|
|
|
|
class MyOptionParser(py.std.optparse.OptionParser):
|
|
|
|
def __init__(self, parser):
|
|
|
|
self._parser = parser
|
2010-10-28 04:29:01 +08:00
|
|
|
py.std.optparse.OptionParser.__init__(self, usage=parser._usage,
|
|
|
|
add_help_option=False)
|
2010-10-12 21:34:32 +08:00
|
|
|
def format_epilog(self, formatter):
|
|
|
|
hints = self._parser.hints
|
|
|
|
if hints:
|
|
|
|
s = "\n".join(["hint: " + x for x in hints]) + "\n"
|
|
|
|
s = "\n" + s + "\n"
|
|
|
|
return s
|
|
|
|
return ""
|
|
|
|
|
|
|
|
class Conftest(object):
|
|
|
|
""" the single place for accessing values and interacting
|
|
|
|
towards conftest modules from py.test objects.
|
|
|
|
"""
|
|
|
|
def __init__(self, onimport=None, confcutdir=None):
|
|
|
|
self._path2confmods = {}
|
|
|
|
self._onimport = onimport
|
|
|
|
self._conftestpath2mod = {}
|
|
|
|
self._confcutdir = confcutdir
|
|
|
|
self._md5cache = {}
|
|
|
|
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
for arg in args + [current]:
|
|
|
|
if hasattr(arg, 'startswith') and arg.startswith("--"):
|
|
|
|
continue
|
|
|
|
anchor = current.join(arg, abs=1)
|
|
|
|
if anchor.check(): # we found some file object
|
|
|
|
self._path2confmods[None] = self.getconftestmodules(anchor)
|
|
|
|
# let's also consider test* dirs
|
|
|
|
if anchor.check(dir=1):
|
|
|
|
for x in anchor.listdir("test*"):
|
|
|
|
if x.check(dir=1):
|
|
|
|
self.getconftestmodules(x)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
assert 0, "no root of filesystem?"
|
|
|
|
|
|
|
|
def getconftestmodules(self, path):
|
|
|
|
""" return a list of imported conftest modules for the given path. """
|
|
|
|
try:
|
|
|
|
clist = self._path2confmods[path]
|
|
|
|
except KeyError:
|
|
|
|
if path is None:
|
|
|
|
raise ValueError("missing default confest.")
|
|
|
|
dp = path.dirpath()
|
|
|
|
clist = []
|
|
|
|
if dp != path:
|
|
|
|
cutdir = self._confcutdir
|
|
|
|
if cutdir and path != cutdir and not path.relto(cutdir):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
conftestpath = path.join("conftest.py")
|
|
|
|
if conftestpath.check(file=1):
|
|
|
|
key = conftestpath.computehash()
|
|
|
|
# XXX logging about conftest loading
|
|
|
|
if key not in self._md5cache:
|
|
|
|
clist.append(self.importconftest(conftestpath))
|
|
|
|
self._md5cache[key] = conftestpath
|
|
|
|
else:
|
|
|
|
# use some kind of logging
|
|
|
|
print ("WARN: not loading %s" % conftestpath)
|
|
|
|
clist[:0] = self.getconftestmodules(dp)
|
|
|
|
self._path2confmods[path] = clist
|
|
|
|
# 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
|
|
|
|
raise KeyError(name)
|
|
|
|
|
|
|
|
def importconftest(self, conftestpath):
|
|
|
|
assert conftestpath.check(), conftestpath
|
|
|
|
try:
|
|
|
|
return self._conftestpath2mod[conftestpath]
|
|
|
|
except KeyError:
|
2010-10-14 00:45:07 +08:00
|
|
|
pkgpath = conftestpath.pypkgpath()
|
|
|
|
if pkgpath is None:
|
|
|
|
_ensure_removed_sysmodule(conftestpath.purebasename)
|
|
|
|
self._conftestpath2mod[conftestpath] = mod = conftestpath.pyimport()
|
2010-10-12 21:34: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)
|
|
|
|
return mod
|
|
|
|
|
|
|
|
def _postimport(self, mod):
|
|
|
|
if self._onimport:
|
|
|
|
self._onimport(mod)
|
|
|
|
return mod
|
|
|
|
|
2010-10-14 00:45:07 +08:00
|
|
|
def _ensure_removed_sysmodule(modname):
|
|
|
|
try:
|
|
|
|
del sys.modules[modname]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
class CmdOptions(object):
|
|
|
|
""" holds cmdline options as attributes."""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
def __repr__(self):
|
|
|
|
return "<CmdOptions %r>" %(self.__dict__,)
|
|
|
|
|
|
|
|
class Config(object):
|
|
|
|
""" access to configuration values, pluginmanager and plugin hooks. """
|
|
|
|
def __init__(self, pluginmanager=None):
|
2010-11-02 07:53:53 +08:00
|
|
|
#: command line option values, usually added via parser.addoption(...)
|
|
|
|
#: or parser.getgroup(...).addoption(...) calls
|
2010-10-12 21:34:32 +08:00
|
|
|
self.option = CmdOptions()
|
|
|
|
self._parser = Parser(
|
|
|
|
usage="usage: %prog [options] [file_or_dir] [file_or_dir] [...]",
|
|
|
|
processopt=self._processopt,
|
|
|
|
)
|
|
|
|
#: a pluginmanager instance
|
2010-10-13 17:12:27 +08:00
|
|
|
self.pluginmanager = pluginmanager or PluginManager(load=True)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace = self.pluginmanager.trace.root.get("config")
|
2010-10-12 21:34:32 +08:00
|
|
|
self._conftest = Conftest(onimport=self._onimportconftest)
|
|
|
|
self.hook = self.pluginmanager.hook
|
2010-11-25 05:01:04 +08:00
|
|
|
self._inicache = {}
|
2010-10-12 21:34:32 +08:00
|
|
|
|
|
|
|
def _onimportconftest(self, conftestmodule):
|
|
|
|
self.trace("loaded conftestmodule %r" %(conftestmodule,))
|
|
|
|
self.pluginmanager.consider_conftest(conftestmodule)
|
|
|
|
|
2010-10-28 04:29:01 +08:00
|
|
|
def _processopt(self, opt):
|
|
|
|
if hasattr(opt, 'default') and opt.dest:
|
|
|
|
if not hasattr(self.option, opt.dest):
|
|
|
|
setattr(self.option, opt.dest, opt.default)
|
|
|
|
|
2010-10-12 21:34:32 +08:00
|
|
|
def _getmatchingplugins(self, fspath):
|
|
|
|
allconftests = self._conftest._conftestpath2mod.values()
|
|
|
|
plugins = [x for x in self.pluginmanager.getplugins()
|
|
|
|
if x not in allconftests]
|
|
|
|
plugins += self._conftest.getconftestmodules(fspath)
|
|
|
|
return plugins
|
|
|
|
|
2010-10-25 05:26:14 +08:00
|
|
|
def _setinitialconftest(self, args):
|
|
|
|
# capture output during conftest init (#issue93)
|
|
|
|
name = hasattr(os, 'dup') and 'StdCaptureFD' or 'StdCapture'
|
|
|
|
cap = getattr(py.io, name)()
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self._conftest.setinitial(args)
|
|
|
|
finally:
|
|
|
|
out, err = cap.reset()
|
|
|
|
except:
|
|
|
|
sys.stdout.write(out)
|
|
|
|
sys.stderr.write(err)
|
|
|
|
raise
|
|
|
|
|
2010-11-05 06:21:26 +08:00
|
|
|
def _initini(self, args):
|
2010-11-07 23:10:22 +08:00
|
|
|
self.inicfg = getcfg(args, ["pytest.ini", "tox.ini", "setup.cfg"])
|
2010-11-05 06:21:26 +08:00
|
|
|
self._parser.addini('addopts', 'extra command line options', 'args')
|
|
|
|
self._parser.addini('minversion', 'minimally required pytest version')
|
|
|
|
|
|
|
|
def _preparse(self, args, addopts=True):
|
|
|
|
self._initini(args)
|
|
|
|
if addopts:
|
|
|
|
args[:] = self.getini("addopts") + args
|
2010-10-28 01:35:27 +08:00
|
|
|
self._checkversion()
|
2010-12-06 23:54:42 +08:00
|
|
|
self.pluginmanager.consider_preparse(args)
|
2010-10-12 21:34:32 +08:00
|
|
|
self.pluginmanager.consider_setuptools_entrypoints()
|
|
|
|
self.pluginmanager.consider_env()
|
2010-10-25 05:26:14 +08:00
|
|
|
self._setinitialconftest(args)
|
2010-11-07 23:10:22 +08:00
|
|
|
self.pluginmanager.do_addoption(self._parser)
|
2010-12-07 19:14:12 +08:00
|
|
|
if addopts:
|
|
|
|
self.hook.pytest_cmdline_processargs(config=self, args=args)
|
2010-10-12 21:34:32 +08:00
|
|
|
|
2010-10-28 01:35:27 +08:00
|
|
|
def _checkversion(self):
|
|
|
|
minver = self.inicfg.get('minversion', None)
|
|
|
|
if minver:
|
|
|
|
ver = minver.split(".")
|
|
|
|
myver = pytest.__version__.split(".")
|
|
|
|
if myver < ver:
|
|
|
|
raise pytest.UsageError(
|
|
|
|
"%s:%d: requires pytest-%s, actual pytest-%s'" %(
|
|
|
|
self.inicfg.config.path, self.inicfg.lineof('minversion'),
|
|
|
|
minver, pytest.__version__))
|
|
|
|
|
2010-10-12 21:34:32 +08:00
|
|
|
def parse(self, args):
|
2010-10-31 01:23:50 +08:00
|
|
|
# parse given cmdline arguments into this config object.
|
2010-10-12 21:34:32 +08:00
|
|
|
# Note that this can only be called once per testing process.
|
|
|
|
assert not hasattr(self, 'args'), (
|
|
|
|
"can only parse cmdline args at most once per Config object")
|
|
|
|
self._preparse(args)
|
|
|
|
self._parser.hints.extend(self.pluginmanager._hints)
|
|
|
|
args = self._parser.parse_setoption(args, self.option)
|
|
|
|
if not args:
|
|
|
|
args.append(py.std.os.getcwd())
|
|
|
|
self.args = args
|
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
def getini(self, name):
|
2010-11-01 01:01:33 +08:00
|
|
|
""" return configuration value from an ini file. If the
|
|
|
|
specified name hasn't been registered through a prior ``parse.addini``
|
|
|
|
call (usually from a plugin), a ValueError is raised. """
|
2010-11-25 05:01:04 +08:00
|
|
|
try:
|
|
|
|
return self._inicache[name]
|
|
|
|
except KeyError:
|
|
|
|
self._inicache[name] = val = self._getini(name)
|
|
|
|
return val
|
|
|
|
|
|
|
|
def _getini(self, name):
|
2010-11-01 00:41:58 +08:00
|
|
|
try:
|
2010-11-05 06:21:26 +08:00
|
|
|
description, type, default = self._parser._inidict[name]
|
2010-11-01 00:41:58 +08:00
|
|
|
except KeyError:
|
|
|
|
raise ValueError("unknown configuration value: %r" %(name,))
|
|
|
|
try:
|
|
|
|
value = self.inicfg[name]
|
|
|
|
except KeyError:
|
2010-11-05 06:21:26 +08:00
|
|
|
if default is not None:
|
|
|
|
return default
|
2010-11-06 06:37:31 +08:00
|
|
|
if type is None:
|
|
|
|
return ''
|
|
|
|
return []
|
2010-11-01 00:41:58 +08:00
|
|
|
if type == "pathlist":
|
|
|
|
dp = py.path.local(self.inicfg.config.path).dirpath()
|
|
|
|
l = []
|
|
|
|
for relpath in py.std.shlex.split(value):
|
|
|
|
l.append(dp.join(relpath, abs=True))
|
|
|
|
return l
|
2010-11-05 06:21:26 +08:00
|
|
|
elif type == "args":
|
|
|
|
return py.std.shlex.split(value)
|
2010-11-06 06:37:31 +08:00
|
|
|
elif type == "linelist":
|
2010-11-07 07:34:00 +08:00
|
|
|
return [t for t in map(lambda x: x.strip(), value.split("\n")) if t]
|
2010-11-01 00:41:58 +08:00
|
|
|
else:
|
2010-11-05 06:21:26 +08:00
|
|
|
assert type is None
|
2010-11-01 00:41:58 +08:00
|
|
|
return value
|
|
|
|
|
|
|
|
def _getconftest_pathlist(self, name, path=None):
|
2010-10-12 21:34:32 +08:00
|
|
|
try:
|
|
|
|
mod, relroots = self._conftest.rget_with_confmod(name, path)
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
modpath = py.path.local(mod.__file__).dirpath()
|
|
|
|
l = []
|
|
|
|
for relroot in relroots:
|
|
|
|
if not isinstance(relroot, py.path.local):
|
|
|
|
relroot = relroot.replace("/", py.path.local.sep)
|
|
|
|
relroot = modpath.join(relroot, abs=True)
|
|
|
|
l.append(relroot)
|
|
|
|
return l
|
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
def _getconftest(self, name, path=None, check=False):
|
|
|
|
if check:
|
|
|
|
self._checkconftest(name)
|
|
|
|
return self._conftest.rget(name, path)
|
|
|
|
|
|
|
|
def getvalue(self, name, path=None):
|
2010-11-01 01:01:33 +08:00
|
|
|
""" return ``name`` value looked set from command line options.
|
|
|
|
|
|
|
|
(deprecated) if we can't find the option also lookup
|
|
|
|
the name in a matching conftest file.
|
2010-11-01 00:41:58 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return getattr(self.option, name)
|
|
|
|
except AttributeError:
|
|
|
|
return self._getconftest(name, path, check=False)
|
|
|
|
|
2010-10-12 21:34:32 +08:00
|
|
|
def getvalueorskip(self, name, path=None):
|
2010-11-02 07:53:53 +08:00
|
|
|
""" (deprecated) return getvalue(name) or call py.test.skip if no value exists. """
|
2010-10-12 21:34:32 +08:00
|
|
|
try:
|
|
|
|
val = self.getvalue(name, path)
|
|
|
|
if val is None:
|
|
|
|
raise KeyError(name)
|
|
|
|
return val
|
|
|
|
except KeyError:
|
|
|
|
py.test.skip("no %r value found" %(name,))
|
|
|
|
|
|
|
|
|
2010-10-28 01:35:27 +08:00
|
|
|
def getcfg(args, inibasenames):
|
2010-11-01 15:16:10 +08:00
|
|
|
args = [x for x in args if str(x)[0] != "-"]
|
2010-10-28 01:35:27 +08:00
|
|
|
if not args:
|
|
|
|
args = [py.path.local()]
|
2010-11-07 23:10:22 +08:00
|
|
|
for arg in args:
|
|
|
|
arg = py.path.local(arg)
|
2010-11-22 18:59:56 +08:00
|
|
|
for base in arg.parts(reverse=True):
|
|
|
|
for inibasename in inibasenames:
|
|
|
|
p = base.join(inibasename)
|
|
|
|
if p.check():
|
|
|
|
iniconfig = py.iniconfig.IniConfig(p)
|
|
|
|
if 'pytest' in iniconfig.sections:
|
|
|
|
return iniconfig['pytest']
|
2010-10-28 01:35:27 +08:00
|
|
|
return {}
|
|
|
|
|
|
|
|
def findupwards(current, basename):
|
|
|
|
current = py.path.local(current)
|
|
|
|
while 1:
|
|
|
|
p = current.join(basename)
|
|
|
|
if p.check():
|
|
|
|
return p
|
|
|
|
p = current.dirpath()
|
|
|
|
if p == current:
|
|
|
|
return
|
|
|
|
current = p
|
|
|
|
|