from os import getcwd import traceback from nose.plugins import Plugin class NoseGNUErrorformatOutput(Plugin): """Output tracebacks in GNU errorformat-compatible way. """ name = 'gnuerrorformat' def __init__(self): super(NoseGNUErrorformatOutput, self).__init__() self.cwd = getcwd() def formatFailure(self, test, err): return self.formatError(test, err) def formatError(self, _, err): ec, ev, tb = err if not isinstance(ev, str): ev = traceback.format_exception_only(ec, ev)[0] evs = [ev, "-----BEGIN GNU errorformat-----"] for (filen, linen, funcn, txt) in traceback.extract_tb(tb): if not filen.startswith(self.cwd): continue filen = filen[len(self.cwd) + 1:] evs.append("{}:{:d}:0: {}: {}".format(filen, linen, funcn, txt)) evs.append("-----END GNU errorformat-----") return ec, "\n".join(evs), tb