#!/usr/bin/python import os import sys import glob import datetime import xml.sax f = None from tempfile import mkstemp from shutil import move from os import remove, close def replace(file_path): #Create temp file fh, abs_path = mkstemp() with open(abs_path,'w') as new_file: with open(file_path) as old_file: for line in old_file: line = line.replace("FAIL", '\FAIL{[FAIL]}') line = line.replace("PASS", '\PASS{[PASS]}') new_file.write(line) close(fh) #Remove original file remove(file_path) #Move new file move(abs_path, file_path) class TestsuitesHandler( xml.sax.ContentHandler ): # Call when an element starts def startElement(self, tag, attributes): self.CurrentData = tag if tag == "testsuites": name = attributes["name"] failures = attributes["failures"] disabled = attributes["disabled"] errors = attributes["errors"] timestamp = attributes["timestamp"] f.write("Time|Failures|Disabled|Errors\n") f.write("----|--------|--------|------\n") f.write(timestamp + "|" + failures + "|" + disabled + "|" + errors + "\n") f.write("\n") if tag == "testsuite": name = attributes["name"] failures = attributes["failures"] disabled = attributes["disabled"] errors = attributes["errors"] f.write("\n## Testsuite " + name + "\n\n") f.write("Failures|Disabled|Errors\n") f.write("--------|--------|------\n") f.write(failures + "|" + disabled + "|" + errors + "\n\n") f.write("Test|Result\n") f.write("----|------\n") if tag == "testcase": self.name = attributes["name"] self.failure = False if tag == "failure": self.failure = True def endElement(self, tag): if self.CurrentData == "testcase" or self.CurrentData == "failure": if self.failure == False: f.write(self.name + "| PASS\n") else: f.write(self.name + "| FAIL\n") self.failure = False if self.CurrentData == "testsuites" or self.CurrentData == "testsuite": f.write("\n") self.CurrentData = "" def process_file(filename): global f testname = os.path.splitext(os.path.basename(filename))[0] mdfile = os.path.splitext(filename)[0] + '.md' texfile = os.path.splitext(filename)[0] + '.tex' # create an XMLReader parser = xml.sax.make_parser() # turn off namepsaces parser.setFeature(xml.sax.handler.feature_namespaces, 0) # override the default ContextHandler Handler = TestsuitesHandler() parser.setContentHandler( Handler ) f = open(mdfile, 'w') f.write("\n# " + testname + "\n") f.write("\n") parser.parse(filename) f.close() os.system("pandoc -f markdown_github -t latex " + mdfile + " -o " + texfile) replace(texfile) if ( __name__ == "__main__"): basedir = os.path.dirname(os.path.splitext(sys.argv[1])[0]) date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sumfile = basedir + "/report.tex" summary = open(sumfile, "w") summary.write("%% Google Test Report generated at " + date + "\n") for f in glob.glob(sys.argv[1]): print("Processing " + f) summary.write('\input{\detokenize{' + os.path.splitext(f)[0] + '.tex' + '}}\n') process_file(f) summary.close() print("Writing summary " + sumfile)