diff --git a/_py/test/parseopt.py b/_py/test/parseopt.py index 7b10c0a9b..117860af9 100644 --- a/_py/test/parseopt.py +++ b/_py/test/parseopt.py @@ -21,7 +21,7 @@ class Parser: def __init__(self, usage=None, processopt=None): self._anonymous = OptionGroup("custom options", parser=self) - self._groups = [self._anonymous] + self._groups = [] self._processopt = processopt self._usage = usage self.epilog = "" @@ -34,19 +34,21 @@ class Parser: def addnote(self, note): self._notes.append(note) - def addgroup(self, name, description=""): - for group in self._groups: - if group.name == name: - raise ValueError("group %r already exists" % name) - group = OptionGroup(name, description, parser=self) - self._groups.append(group) - return group - - def getgroup(self, name, description=""): + def getgroup(self, name, description="", after=None): for group in self._groups: if group.name == name: return group - return self.addgroup(name, description) + 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 addgroup(self, name, description=""): + py.log._apiwarn("1.1", "use getgroup() which gets-or-creates") + return self.getgroup(name, description) def addoption(self, *opts, **attrs): """ add an optparse-style option. """ @@ -56,7 +58,7 @@ class Parser: optparser = optparse.OptionParser(usage=self._usage) # make sure anaonymous group is at the end optparser.epilog = self.epilog - groups = self._groups[1:] + [self._groups[0]] + groups = self._groups + [self._anonymous] for group in groups: if group.options: desc = group.description or group.name diff --git a/_py/test/plugin/pytest_default.py b/_py/test/plugin/pytest_default.py index 028e49c4c..1917ac569 100644 --- a/_py/test/plugin/pytest_default.py +++ b/_py/test/plugin/pytest_default.py @@ -73,7 +73,8 @@ def pytest_addoption(parser): action="store_true", dest="looponfail", default=False, help="run tests, re-run failing test set until all pass.") - group = parser.addgroup("debugconfig", "test process debugging and configuration") + group = parser.getgroup("debugconfig", + "test process debugging and configuration") group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir", help="base temporary directory for this test run.") @@ -84,7 +85,8 @@ def pytest_addoption(parser): "execnet missing: --looponfailing and distributed testing not available.") def add_dist_options(parser): - group = parser.addgroup("dist", "distributed testing") # see http://pytest.org/help/dist") + # see http://pytest.org/help/dist") + group = parser.getgroup("dist", "distributed testing") group._addoption('--dist', metavar="distmode", action="store", choices=['load', 'each', 'no'], type="choice", dest="dist", default="no", diff --git a/_py/test/plugin/pytest_doctest.py b/_py/test/plugin/pytest_doctest.py index eb20afe09..65ab09c17 100644 --- a/_py/test/plugin/pytest_doctest.py +++ b/_py/test/plugin/pytest_doctest.py @@ -18,7 +18,7 @@ from _py.code.code import TerminalRepr, ReprFileLocation import doctest def pytest_addoption(parser): - group = parser.addgroup("doctest options") + group = parser.getgroup("doctest options") group.addoption("--doctest-modules", action="store_true", default=False, help="search all python files for doctests", diff --git a/conftest.py b/conftest.py index c35968219..cc8a1128f 100644 --- a/conftest.py +++ b/conftest.py @@ -4,7 +4,7 @@ rsyncdirs = ['conftest.py', 'py', 'doc', 'testing'] import py def pytest_addoption(parser): - group = parser.addgroup("pylib", "py lib testing options") + group = parser.getgroup("pylib", "py lib testing options") group.addoption('--sshhost', action="store", dest="sshhost", default=None, help=("ssh xspec for ssh functional tests. ")) diff --git a/doc/changelog.txt b/doc/changelog.txt index c77ad2eee..c10e5041b 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -1,6 +1,8 @@ Changes between 1.0.2 and '1.1.0b1' ===================================== +* deprecate parser.addgroup in favour of getgroup which creates option group + * add --report command line option that allows to control showing of skipped/xfailed sections * generalized skipping: a new way to mark python functions with skipif or xfail diff --git a/testing/pytest/test_parseopt.py b/testing/pytest/test_parseopt.py index bbe6c2a85..3e4779357 100644 --- a/testing/pytest/test_parseopt.py +++ b/testing/pytest/test_parseopt.py @@ -19,11 +19,15 @@ class TestParser: group = parser.addgroup("hello", description="desc") assert group.name == "hello" assert group.description == "desc" - py.test.raises(ValueError, parser.addgroup, "hello") - group2 = parser.getgroup("hello") - assert group2 is group - def test_getgroup_addsgroup(self): + def test_addgroup_deprecation(self, recwarn): + parser = parseopt.Parser() + group = parser.addgroup("hello", description="desc") + assert recwarn.pop() + group2 = parser.getgroup("hello") + assert group == group2 + + def test_getgroup_simple(self): parser = parseopt.Parser() group = parser.getgroup("hello", description="desc") assert group.name == "hello" @@ -31,6 +35,14 @@ class TestParser: group2 = parser.getgroup("hello") assert group2 is group + def test_group_ordering(self): + parser = parseopt.Parser() + group0 = parser.getgroup("1") + group1 = parser.getgroup("2") + group1 = parser.getgroup("3", after="1") + groups = parser._groups + groups_names = [x.name for x in groups] + assert groups_names == list("132") def test_group_addoption(self): group = parseopt.OptionGroup("hello")