From 1280041f0c1fe74219cbbbfca6b13c27c6d2dbeb Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 30 Oct 2010 19:23:50 +0200 Subject: [PATCH] add and document new parser.addini(name, description) method to describe ini-values. Also document the parser object with its public methods. --HG-- branch : trunk --- doc/customize.txt | 4 ++++ doc/talks.txt | 2 -- pytest/plugin/config.py | 23 +++++++++++++++++++++-- pytest/plugin/helpconfig.py | 10 +++------- testing/plugin/test_helpconfig.py | 2 +- testing/test_parseopt.py | 13 +++++++++++++ 6 files changed, 42 insertions(+), 12 deletions(-) diff --git a/doc/customize.txt b/doc/customize.txt index 88843556c..abef35e52 100644 --- a/doc/customize.txt +++ b/doc/customize.txt @@ -354,6 +354,9 @@ Reference of important objects involved in hooks .. autoclass:: pytest.plugin.config.Config :members: +.. autoclass:: pytest.plugin.config.Parser + :members: + .. autoclass:: pytest.plugin.session.Item :inherited-members: @@ -367,6 +370,7 @@ Reference of important objects involved in hooks :members: + conftest.py configuration files ================================================= diff --git a/doc/talks.txt b/doc/talks.txt index 6ec6a38b4..1b8963e5d 100644 --- a/doc/talks.txt +++ b/doc/talks.txt @@ -18,7 +18,6 @@ function arguments: - :ref:`mysetup` - `application setup in test functions with funcargs`_ -- `making funcargs dependendent on command line options`_ - `monkey patching done right`_ (blog post, consult `monkeypatch plugin`_ for actual 1.0 API) @@ -40,7 +39,6 @@ plugin specific examples: - `many examples in the docs for plugins`_ .. _`skipping slow tests by default in py.test`: http://bruynooghe.blogspot.com/2009/12/skipping-slow-test-by-default-in-pytest.html -.. _`making funcargs dependendent on command line options`: funcargs.html#tut-cmdlineoption .. _`many examples in the docs for plugins`: plugin/index.html .. _`monkeypatch plugin`: plugin/monkeypatch.html .. _`application setup in test functions with funcargs`: funcargs.html#appsetup diff --git a/pytest/plugin/config.py b/pytest/plugin/config.py index d6188dfa5..120608045 100644 --- a/pytest/plugin/config.py +++ b/pytest/plugin/config.py @@ -10,6 +10,10 @@ def pytest_cmdline_parse(pluginmanager, args): config.parse(args) return config +def pytest_addoption(parser): + parser.addini('addargs', 'extra command line arguments') + parser.addini('minversion', 'minimally required pytest version') + class Parser: """ Parser for command line arguments. """ @@ -18,6 +22,7 @@ class Parser: self._groups = [] self._processopt = processopt self._usage = usage + self._inidict = {} self.hints = [] def processoption(self, option): @@ -29,6 +34,12 @@ class Parser: self._notes.append(note) def getgroup(self, name, description="", after=None): + """ 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. + """ for group in self._groups: if group.name == name: return group @@ -61,6 +72,14 @@ class Parser: setattr(option, name, value) return args + def addini(self, name, description): + """ add an ini-file option with the given name and description. """ + self._inidict[name] = description + + def setfromini(self, inisection, option): + for name, value in inisection.items(): + assert name in self._inidict + return setattr(option, name, value) class OptionGroup: def __init__(self, name, description="", parser=None): @@ -254,7 +273,6 @@ class Config(object): if not hasattr(self.option, opt.dest): setattr(self.option, opt.dest, opt.default) - def _getmatchingplugins(self, fspath): allconftests = self._conftest._conftestpath2mod.values() plugins = [x for x in self.pluginmanager.getplugins() @@ -305,13 +323,14 @@ class Config(object): minver, pytest.__version__)) def parse(self, args): - # cmdline arguments into this config object. + # parse given cmdline arguments into this config object. # 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) + self._parser.setfromini(self.inicfg, self.option) if not args: args.append(py.std.os.getcwd()) self.args = args diff --git a/pytest/plugin/helpconfig.py b/pytest/plugin/helpconfig.py index 7086d1072..203cf494e 100644 --- a/pytest/plugin/helpconfig.py +++ b/pytest/plugin/helpconfig.py @@ -7,7 +7,7 @@ import inspect, sys def pytest_addoption(parser): group = parser.getgroup('debugconfig') group.addoption('--version', action="store_true", - help="display py lib version and import information.") + help="display pytest lib version and import information.") group._addoption("-h", "--help", action="store_true", dest="help", help="show help message and configuration info") group._addoption('-p', action="append", dest="plugins", default = [], @@ -40,12 +40,12 @@ def showhelp(config): tw.write(config._parser.optparser.format_help()) tw.line() tw.line() - tw.sep( "=", "ini-settings") + tw.sep( "=", "config file settings") tw.line("the following values can be defined in [pytest] sections of") tw.line("setup.cfg or tox.ini files:") tw.line() - for name, help in ini_settings: + for name, help in sorted(config._parser._inidict.items()): line = " %-15s %s" %(name, help) tw.line(line[:tw.fullwidth]) @@ -59,10 +59,6 @@ def showhelp(config): tw.line() tw.sep( "=") -ini_settings = ( - ('addargs', 'extra command line arguments'), - ('minversion', 'minimally required pytest version'), -) conftest_options = ( ('pytest_plugins', 'list of plugin names to load'), diff --git a/testing/plugin/test_helpconfig.py b/testing/plugin/test_helpconfig.py index e43df0541..691cb5bf4 100644 --- a/testing/plugin/test_helpconfig.py +++ b/testing/plugin/test_helpconfig.py @@ -15,7 +15,7 @@ def test_help(testdir): assert result.ret == 0 result.stdout.fnmatch_lines([ "*-v*verbose*", - "*ini-settings*", + "*settings*", "*conftest.py*", ]) diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index 1fab2fdcb..fbe800a7f 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -1,5 +1,6 @@ import py from pytest.plugin import config as parseopt +from textwrap import dedent class TestParser: def test_no_help_by_default(self, capsys): @@ -100,6 +101,18 @@ class TestParser: assert option.hello == "world" assert option.this == 42 + def test_parser_addini(self, tmpdir): + parser = parseopt.Parser() + parser.addini("myname", "my new ini value") + cfg = py.iniconfig.IniConfig("tox.ini", dedent(""" + [pytest] + myname=hello + """))['pytest'] + class option: + pass + parser.setfromini(cfg, option) + assert option.myname == "hello" + @py.test.mark.skipif("sys.version_info < (2,5)") def test_addoption_parser_epilog(testdir): testdir.makeconftest("""