From aad04ea8aed99382817e5067e7932b56a8a2cbab Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 5 May 2009 23:52:25 +0200 Subject: [PATCH] * kill some code and indirections * have metainfo() directly provide fileinfo + message --HG-- branch : trunk --- py/test/collect.py | 48 ++++--------------------- py/test/plugin/pytest_restdoc.py | 12 +++++-- py/test/plugin/pytest_terminal.py | 25 +++++++++---- py/test/pycollect.py | 18 +++++----- py/test/testing/test_collect.py | 60 ------------------------------- py/test/testing/test_pycollect.py | 52 +++++++++++++++++++++++++++ 6 files changed, 94 insertions(+), 121 deletions(-) diff --git a/py/test/collect.py b/py/test/collect.py index 7c3c46497..358f33987 100644 --- a/py/test/collect.py +++ b/py/test/collect.py @@ -12,31 +12,6 @@ def configproperty(name): return self.config.getvalue(name, self.fspath) return property(fget) -class ReprMetaInfo(object): - def __init__(self, fspath=None, lineno=None, modpath=None): - self.fspath = fspath - self.lineno = lineno - self.modpath = modpath - - def verboseline(self, basedir=None): - params = self.__dict__.copy() - if self.fspath: - if basedir is not None: - params['fspath'] = basedir.bestrelpath(self.fspath) - if self.lineno is not None: - params['lineno'] = self.lineno + 1 - - if self.fspath and self.lineno and self.modpath: - line = "%(fspath)s:%(lineno)s: %(modpath)s" - elif self.fspath and self.modpath: - line = "%(fspath)s: %(modpath)s" - elif self.fspath and self.lineno: - line = "%(fspath)s:%(lineno)s" - else: - line = "[nometainfo]" - return line % params - - class Node(object): """ base class for Nodes in the collection tree. Collector nodes have children and @@ -50,7 +25,6 @@ class Node(object): - configuration/options for setup/teardown stdout/stderr capturing and execution of test items """ - ReprMetaInfo = ReprMetaInfo def __init__(self, name, parent=None, config=None): self.name = name self.parent = parent @@ -450,31 +424,21 @@ class Item(Node): def _deprecated_testexecution(self): if self.__class__.run != Item.run: warnoldtestrun(function=self.run) - self.run() - return True elif self.__class__.execute != Item.execute: warnoldtestrun(function=self.execute) - self.execute(self.obj, *self._args) - return True + else: + return False + self.run() + return True def run(self): - """ deprecated, here because subclasses might call it. """ + """ deprecated, here because subclasses might call it. """ return self.execute(self.obj, *self._args) def execute(self, obj, *args): - """ deprecated, here because subclasses might call it. """ - warnoldtestrun(function=self.execute) + """ deprecated, here because subclasses might call it. """ return obj(*args) - def repr_metainfo(self): - try: - return self.ReprMetaInfo(self.fspath, modpath=self.__class__.__name__) - except AttributeError: - code = py.code.Code(self.execute) - return self.ReprMetaInfo(code.path, code.firstlineno) - - def runtest(self): - """ execute this test item.""" def warnoldcollect(function=None): py.log._apiwarn("1.0", diff --git a/py/test/plugin/pytest_restdoc.py b/py/test/plugin/pytest_restdoc.py index 7952abbbf..7dc8c6f0e 100644 --- a/py/test/plugin/pytest_restdoc.py +++ b/py/test/plugin/pytest_restdoc.py @@ -64,6 +64,9 @@ class ReSTSyntaxTest(py.test.collect.Item): super(ReSTSyntaxTest, self).__init__(*args, **kwargs) self.project = project + def metainfo(self): + return self.fspath, None, "syntax check" + def runtest(self): self.restcheck(py.path.svnwc(self.fspath)) @@ -193,6 +196,9 @@ class ReSTSyntaxTest(py.test.collect.Item): #return [] # no need to rebuild class DoctestText(py.test.collect.Item): + def metainfo(self): + return self.fspath, None, "doctest" + def runtest(self): content = self._normalize_linesep() newcontent = self.config.api.pytest_doctest_prepare_content(content=content) @@ -276,9 +282,9 @@ class LinkCheckerMaker(py.test.collect.Collector): args=(tryfn, path, lineno), callobj=localrefcheck) class CheckLink(py.test.collect.Function): - def repr_metainfo(self): - return self.ReprMetaInfo(fspath=self.fspath, lineno=self._args[2], - modpath="checklink: %s" % (self._args[0],)) + def metainfo(self, basedir=None): + return (self.fspath, self._args[2], "checklink: %s" % self._args[0]) + def setup(self): pass def teardown(self): diff --git a/py/test/plugin/pytest_terminal.py b/py/test/plugin/pytest_terminal.py index 4c74cb724..3a7c9bf75 100644 --- a/py/test/plugin/pytest_terminal.py +++ b/py/test/plugin/pytest_terminal.py @@ -138,8 +138,7 @@ class TerminalReporter: def pytest_itemstart(self, item, node=None): if self.config.option.debug: - info = item.repr_metainfo() - line = info.verboseline(basedir=self.curdir) + " " + line = self._metainfoline(item) extra = "" if node: extra = "-> " + str(node.gateway.id) @@ -149,8 +148,7 @@ class TerminalReporter: elif self.config.option.verbose and self.config.option.dist == "no": # ensure that the path is printed before the 1st test of # a module starts running - info = item.repr_metainfo() - line = info.verboseline(basedir=self.curdir) + " " + line = self._metainfoline(item) #self.write_fspath_result(fspath, "") self.write_ensure_prefix(line, "") @@ -172,8 +170,7 @@ class TerminalReporter: if not self.config.option.verbose: self.write_fspath_result(fspath, letter) else: - info = rep.colitem.repr_metainfo() - line = info.verboseline(basedir=self.curdir) + " " + line = self._metainfoline(rep.colitem) if not hasattr(rep, 'node'): self.write_ensure_prefix(line, word, **markup) else: @@ -250,6 +247,22 @@ class TerminalReporter: for rootdir in rootdirs: self.write_line("### Watching: %s" %(rootdir,), bold=True) + def _metainfoline(self, item): + fspath, lineno, msg = item.metainfo() + if fspath: + fspath = self.curdir.bestrelpath(fspath) + if lineno is not None: + lineno += 1 + if fspath and lineno and msg: + line = "%(fspath)s:%(lineno)s: %(msg)s" + elif fspath and msg: + line = "%(fspath)s: %(msg)s" + elif fspath and lineno: + line = "%(fspath)s:%(lineno)s" + else: + line = "[nometainfo]" + return line % locals() + " " + # # summaries for testrunfinish # diff --git a/py/test/pycollect.py b/py/test/pycollect.py index d53302c52..24dd5442b 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -87,15 +87,10 @@ class PyobjMixin(object): self._fslineno = fspath, lineno return fspath, lineno - def repr_metainfo(self): + def metainfo(self): fspath, lineno = self.getfslineno() modpath = self.getmodpath() - return self.ReprMetaInfo( - fspath=fspath, - lineno=lineno, - modpath=modpath, - ) - + return fspath, lineno, modpath class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): Class = configproperty('Class') @@ -235,6 +230,7 @@ class Instance(PyCollectorMixin, py.test.collect.Collector): class FunctionMixin(PyobjMixin): """ mixin for the code common to Function and Generator. """ + def _getsortvalue(self): return self.getfslineno() @@ -314,6 +310,7 @@ class Generator(FunctionMixin, PyCollectorMixin, py.test.collect.Collector): name = None call, args = obj[0], obj[1:] return name, call, args + # # Test Items @@ -389,7 +386,7 @@ class Function(FunctionMixin, py.test.collect.Item): def getrequest(self, argname): return FuncargRequest(pyfuncitem=self, argname=argname) - + class FuncargRequest: _argprefix = "pytest_funcarg__" @@ -447,8 +444,9 @@ class FuncargRequest: name = name[len(self._argprefix):] if name not in available: available.append(name) - metainfo = self._pyfuncitem.repr_metainfo() - msg = "funcargument %r not found for: %s" %(self.argname,metainfo.verboseline()) + fspath, lineno, msg = self._pyfuncitem.metainfo() + line = "%s:%s" %(fspath, lineno) + msg = "funcargument %r not found for: %s" %(self.argname, line) msg += "\n available funcargs: %s" %(", ".join(available),) raise LookupError(msg) diff --git a/py/test/testing/test_collect.py b/py/test/testing/test_collect.py index 670f141f1..6d4c50415 100644 --- a/py/test/testing/test_collect.py +++ b/py/test/testing/test_collect.py @@ -206,63 +206,3 @@ class TestCustomConftests: evrec = testdir.inline_run(testdir.tmpdir, "--XX") names = [rep.colitem.name for rep in evrec.getreports("collectreport")] assert 'hello' in names - -class TestCollectorReprs: - def test_repr_metainfo_basic_item(self, testdir): - modcol = testdir.getmodulecol("") - Item = py.test.collect.Item - item = Item("virtual", parent=modcol) - info = item.repr_metainfo() - assert info.fspath == modcol.fspath - assert not info.lineno - assert info.modpath == "Item" - - def test_repr_metainfo_func(self, testdir): - item = testdir.getitem("def test_func(): pass") - info = item.repr_metainfo() - assert info.fspath == item.fspath - assert info.lineno == 0 - assert info.modpath == "test_func" - - def test_repr_metainfo_class(self, testdir): - modcol = testdir.getmodulecol(""" - # lineno 0 - class TestClass: - def test_hello(self): pass - """) - classcol = modcol.collect_by_name("TestClass") - info = classcol.repr_metainfo() - assert info.fspath == modcol.fspath - assert info.lineno == 1 - assert info.modpath == "TestClass" - - def test_repr_metainfo_generator(self, testdir): - modcol = testdir.getmodulecol(""" - # lineno 0 - def test_gen(): - def check(x): - assert x - yield check, 3 - """) - gencol = modcol.collect_by_name("test_gen") - info = gencol.repr_metainfo() - assert info.fspath == modcol.fspath - assert info.lineno == 1 - assert info.modpath == "test_gen" - - genitem = gencol.collect()[0] - info = genitem.repr_metainfo() - assert info.fspath == modcol.fspath - assert info.lineno == 2 - assert info.modpath == "test_gen[0]" - """ - def test_func(): - pass - def test_genfunc(): - def check(x): - pass - yield check, 3 - class TestClass: - def test_method(self): - pass - """ diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index e7fb0d195..7e8985831 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -339,3 +339,55 @@ class TestConftestCustomization: assert len(colitems) == 1 assert colitems[0].name == "check_method" + +class TestMetaInfo: + + def test_func_metainfo(self, testdir): + item = testdir.getitem("def test_func(): pass") + fspath, lineno, modpath = item.metainfo() + assert fspath == item.fspath + assert lineno == 0 + assert modpath == "test_func" + + def test_class_metainfo(self, testdir): + modcol = testdir.getmodulecol(""" + # lineno 0 + class TestClass: + def test_hello(self): pass + """) + classcol = modcol.collect_by_name("TestClass") + fspath, lineno, msg = classcol.metainfo() + assert fspath == modcol.fspath + assert lineno == 1 + assert msg == "TestClass" + + def test_generator_metainfo(self, testdir): + modcol = testdir.getmodulecol(""" + # lineno 0 + def test_gen(): + def check(x): + assert x + yield check, 3 + """) + gencol = modcol.collect_by_name("test_gen") + fspath, lineno, modpath = gencol.metainfo() + assert fspath == modcol.fspath + assert lineno == 1 + assert modpath == "test_gen" + + genitem = gencol.collect()[0] + fspath, lineno, modpath = genitem.metainfo() + assert fspath == modcol.fspath + assert lineno == 2 + assert modpath == "test_gen[0]" + """ + def test_func(): + pass + def test_genfunc(): + def check(x): + pass + yield check, 3 + class TestClass: + def test_method(self): + pass + """