Merge pull request #3535 from RonnyPfannschmidt/config-split
begin to turn config into package
This commit is contained in:
commit
b285078db4
|
@ -733,7 +733,7 @@ Node
|
||||||
Parser
|
Parser
|
||||||
~~~~~~
|
~~~~~~
|
||||||
|
|
||||||
.. autoclass:: _pytest.config.Parser()
|
.. autoclass:: _pytest.config.argparsing.Parser()
|
||||||
:members:
|
:members:
|
||||||
|
|
||||||
PluginManager
|
PluginManager
|
||||||
|
|
8
setup.py
8
setup.py
|
@ -108,7 +108,13 @@ def main():
|
||||||
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
|
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
|
||||||
install_requires=install_requires,
|
install_requires=install_requires,
|
||||||
extras_require=extras_require,
|
extras_require=extras_require,
|
||||||
packages=["_pytest", "_pytest.assertion", "_pytest._code", "_pytest.mark"],
|
packages=[
|
||||||
|
"_pytest",
|
||||||
|
"_pytest.assertion",
|
||||||
|
"_pytest._code",
|
||||||
|
"_pytest.mark",
|
||||||
|
"_pytest.config",
|
||||||
|
],
|
||||||
py_modules=["pytest"],
|
py_modules=["pytest"],
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
)
|
)
|
||||||
|
|
|
@ -19,6 +19,8 @@ import _pytest.hookspec # the extension point definitions
|
||||||
import _pytest.assertion
|
import _pytest.assertion
|
||||||
from pluggy import PluginManager, HookimplMarker, HookspecMarker
|
from pluggy import PluginManager, HookimplMarker, HookspecMarker
|
||||||
from _pytest.compat import safe_str
|
from _pytest.compat import safe_str
|
||||||
|
from .exceptions import UsageError, PrintHelp
|
||||||
|
from .findpaths import determine_setup, exists
|
||||||
|
|
||||||
hookimpl = HookimplMarker("pytest")
|
hookimpl = HookimplMarker("pytest")
|
||||||
hookspec = HookspecMarker("pytest")
|
hookspec = HookspecMarker("pytest")
|
||||||
|
@ -74,16 +76,6 @@ class cmdline(object): # NOQA compatibility namespace
|
||||||
main = staticmethod(main)
|
main = staticmethod(main)
|
||||||
|
|
||||||
|
|
||||||
class UsageError(Exception):
|
|
||||||
""" error in pytest usage or invocation"""
|
|
||||||
|
|
||||||
|
|
||||||
class PrintHelp(Exception):
|
|
||||||
"""Raised when pytest should print it's help to skip the rest of the
|
|
||||||
argument parsing and validation."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def filename_arg(path, optname):
|
def filename_arg(path, optname):
|
||||||
""" Argparse type validator for filename arguments.
|
""" Argparse type validator for filename arguments.
|
||||||
|
|
||||||
|
@ -107,11 +99,33 @@ def directory_arg(path, optname):
|
||||||
|
|
||||||
|
|
||||||
default_plugins = (
|
default_plugins = (
|
||||||
"mark main terminal runner python fixtures debugging unittest capture skipping "
|
"mark",
|
||||||
"tmpdir monkeypatch recwarn pastebin helpconfig nose assertion "
|
"main",
|
||||||
"junitxml resultlog doctest cacheprovider freeze_support "
|
"terminal",
|
||||||
"setuponly setupplan warnings logging"
|
"runner",
|
||||||
).split()
|
"python",
|
||||||
|
"fixtures",
|
||||||
|
"debugging",
|
||||||
|
"unittest",
|
||||||
|
"capture",
|
||||||
|
"skipping",
|
||||||
|
"tmpdir",
|
||||||
|
"monkeypatch",
|
||||||
|
"recwarn",
|
||||||
|
"pastebin",
|
||||||
|
"helpconfig",
|
||||||
|
"nose",
|
||||||
|
"assertion",
|
||||||
|
"junitxml",
|
||||||
|
"resultlog",
|
||||||
|
"doctest",
|
||||||
|
"cacheprovider",
|
||||||
|
"freeze_support",
|
||||||
|
"setuponly",
|
||||||
|
"setupplan",
|
||||||
|
"warnings",
|
||||||
|
"logging",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
builtin_plugins = set(default_plugins)
|
builtin_plugins = set(default_plugins)
|
||||||
|
@ -496,395 +510,6 @@ def _get_plugin_specs_as_list(specs):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
class Parser(object):
|
|
||||||
""" Parser for command line arguments and ini-file values.
|
|
||||||
|
|
||||||
:ivar extra_info: dict of generic param -> value to display in case
|
|
||||||
there's an error processing the 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
|
|
||||||
self._inidict = {}
|
|
||||||
self._ininames = []
|
|
||||||
self.extra_info = {}
|
|
||||||
|
|
||||||
def processoption(self, option):
|
|
||||||
if self._processopt:
|
|
||||||
if option.dest:
|
|
||||||
self._processopt(option)
|
|
||||||
|
|
||||||
def getgroup(self, name, description="", after=None):
|
|
||||||
""" get (or create) a named option Group.
|
|
||||||
|
|
||||||
:name: name of the option group.
|
|
||||||
:description: long description for --help output.
|
|
||||||
:after: name of other group, used for ordering --help output.
|
|
||||||
|
|
||||||
The returned group object has an ``addoption`` method with the same
|
|
||||||
signature as :py:func:`parser.addoption
|
|
||||||
<_pytest.config.Parser.addoption>` but will be shown in the
|
|
||||||
respective group in the output of ``pytest. --help``.
|
|
||||||
"""
|
|
||||||
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):
|
|
||||||
""" register a command line option.
|
|
||||||
|
|
||||||
:opts: option names, can be short or long options.
|
|
||||||
:attrs: same attributes which the ``add_option()`` function of the
|
|
||||||
`argparse library
|
|
||||||
<http://docs.python.org/2/library/argparse.html>`_
|
|
||||||
accepts.
|
|
||||||
|
|
||||||
After command line parsing options are available on the pytest config
|
|
||||||
object via ``config.option.NAME`` where ``NAME`` is usually set
|
|
||||||
by passing a ``dest`` attribute, for example
|
|
||||||
``addoption("--long", dest="NAME", ...)``.
|
|
||||||
"""
|
|
||||||
self._anonymous.addoption(*opts, **attrs)
|
|
||||||
|
|
||||||
def parse(self, args, namespace=None):
|
|
||||||
from _pytest._argcomplete import try_argcomplete
|
|
||||||
|
|
||||||
self.optparser = self._getparser()
|
|
||||||
try_argcomplete(self.optparser)
|
|
||||||
return self.optparser.parse_args([str(x) for x in args], namespace=namespace)
|
|
||||||
|
|
||||||
def _getparser(self):
|
|
||||||
from _pytest._argcomplete import filescompleter
|
|
||||||
|
|
||||||
optparser = MyOptionParser(self, self.extra_info)
|
|
||||||
groups = self._groups + [self._anonymous]
|
|
||||||
for group in groups:
|
|
||||||
if group.options:
|
|
||||||
desc = group.description or group.name
|
|
||||||
arggroup = optparser.add_argument_group(desc)
|
|
||||||
for option in group.options:
|
|
||||||
n = option.names()
|
|
||||||
a = option.attrs()
|
|
||||||
arggroup.add_argument(*n, **a)
|
|
||||||
# bash like autocompletion for dirs (appending '/')
|
|
||||||
optparser.add_argument(FILE_OR_DIR, nargs="*").completer = filescompleter
|
|
||||||
return optparser
|
|
||||||
|
|
||||||
def parse_setoption(self, args, option, namespace=None):
|
|
||||||
parsedoption = self.parse(args, namespace=namespace)
|
|
||||||
for name, value in parsedoption.__dict__.items():
|
|
||||||
setattr(option, name, value)
|
|
||||||
return getattr(parsedoption, FILE_OR_DIR)
|
|
||||||
|
|
||||||
def parse_known_args(self, args, namespace=None):
|
|
||||||
"""parses and returns a namespace object with known arguments at this
|
|
||||||
point.
|
|
||||||
"""
|
|
||||||
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
|
|
||||||
|
|
||||||
def parse_known_and_unknown_args(self, args, namespace=None):
|
|
||||||
"""parses and returns a namespace object with known arguments, and
|
|
||||||
the remaining arguments unknown at this point.
|
|
||||||
"""
|
|
||||||
optparser = self._getparser()
|
|
||||||
args = [str(x) for x in args]
|
|
||||||
return optparser.parse_known_args(args, namespace=namespace)
|
|
||||||
|
|
||||||
def addini(self, name, help, type=None, default=None):
|
|
||||||
""" register an ini-file option.
|
|
||||||
|
|
||||||
:name: name of the ini-variable
|
|
||||||
:type: type of the variable, can be ``pathlist``, ``args``, ``linelist``
|
|
||||||
or ``bool``.
|
|
||||||
:default: default value if no ini-file option exists but is queried.
|
|
||||||
|
|
||||||
The value of ini-variables can be retrieved via a call to
|
|
||||||
:py:func:`config.getini(name) <_pytest.config.Config.getini>`.
|
|
||||||
"""
|
|
||||||
assert type in (None, "pathlist", "args", "linelist", "bool")
|
|
||||||
self._inidict[name] = (help, type, default)
|
|
||||||
self._ininames.append(name)
|
|
||||||
|
|
||||||
|
|
||||||
class ArgumentError(Exception):
|
|
||||||
"""
|
|
||||||
Raised if an Argument instance is created with invalid or
|
|
||||||
inconsistent arguments.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, msg, option):
|
|
||||||
self.msg = msg
|
|
||||||
self.option_id = str(option)
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
if self.option_id:
|
|
||||||
return "option %s: %s" % (self.option_id, self.msg)
|
|
||||||
else:
|
|
||||||
return self.msg
|
|
||||||
|
|
||||||
|
|
||||||
class Argument(object):
|
|
||||||
"""class that mimics the necessary behaviour of optparse.Option
|
|
||||||
|
|
||||||
its currently a least effort implementation
|
|
||||||
and ignoring choices and integer prefixes
|
|
||||||
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
|
|
||||||
"""
|
|
||||||
_typ_map = {"int": int, "string": str, "float": float, "complex": complex}
|
|
||||||
|
|
||||||
def __init__(self, *names, **attrs):
|
|
||||||
"""store parms in private vars for use in add_argument"""
|
|
||||||
self._attrs = attrs
|
|
||||||
self._short_opts = []
|
|
||||||
self._long_opts = []
|
|
||||||
self.dest = attrs.get("dest")
|
|
||||||
if "%default" in (attrs.get("help") or ""):
|
|
||||||
warnings.warn(
|
|
||||||
'pytest now uses argparse. "%default" should be'
|
|
||||||
' changed to "%(default)s" ',
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=3,
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
typ = attrs["type"]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# this might raise a keyerror as well, don't want to catch that
|
|
||||||
if isinstance(typ, six.string_types):
|
|
||||||
if typ == "choice":
|
|
||||||
warnings.warn(
|
|
||||||
"type argument to addoption() is a string %r."
|
|
||||||
" For parsearg this is optional and when supplied"
|
|
||||||
" should be a type."
|
|
||||||
" (options: %s)" % (typ, names),
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=3,
|
|
||||||
)
|
|
||||||
# argparse expects a type here take it from
|
|
||||||
# the type of the first element
|
|
||||||
attrs["type"] = type(attrs["choices"][0])
|
|
||||||
else:
|
|
||||||
warnings.warn(
|
|
||||||
"type argument to addoption() is a string %r."
|
|
||||||
" For parsearg this should be a type."
|
|
||||||
" (options: %s)" % (typ, names),
|
|
||||||
DeprecationWarning,
|
|
||||||
stacklevel=3,
|
|
||||||
)
|
|
||||||
attrs["type"] = Argument._typ_map[typ]
|
|
||||||
# used in test_parseopt -> test_parse_defaultgetter
|
|
||||||
self.type = attrs["type"]
|
|
||||||
else:
|
|
||||||
self.type = typ
|
|
||||||
try:
|
|
||||||
# attribute existence is tested in Config._processopt
|
|
||||||
self.default = attrs["default"]
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
self._set_opt_strings(names)
|
|
||||||
if not self.dest:
|
|
||||||
if self._long_opts:
|
|
||||||
self.dest = self._long_opts[0][2:].replace("-", "_")
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self.dest = self._short_opts[0][1:]
|
|
||||||
except IndexError:
|
|
||||||
raise ArgumentError("need a long or short option", self)
|
|
||||||
|
|
||||||
def names(self):
|
|
||||||
return self._short_opts + self._long_opts
|
|
||||||
|
|
||||||
def attrs(self):
|
|
||||||
# update any attributes set by processopt
|
|
||||||
attrs = "default dest help".split()
|
|
||||||
if self.dest:
|
|
||||||
attrs.append(self.dest)
|
|
||||||
for attr in attrs:
|
|
||||||
try:
|
|
||||||
self._attrs[attr] = getattr(self, attr)
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
if self._attrs.get("help"):
|
|
||||||
a = self._attrs["help"]
|
|
||||||
a = a.replace("%default", "%(default)s")
|
|
||||||
# a = a.replace('%prog', '%(prog)s')
|
|
||||||
self._attrs["help"] = a
|
|
||||||
return self._attrs
|
|
||||||
|
|
||||||
def _set_opt_strings(self, opts):
|
|
||||||
"""directly from optparse
|
|
||||||
|
|
||||||
might not be necessary as this is passed to argparse later on"""
|
|
||||||
for opt in opts:
|
|
||||||
if len(opt) < 2:
|
|
||||||
raise ArgumentError(
|
|
||||||
"invalid option string %r: "
|
|
||||||
"must be at least two characters long" % opt,
|
|
||||||
self,
|
|
||||||
)
|
|
||||||
elif len(opt) == 2:
|
|
||||||
if not (opt[0] == "-" and opt[1] != "-"):
|
|
||||||
raise ArgumentError(
|
|
||||||
"invalid short option string %r: "
|
|
||||||
"must be of the form -x, (x any non-dash char)" % opt,
|
|
||||||
self,
|
|
||||||
)
|
|
||||||
self._short_opts.append(opt)
|
|
||||||
else:
|
|
||||||
if not (opt[0:2] == "--" and opt[2] != "-"):
|
|
||||||
raise ArgumentError(
|
|
||||||
"invalid long option string %r: "
|
|
||||||
"must start with --, followed by non-dash" % opt,
|
|
||||||
self,
|
|
||||||
)
|
|
||||||
self._long_opts.append(opt)
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
args = []
|
|
||||||
if self._short_opts:
|
|
||||||
args += ["_short_opts: " + repr(self._short_opts)]
|
|
||||||
if self._long_opts:
|
|
||||||
args += ["_long_opts: " + repr(self._long_opts)]
|
|
||||||
args += ["dest: " + repr(self.dest)]
|
|
||||||
if hasattr(self, "type"):
|
|
||||||
args += ["type: " + repr(self.type)]
|
|
||||||
if hasattr(self, "default"):
|
|
||||||
args += ["default: " + repr(self.default)]
|
|
||||||
return "Argument({})".format(", ".join(args))
|
|
||||||
|
|
||||||
|
|
||||||
class OptionGroup(object):
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
if a shortened version of a long option is specified it will
|
|
||||||
be suppressed in the help. addoption('--twowords', '--two-words')
|
|
||||||
results in help showing '--two-words' only, but --twowords gets
|
|
||||||
accepted **and** the automatic destination is in args.twowords
|
|
||||||
"""
|
|
||||||
conflict = set(optnames).intersection(
|
|
||||||
name for opt in self.options for name in opt.names()
|
|
||||||
)
|
|
||||||
if conflict:
|
|
||||||
raise ValueError("option names %s already added" % conflict)
|
|
||||||
option = Argument(*optnames, **attrs)
|
|
||||||
self._addoption_instance(option, shortupper=False)
|
|
||||||
|
|
||||||
def _addoption(self, *optnames, **attrs):
|
|
||||||
option = Argument(*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(argparse.ArgumentParser):
|
|
||||||
|
|
||||||
def __init__(self, parser, extra_info=None):
|
|
||||||
if not extra_info:
|
|
||||||
extra_info = {}
|
|
||||||
self._parser = parser
|
|
||||||
argparse.ArgumentParser.__init__(
|
|
||||||
self,
|
|
||||||
usage=parser._usage,
|
|
||||||
add_help=False,
|
|
||||||
formatter_class=DropShorterLongHelpFormatter,
|
|
||||||
)
|
|
||||||
# extra_info is a dict of (param -> value) to display if there's
|
|
||||||
# an usage error to provide more contextual information to the user
|
|
||||||
self.extra_info = extra_info
|
|
||||||
|
|
||||||
def parse_args(self, args=None, namespace=None):
|
|
||||||
"""allow splitting of positional arguments"""
|
|
||||||
args, argv = self.parse_known_args(args, namespace)
|
|
||||||
if argv:
|
|
||||||
for arg in argv:
|
|
||||||
if arg and arg[0] == "-":
|
|
||||||
lines = ["unrecognized arguments: %s" % (" ".join(argv))]
|
|
||||||
for k, v in sorted(self.extra_info.items()):
|
|
||||||
lines.append(" %s: %s" % (k, v))
|
|
||||||
self.error("\n".join(lines))
|
|
||||||
getattr(args, FILE_OR_DIR).extend(argv)
|
|
||||||
return args
|
|
||||||
|
|
||||||
|
|
||||||
class DropShorterLongHelpFormatter(argparse.HelpFormatter):
|
|
||||||
"""shorten help for long options that differ only in extra hyphens
|
|
||||||
|
|
||||||
- collapse **long** options that are the same except for extra hyphens
|
|
||||||
- special action attribute map_long_option allows surpressing additional
|
|
||||||
long options
|
|
||||||
- shortcut if there are only two options and one of them is a short one
|
|
||||||
- cache result on action object as this is called at least 2 times
|
|
||||||
"""
|
|
||||||
|
|
||||||
def _format_action_invocation(self, action):
|
|
||||||
orgstr = argparse.HelpFormatter._format_action_invocation(self, action)
|
|
||||||
if orgstr and orgstr[0] != "-": # only optional arguments
|
|
||||||
return orgstr
|
|
||||||
res = getattr(action, "_formatted_action_invocation", None)
|
|
||||||
if res:
|
|
||||||
return res
|
|
||||||
options = orgstr.split(", ")
|
|
||||||
if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2):
|
|
||||||
# a shortcut for '-h, --help' or '--abc', '-a'
|
|
||||||
action._formatted_action_invocation = orgstr
|
|
||||||
return orgstr
|
|
||||||
return_list = []
|
|
||||||
option_map = getattr(action, "map_long_option", {})
|
|
||||||
if option_map is None:
|
|
||||||
option_map = {}
|
|
||||||
short_long = {}
|
|
||||||
for option in options:
|
|
||||||
if len(option) == 2 or option[2] == " ":
|
|
||||||
continue
|
|
||||||
if not option.startswith("--"):
|
|
||||||
raise ArgumentError(
|
|
||||||
'long optional argument without "--": [%s]' % (option), self
|
|
||||||
)
|
|
||||||
xxoption = option[2:]
|
|
||||||
if xxoption.split()[0] not in option_map:
|
|
||||||
shortened = xxoption.replace("-", "")
|
|
||||||
if (
|
|
||||||
shortened not in short_long
|
|
||||||
or len(short_long[shortened]) < len(xxoption)
|
|
||||||
):
|
|
||||||
short_long[shortened] = xxoption
|
|
||||||
# now short_long has been filled out to the longest with dashes
|
|
||||||
# **and** we keep the right option ordering from add_argument
|
|
||||||
for option in options:
|
|
||||||
if len(option) == 2 or option[2] == " ":
|
|
||||||
return_list.append(option)
|
|
||||||
if option[2:] == short_long.get(option.replace("-", "")):
|
|
||||||
return_list.append(option.replace(" ", "=", 1))
|
|
||||||
action._formatted_action_invocation = ", ".join(return_list)
|
|
||||||
return action._formatted_action_invocation
|
|
||||||
|
|
||||||
|
|
||||||
def _ensure_removed_sysmodule(modname):
|
def _ensure_removed_sysmodule(modname):
|
||||||
try:
|
try:
|
||||||
del sys.modules[modname]
|
del sys.modules[modname]
|
||||||
|
@ -899,7 +524,6 @@ class Notset(object):
|
||||||
|
|
||||||
|
|
||||||
notset = Notset()
|
notset = Notset()
|
||||||
FILE_OR_DIR = "file_or_dir"
|
|
||||||
|
|
||||||
|
|
||||||
def _iter_rewritable_modules(package_files):
|
def _iter_rewritable_modules(package_files):
|
||||||
|
@ -921,6 +545,8 @@ class Config(object):
|
||||||
#: access to command line option as attributes.
|
#: access to command line option as attributes.
|
||||||
#: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead
|
#: (deprecated), use :py:func:`getoption() <_pytest.config.Config.getoption>` instead
|
||||||
self.option = argparse.Namespace()
|
self.option = argparse.Namespace()
|
||||||
|
from .argparsing import Parser, FILE_OR_DIR
|
||||||
|
|
||||||
_a = FILE_OR_DIR
|
_a = FILE_OR_DIR
|
||||||
self._parser = Parser(
|
self._parser = Parser(
|
||||||
usage="%%(prog)s [options] [%s] [%s] [...]" % (_a, _a),
|
usage="%%(prog)s [options] [%s] [%s] [...]" % (_a, _a),
|
||||||
|
@ -1300,143 +926,6 @@ def _warn_about_missing_assertion(mode):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def exists(path, ignore=EnvironmentError):
|
|
||||||
try:
|
|
||||||
return path.check()
|
|
||||||
except ignore:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def getcfg(args, warnfunc=None):
|
|
||||||
"""
|
|
||||||
Search the list of arguments for a valid ini-file for pytest,
|
|
||||||
and return a tuple of (rootdir, inifile, cfg-dict).
|
|
||||||
|
|
||||||
note: warnfunc is an optional function used to warn
|
|
||||||
about ini-files that use deprecated features.
|
|
||||||
This parameter should be removed when pytest
|
|
||||||
adopts standard deprecation warnings (#1804).
|
|
||||||
"""
|
|
||||||
from _pytest.deprecated import CFG_PYTEST_SECTION
|
|
||||||
|
|
||||||
inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"]
|
|
||||||
args = [x for x in args if not str(x).startswith("-")]
|
|
||||||
if not args:
|
|
||||||
args = [py.path.local()]
|
|
||||||
for arg in args:
|
|
||||||
arg = py.path.local(arg)
|
|
||||||
for base in arg.parts(reverse=True):
|
|
||||||
for inibasename in inibasenames:
|
|
||||||
p = base.join(inibasename)
|
|
||||||
if exists(p):
|
|
||||||
iniconfig = py.iniconfig.IniConfig(p)
|
|
||||||
if "pytest" in iniconfig.sections:
|
|
||||||
if inibasename == "setup.cfg" and warnfunc:
|
|
||||||
warnfunc(
|
|
||||||
"C1", CFG_PYTEST_SECTION.format(filename=inibasename)
|
|
||||||
)
|
|
||||||
return base, p, iniconfig["pytest"]
|
|
||||||
if (
|
|
||||||
inibasename == "setup.cfg"
|
|
||||||
and "tool:pytest" in iniconfig.sections
|
|
||||||
):
|
|
||||||
return base, p, iniconfig["tool:pytest"]
|
|
||||||
elif inibasename == "pytest.ini":
|
|
||||||
# allowed to be empty
|
|
||||||
return base, p, {}
|
|
||||||
return None, None, None
|
|
||||||
|
|
||||||
|
|
||||||
def get_common_ancestor(paths):
|
|
||||||
common_ancestor = None
|
|
||||||
for path in paths:
|
|
||||||
if not path.exists():
|
|
||||||
continue
|
|
||||||
if common_ancestor is None:
|
|
||||||
common_ancestor = path
|
|
||||||
else:
|
|
||||||
if path.relto(common_ancestor) or path == common_ancestor:
|
|
||||||
continue
|
|
||||||
elif common_ancestor.relto(path):
|
|
||||||
common_ancestor = path
|
|
||||||
else:
|
|
||||||
shared = path.common(common_ancestor)
|
|
||||||
if shared is not None:
|
|
||||||
common_ancestor = shared
|
|
||||||
if common_ancestor is None:
|
|
||||||
common_ancestor = py.path.local()
|
|
||||||
elif common_ancestor.isfile():
|
|
||||||
common_ancestor = common_ancestor.dirpath()
|
|
||||||
return common_ancestor
|
|
||||||
|
|
||||||
|
|
||||||
def get_dirs_from_args(args):
|
|
||||||
|
|
||||||
def is_option(x):
|
|
||||||
return str(x).startswith("-")
|
|
||||||
|
|
||||||
def get_file_part_from_node_id(x):
|
|
||||||
return str(x).split("::")[0]
|
|
||||||
|
|
||||||
def get_dir_from_path(path):
|
|
||||||
if path.isdir():
|
|
||||||
return path
|
|
||||||
return py.path.local(path.dirname)
|
|
||||||
|
|
||||||
# These look like paths but may not exist
|
|
||||||
possible_paths = (
|
|
||||||
py.path.local(get_file_part_from_node_id(arg))
|
|
||||||
for arg in args
|
|
||||||
if not is_option(arg)
|
|
||||||
)
|
|
||||||
|
|
||||||
return [get_dir_from_path(path) for path in possible_paths if path.exists()]
|
|
||||||
|
|
||||||
|
|
||||||
def determine_setup(inifile, args, warnfunc=None, rootdir_cmd_arg=None):
|
|
||||||
dirs = get_dirs_from_args(args)
|
|
||||||
if inifile:
|
|
||||||
iniconfig = py.iniconfig.IniConfig(inifile)
|
|
||||||
is_cfg_file = str(inifile).endswith(".cfg")
|
|
||||||
# TODO: [pytest] section in *.cfg files is depricated. Need refactoring.
|
|
||||||
sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"]
|
|
||||||
for section in sections:
|
|
||||||
try:
|
|
||||||
inicfg = iniconfig[section]
|
|
||||||
if is_cfg_file and section == "pytest" and warnfunc:
|
|
||||||
from _pytest.deprecated import CFG_PYTEST_SECTION
|
|
||||||
|
|
||||||
warnfunc("C1", CFG_PYTEST_SECTION.format(filename=str(inifile)))
|
|
||||||
break
|
|
||||||
except KeyError:
|
|
||||||
inicfg = None
|
|
||||||
rootdir = get_common_ancestor(dirs)
|
|
||||||
else:
|
|
||||||
ancestor = get_common_ancestor(dirs)
|
|
||||||
rootdir, inifile, inicfg = getcfg([ancestor], warnfunc=warnfunc)
|
|
||||||
if rootdir is None:
|
|
||||||
for rootdir in ancestor.parts(reverse=True):
|
|
||||||
if rootdir.join("setup.py").exists():
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
rootdir, inifile, inicfg = getcfg(dirs, warnfunc=warnfunc)
|
|
||||||
if rootdir is None:
|
|
||||||
rootdir = get_common_ancestor([py.path.local(), ancestor])
|
|
||||||
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
|
|
||||||
if is_fs_root:
|
|
||||||
rootdir = ancestor
|
|
||||||
if rootdir_cmd_arg:
|
|
||||||
rootdir_abs_path = py.path.local(os.path.expandvars(rootdir_cmd_arg))
|
|
||||||
if not os.path.isdir(str(rootdir_abs_path)):
|
|
||||||
raise UsageError(
|
|
||||||
"Directory '{}' not found. Check your '--rootdir' option.".format(
|
|
||||||
rootdir_abs_path
|
|
||||||
)
|
|
||||||
)
|
|
||||||
rootdir = rootdir_abs_path
|
|
||||||
return rootdir, inifile, inicfg or {}
|
|
||||||
|
|
||||||
|
|
||||||
def setns(obj, dic):
|
def setns(obj, dic):
|
||||||
import pytest
|
import pytest
|
||||||
|
|
|
@ -0,0 +1,394 @@
|
||||||
|
import six
|
||||||
|
import warnings
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
FILE_OR_DIR = "file_or_dir"
|
||||||
|
|
||||||
|
|
||||||
|
class Parser(object):
|
||||||
|
""" Parser for command line arguments and ini-file values.
|
||||||
|
|
||||||
|
:ivar extra_info: dict of generic param -> value to display in case
|
||||||
|
there's an error processing the 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
|
||||||
|
self._inidict = {}
|
||||||
|
self._ininames = []
|
||||||
|
self.extra_info = {}
|
||||||
|
|
||||||
|
def processoption(self, option):
|
||||||
|
if self._processopt:
|
||||||
|
if option.dest:
|
||||||
|
self._processopt(option)
|
||||||
|
|
||||||
|
def getgroup(self, name, description="", after=None):
|
||||||
|
""" get (or create) a named option Group.
|
||||||
|
|
||||||
|
:name: name of the option group.
|
||||||
|
:description: long description for --help output.
|
||||||
|
:after: name of other group, used for ordering --help output.
|
||||||
|
|
||||||
|
The returned group object has an ``addoption`` method with the same
|
||||||
|
signature as :py:func:`parser.addoption
|
||||||
|
<_pytest.config.Parser.addoption>` but will be shown in the
|
||||||
|
respective group in the output of ``pytest. --help``.
|
||||||
|
"""
|
||||||
|
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):
|
||||||
|
""" register a command line option.
|
||||||
|
|
||||||
|
:opts: option names, can be short or long options.
|
||||||
|
:attrs: same attributes which the ``add_option()`` function of the
|
||||||
|
`argparse library
|
||||||
|
<http://docs.python.org/2/library/argparse.html>`_
|
||||||
|
accepts.
|
||||||
|
|
||||||
|
After command line parsing options are available on the pytest config
|
||||||
|
object via ``config.option.NAME`` where ``NAME`` is usually set
|
||||||
|
by passing a ``dest`` attribute, for example
|
||||||
|
``addoption("--long", dest="NAME", ...)``.
|
||||||
|
"""
|
||||||
|
self._anonymous.addoption(*opts, **attrs)
|
||||||
|
|
||||||
|
def parse(self, args, namespace=None):
|
||||||
|
from _pytest._argcomplete import try_argcomplete
|
||||||
|
|
||||||
|
self.optparser = self._getparser()
|
||||||
|
try_argcomplete(self.optparser)
|
||||||
|
return self.optparser.parse_args([str(x) for x in args], namespace=namespace)
|
||||||
|
|
||||||
|
def _getparser(self):
|
||||||
|
from _pytest._argcomplete import filescompleter
|
||||||
|
|
||||||
|
optparser = MyOptionParser(self, self.extra_info)
|
||||||
|
groups = self._groups + [self._anonymous]
|
||||||
|
for group in groups:
|
||||||
|
if group.options:
|
||||||
|
desc = group.description or group.name
|
||||||
|
arggroup = optparser.add_argument_group(desc)
|
||||||
|
for option in group.options:
|
||||||
|
n = option.names()
|
||||||
|
a = option.attrs()
|
||||||
|
arggroup.add_argument(*n, **a)
|
||||||
|
# bash like autocompletion for dirs (appending '/')
|
||||||
|
optparser.add_argument(FILE_OR_DIR, nargs="*").completer = filescompleter
|
||||||
|
return optparser
|
||||||
|
|
||||||
|
def parse_setoption(self, args, option, namespace=None):
|
||||||
|
parsedoption = self.parse(args, namespace=namespace)
|
||||||
|
for name, value in parsedoption.__dict__.items():
|
||||||
|
setattr(option, name, value)
|
||||||
|
return getattr(parsedoption, FILE_OR_DIR)
|
||||||
|
|
||||||
|
def parse_known_args(self, args, namespace=None):
|
||||||
|
"""parses and returns a namespace object with known arguments at this
|
||||||
|
point.
|
||||||
|
"""
|
||||||
|
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
|
||||||
|
|
||||||
|
def parse_known_and_unknown_args(self, args, namespace=None):
|
||||||
|
"""parses and returns a namespace object with known arguments, and
|
||||||
|
the remaining arguments unknown at this point.
|
||||||
|
"""
|
||||||
|
optparser = self._getparser()
|
||||||
|
args = [str(x) for x in args]
|
||||||
|
return optparser.parse_known_args(args, namespace=namespace)
|
||||||
|
|
||||||
|
def addini(self, name, help, type=None, default=None):
|
||||||
|
""" register an ini-file option.
|
||||||
|
|
||||||
|
:name: name of the ini-variable
|
||||||
|
:type: type of the variable, can be ``pathlist``, ``args``, ``linelist``
|
||||||
|
or ``bool``.
|
||||||
|
:default: default value if no ini-file option exists but is queried.
|
||||||
|
|
||||||
|
The value of ini-variables can be retrieved via a call to
|
||||||
|
:py:func:`config.getini(name) <_pytest.config.Config.getini>`.
|
||||||
|
"""
|
||||||
|
assert type in (None, "pathlist", "args", "linelist", "bool")
|
||||||
|
self._inidict[name] = (help, type, default)
|
||||||
|
self._ininames.append(name)
|
||||||
|
|
||||||
|
|
||||||
|
class ArgumentError(Exception):
|
||||||
|
"""
|
||||||
|
Raised if an Argument instance is created with invalid or
|
||||||
|
inconsistent arguments.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, msg, option):
|
||||||
|
self.msg = msg
|
||||||
|
self.option_id = str(option)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
if self.option_id:
|
||||||
|
return "option %s: %s" % (self.option_id, self.msg)
|
||||||
|
else:
|
||||||
|
return self.msg
|
||||||
|
|
||||||
|
|
||||||
|
class Argument(object):
|
||||||
|
"""class that mimics the necessary behaviour of optparse.Option
|
||||||
|
|
||||||
|
its currently a least effort implementation
|
||||||
|
and ignoring choices and integer prefixes
|
||||||
|
https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
|
||||||
|
"""
|
||||||
|
_typ_map = {"int": int, "string": str, "float": float, "complex": complex}
|
||||||
|
|
||||||
|
def __init__(self, *names, **attrs):
|
||||||
|
"""store parms in private vars for use in add_argument"""
|
||||||
|
self._attrs = attrs
|
||||||
|
self._short_opts = []
|
||||||
|
self._long_opts = []
|
||||||
|
self.dest = attrs.get("dest")
|
||||||
|
if "%default" in (attrs.get("help") or ""):
|
||||||
|
warnings.warn(
|
||||||
|
'pytest now uses argparse. "%default" should be'
|
||||||
|
' changed to "%(default)s" ',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=3,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
typ = attrs["type"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
# this might raise a keyerror as well, don't want to catch that
|
||||||
|
if isinstance(typ, six.string_types):
|
||||||
|
if typ == "choice":
|
||||||
|
warnings.warn(
|
||||||
|
"type argument to addoption() is a string %r."
|
||||||
|
" For parsearg this is optional and when supplied"
|
||||||
|
" should be a type."
|
||||||
|
" (options: %s)" % (typ, names),
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=3,
|
||||||
|
)
|
||||||
|
# argparse expects a type here take it from
|
||||||
|
# the type of the first element
|
||||||
|
attrs["type"] = type(attrs["choices"][0])
|
||||||
|
else:
|
||||||
|
warnings.warn(
|
||||||
|
"type argument to addoption() is a string %r."
|
||||||
|
" For parsearg this should be a type."
|
||||||
|
" (options: %s)" % (typ, names),
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=3,
|
||||||
|
)
|
||||||
|
attrs["type"] = Argument._typ_map[typ]
|
||||||
|
# used in test_parseopt -> test_parse_defaultgetter
|
||||||
|
self.type = attrs["type"]
|
||||||
|
else:
|
||||||
|
self.type = typ
|
||||||
|
try:
|
||||||
|
# attribute existence is tested in Config._processopt
|
||||||
|
self.default = attrs["default"]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
self._set_opt_strings(names)
|
||||||
|
if not self.dest:
|
||||||
|
if self._long_opts:
|
||||||
|
self.dest = self._long_opts[0][2:].replace("-", "_")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
self.dest = self._short_opts[0][1:]
|
||||||
|
except IndexError:
|
||||||
|
raise ArgumentError("need a long or short option", self)
|
||||||
|
|
||||||
|
def names(self):
|
||||||
|
return self._short_opts + self._long_opts
|
||||||
|
|
||||||
|
def attrs(self):
|
||||||
|
# update any attributes set by processopt
|
||||||
|
attrs = "default dest help".split()
|
||||||
|
if self.dest:
|
||||||
|
attrs.append(self.dest)
|
||||||
|
for attr in attrs:
|
||||||
|
try:
|
||||||
|
self._attrs[attr] = getattr(self, attr)
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
if self._attrs.get("help"):
|
||||||
|
a = self._attrs["help"]
|
||||||
|
a = a.replace("%default", "%(default)s")
|
||||||
|
# a = a.replace('%prog', '%(prog)s')
|
||||||
|
self._attrs["help"] = a
|
||||||
|
return self._attrs
|
||||||
|
|
||||||
|
def _set_opt_strings(self, opts):
|
||||||
|
"""directly from optparse
|
||||||
|
|
||||||
|
might not be necessary as this is passed to argparse later on"""
|
||||||
|
for opt in opts:
|
||||||
|
if len(opt) < 2:
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid option string %r: "
|
||||||
|
"must be at least two characters long" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
elif len(opt) == 2:
|
||||||
|
if not (opt[0] == "-" and opt[1] != "-"):
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid short option string %r: "
|
||||||
|
"must be of the form -x, (x any non-dash char)" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
self._short_opts.append(opt)
|
||||||
|
else:
|
||||||
|
if not (opt[0:2] == "--" and opt[2] != "-"):
|
||||||
|
raise ArgumentError(
|
||||||
|
"invalid long option string %r: "
|
||||||
|
"must start with --, followed by non-dash" % opt,
|
||||||
|
self,
|
||||||
|
)
|
||||||
|
self._long_opts.append(opt)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
args = []
|
||||||
|
if self._short_opts:
|
||||||
|
args += ["_short_opts: " + repr(self._short_opts)]
|
||||||
|
if self._long_opts:
|
||||||
|
args += ["_long_opts: " + repr(self._long_opts)]
|
||||||
|
args += ["dest: " + repr(self.dest)]
|
||||||
|
if hasattr(self, "type"):
|
||||||
|
args += ["type: " + repr(self.type)]
|
||||||
|
if hasattr(self, "default"):
|
||||||
|
args += ["default: " + repr(self.default)]
|
||||||
|
return "Argument({})".format(", ".join(args))
|
||||||
|
|
||||||
|
|
||||||
|
class OptionGroup(object):
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
if a shortened version of a long option is specified it will
|
||||||
|
be suppressed in the help. addoption('--twowords', '--two-words')
|
||||||
|
results in help showing '--two-words' only, but --twowords gets
|
||||||
|
accepted **and** the automatic destination is in args.twowords
|
||||||
|
"""
|
||||||
|
conflict = set(optnames).intersection(
|
||||||
|
name for opt in self.options for name in opt.names()
|
||||||
|
)
|
||||||
|
if conflict:
|
||||||
|
raise ValueError("option names %s already added" % conflict)
|
||||||
|
option = Argument(*optnames, **attrs)
|
||||||
|
self._addoption_instance(option, shortupper=False)
|
||||||
|
|
||||||
|
def _addoption(self, *optnames, **attrs):
|
||||||
|
option = Argument(*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(argparse.ArgumentParser):
|
||||||
|
|
||||||
|
def __init__(self, parser, extra_info=None):
|
||||||
|
if not extra_info:
|
||||||
|
extra_info = {}
|
||||||
|
self._parser = parser
|
||||||
|
argparse.ArgumentParser.__init__(
|
||||||
|
self,
|
||||||
|
usage=parser._usage,
|
||||||
|
add_help=False,
|
||||||
|
formatter_class=DropShorterLongHelpFormatter,
|
||||||
|
)
|
||||||
|
# extra_info is a dict of (param -> value) to display if there's
|
||||||
|
# an usage error to provide more contextual information to the user
|
||||||
|
self.extra_info = extra_info
|
||||||
|
|
||||||
|
def parse_args(self, args=None, namespace=None):
|
||||||
|
"""allow splitting of positional arguments"""
|
||||||
|
args, argv = self.parse_known_args(args, namespace)
|
||||||
|
if argv:
|
||||||
|
for arg in argv:
|
||||||
|
if arg and arg[0] == "-":
|
||||||
|
lines = ["unrecognized arguments: %s" % (" ".join(argv))]
|
||||||
|
for k, v in sorted(self.extra_info.items()):
|
||||||
|
lines.append(" %s: %s" % (k, v))
|
||||||
|
self.error("\n".join(lines))
|
||||||
|
getattr(args, FILE_OR_DIR).extend(argv)
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
class DropShorterLongHelpFormatter(argparse.HelpFormatter):
|
||||||
|
"""shorten help for long options that differ only in extra hyphens
|
||||||
|
|
||||||
|
- collapse **long** options that are the same except for extra hyphens
|
||||||
|
- special action attribute map_long_option allows surpressing additional
|
||||||
|
long options
|
||||||
|
- shortcut if there are only two options and one of them is a short one
|
||||||
|
- cache result on action object as this is called at least 2 times
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _format_action_invocation(self, action):
|
||||||
|
orgstr = argparse.HelpFormatter._format_action_invocation(self, action)
|
||||||
|
if orgstr and orgstr[0] != "-": # only optional arguments
|
||||||
|
return orgstr
|
||||||
|
res = getattr(action, "_formatted_action_invocation", None)
|
||||||
|
if res:
|
||||||
|
return res
|
||||||
|
options = orgstr.split(", ")
|
||||||
|
if len(options) == 2 and (len(options[0]) == 2 or len(options[1]) == 2):
|
||||||
|
# a shortcut for '-h, --help' or '--abc', '-a'
|
||||||
|
action._formatted_action_invocation = orgstr
|
||||||
|
return orgstr
|
||||||
|
return_list = []
|
||||||
|
option_map = getattr(action, "map_long_option", {})
|
||||||
|
if option_map is None:
|
||||||
|
option_map = {}
|
||||||
|
short_long = {}
|
||||||
|
for option in options:
|
||||||
|
if len(option) == 2 or option[2] == " ":
|
||||||
|
continue
|
||||||
|
if not option.startswith("--"):
|
||||||
|
raise ArgumentError(
|
||||||
|
'long optional argument without "--": [%s]' % (option), self
|
||||||
|
)
|
||||||
|
xxoption = option[2:]
|
||||||
|
if xxoption.split()[0] not in option_map:
|
||||||
|
shortened = xxoption.replace("-", "")
|
||||||
|
if (
|
||||||
|
shortened not in short_long
|
||||||
|
or len(short_long[shortened]) < len(xxoption)
|
||||||
|
):
|
||||||
|
short_long[shortened] = xxoption
|
||||||
|
# now short_long has been filled out to the longest with dashes
|
||||||
|
# **and** we keep the right option ordering from add_argument
|
||||||
|
for option in options:
|
||||||
|
if len(option) == 2 or option[2] == " ":
|
||||||
|
return_list.append(option)
|
||||||
|
if option[2:] == short_long.get(option.replace("-", "")):
|
||||||
|
return_list.append(option.replace(" ", "=", 1))
|
||||||
|
action._formatted_action_invocation = ", ".join(return_list)
|
||||||
|
return action._formatted_action_invocation
|
|
@ -0,0 +1,8 @@
|
||||||
|
class UsageError(Exception):
|
||||||
|
""" error in pytest usage or invocation"""
|
||||||
|
|
||||||
|
|
||||||
|
class PrintHelp(Exception):
|
||||||
|
"""Raised when pytest should print it's help to skip the rest of the
|
||||||
|
argument parsing and validation."""
|
||||||
|
pass
|
|
@ -0,0 +1,140 @@
|
||||||
|
import py
|
||||||
|
import os
|
||||||
|
from .exceptions import UsageError
|
||||||
|
|
||||||
|
|
||||||
|
def exists(path, ignore=EnvironmentError):
|
||||||
|
try:
|
||||||
|
return path.check()
|
||||||
|
except ignore:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def getcfg(args, warnfunc=None):
|
||||||
|
"""
|
||||||
|
Search the list of arguments for a valid ini-file for pytest,
|
||||||
|
and return a tuple of (rootdir, inifile, cfg-dict).
|
||||||
|
|
||||||
|
note: warnfunc is an optional function used to warn
|
||||||
|
about ini-files that use deprecated features.
|
||||||
|
This parameter should be removed when pytest
|
||||||
|
adopts standard deprecation warnings (#1804).
|
||||||
|
"""
|
||||||
|
from _pytest.deprecated import CFG_PYTEST_SECTION
|
||||||
|
|
||||||
|
inibasenames = ["pytest.ini", "tox.ini", "setup.cfg"]
|
||||||
|
args = [x for x in args if not str(x).startswith("-")]
|
||||||
|
if not args:
|
||||||
|
args = [py.path.local()]
|
||||||
|
for arg in args:
|
||||||
|
arg = py.path.local(arg)
|
||||||
|
for base in arg.parts(reverse=True):
|
||||||
|
for inibasename in inibasenames:
|
||||||
|
p = base.join(inibasename)
|
||||||
|
if exists(p):
|
||||||
|
iniconfig = py.iniconfig.IniConfig(p)
|
||||||
|
if "pytest" in iniconfig.sections:
|
||||||
|
if inibasename == "setup.cfg" and warnfunc:
|
||||||
|
warnfunc(
|
||||||
|
"C1", CFG_PYTEST_SECTION.format(filename=inibasename)
|
||||||
|
)
|
||||||
|
return base, p, iniconfig["pytest"]
|
||||||
|
if (
|
||||||
|
inibasename == "setup.cfg"
|
||||||
|
and "tool:pytest" in iniconfig.sections
|
||||||
|
):
|
||||||
|
return base, p, iniconfig["tool:pytest"]
|
||||||
|
elif inibasename == "pytest.ini":
|
||||||
|
# allowed to be empty
|
||||||
|
return base, p, {}
|
||||||
|
return None, None, None
|
||||||
|
|
||||||
|
|
||||||
|
def get_common_ancestor(paths):
|
||||||
|
common_ancestor = None
|
||||||
|
for path in paths:
|
||||||
|
if not path.exists():
|
||||||
|
continue
|
||||||
|
if common_ancestor is None:
|
||||||
|
common_ancestor = path
|
||||||
|
else:
|
||||||
|
if path.relto(common_ancestor) or path == common_ancestor:
|
||||||
|
continue
|
||||||
|
elif common_ancestor.relto(path):
|
||||||
|
common_ancestor = path
|
||||||
|
else:
|
||||||
|
shared = path.common(common_ancestor)
|
||||||
|
if shared is not None:
|
||||||
|
common_ancestor = shared
|
||||||
|
if common_ancestor is None:
|
||||||
|
common_ancestor = py.path.local()
|
||||||
|
elif common_ancestor.isfile():
|
||||||
|
common_ancestor = common_ancestor.dirpath()
|
||||||
|
return common_ancestor
|
||||||
|
|
||||||
|
|
||||||
|
def get_dirs_from_args(args):
|
||||||
|
|
||||||
|
def is_option(x):
|
||||||
|
return str(x).startswith("-")
|
||||||
|
|
||||||
|
def get_file_part_from_node_id(x):
|
||||||
|
return str(x).split("::")[0]
|
||||||
|
|
||||||
|
def get_dir_from_path(path):
|
||||||
|
if path.isdir():
|
||||||
|
return path
|
||||||
|
return py.path.local(path.dirname)
|
||||||
|
|
||||||
|
# These look like paths but may not exist
|
||||||
|
possible_paths = (
|
||||||
|
py.path.local(get_file_part_from_node_id(arg))
|
||||||
|
for arg in args
|
||||||
|
if not is_option(arg)
|
||||||
|
)
|
||||||
|
|
||||||
|
return [get_dir_from_path(path) for path in possible_paths if path.exists()]
|
||||||
|
|
||||||
|
|
||||||
|
def determine_setup(inifile, args, warnfunc=None, rootdir_cmd_arg=None):
|
||||||
|
dirs = get_dirs_from_args(args)
|
||||||
|
if inifile:
|
||||||
|
iniconfig = py.iniconfig.IniConfig(inifile)
|
||||||
|
is_cfg_file = str(inifile).endswith(".cfg")
|
||||||
|
# TODO: [pytest] section in *.cfg files is depricated. Need refactoring.
|
||||||
|
sections = ["tool:pytest", "pytest"] if is_cfg_file else ["pytest"]
|
||||||
|
for section in sections:
|
||||||
|
try:
|
||||||
|
inicfg = iniconfig[section]
|
||||||
|
if is_cfg_file and section == "pytest" and warnfunc:
|
||||||
|
from _pytest.deprecated import CFG_PYTEST_SECTION
|
||||||
|
|
||||||
|
warnfunc("C1", CFG_PYTEST_SECTION.format(filename=str(inifile)))
|
||||||
|
break
|
||||||
|
except KeyError:
|
||||||
|
inicfg = None
|
||||||
|
rootdir = get_common_ancestor(dirs)
|
||||||
|
else:
|
||||||
|
ancestor = get_common_ancestor(dirs)
|
||||||
|
rootdir, inifile, inicfg = getcfg([ancestor], warnfunc=warnfunc)
|
||||||
|
if rootdir is None:
|
||||||
|
for rootdir in ancestor.parts(reverse=True):
|
||||||
|
if rootdir.join("setup.py").exists():
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
rootdir, inifile, inicfg = getcfg(dirs, warnfunc=warnfunc)
|
||||||
|
if rootdir is None:
|
||||||
|
rootdir = get_common_ancestor([py.path.local(), ancestor])
|
||||||
|
is_fs_root = os.path.splitdrive(str(rootdir))[1] == "/"
|
||||||
|
if is_fs_root:
|
||||||
|
rootdir = ancestor
|
||||||
|
if rootdir_cmd_arg:
|
||||||
|
rootdir_abs_path = py.path.local(os.path.expandvars(rootdir_cmd_arg))
|
||||||
|
if not os.path.isdir(str(rootdir_abs_path)):
|
||||||
|
raise UsageError(
|
||||||
|
"Directory '{}' not found. Check your '--rootdir' option.".format(
|
||||||
|
rootdir_abs_path
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rootdir = rootdir_abs_path
|
||||||
|
return rootdir, inifile, inicfg or {}
|
|
@ -51,7 +51,7 @@ class TestCaptureManager(object):
|
||||||
|
|
||||||
def test_getmethod_default_no_fd(self, monkeypatch):
|
def test_getmethod_default_no_fd(self, monkeypatch):
|
||||||
from _pytest.capture import pytest_addoption
|
from _pytest.capture import pytest_addoption
|
||||||
from _pytest.config import Parser
|
from _pytest.config.argparsing import Parser
|
||||||
|
|
||||||
parser = Parser()
|
parser = Parser()
|
||||||
pytest_addoption(parser)
|
pytest_addoption(parser)
|
||||||
|
|
|
@ -4,12 +4,8 @@ import textwrap
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import _pytest._code
|
import _pytest._code
|
||||||
from _pytest.config import (
|
from _pytest.config.findpaths import getcfg, get_common_ancestor, determine_setup
|
||||||
getcfg,
|
from _pytest.config import _iter_rewritable_modules
|
||||||
get_common_ancestor,
|
|
||||||
determine_setup,
|
|
||||||
_iter_rewritable_modules,
|
|
||||||
)
|
|
||||||
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
from _pytest.main import EXIT_NOTESTSCOLLECTED
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import sys
|
||||||
import os
|
import os
|
||||||
import py
|
import py
|
||||||
import pytest
|
import pytest
|
||||||
from _pytest import config as parseopt
|
from _pytest.config import argparsing as parseopt
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
Loading…
Reference in New Issue