SHARE
TWEET

Untitled

a guest Jun 16th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. from collections import defaultdict
  4. from datetime import datetime
  5. import json
  6. import fileinput
  7. import os
  8. import pprint
  9. from xml.dom.minidom import Document
  10.  
  11. LINENO = defaultdict(int)
  12. (PASS, FAIL, SKIP) = ('PASS', 'FAIL', 'SKIP')
  13. RESULT_FILE = os.path.join('build', 'TEST-result.xml')
  14.  
  15. class Testcase:
  16.     def __init__(self):
  17.         self.status = None
  18.         self.timestamp = None
  19.         self.elapsed = None
  20.         self.lines = []
  21.  
  22.     def report(self, status, timestamp, elapsed):
  23.         self.status = status
  24.         self.timestamp = timestamp
  25.         self.elapsed = elapsed
  26.  
  27.     def add_output(self, line):
  28.         self.lines.append(line)
  29.  
  30. class Testsuite:
  31.  
  32.     def __init__(self):
  33.         self.package = None
  34.         self.tests = defaultdict(Testcase)
  35.         self.timestamp = None
  36.         self.elapsed = None
  37.         self.failures = 0
  38.         self.skipped = 0
  39.         self.total = 0
  40.         self.lines = []
  41.  
  42.     def run(self, test):
  43.         if test is None:
  44.             self.elapsed = elapsed
  45.             return
  46.  
  47.         _ = self.tests[test]
  48.         self.total += 1
  49.  
  50.     def report(self, test, status, timestamp, elapsed):
  51.         if test is None:
  52.             self.elapsed = elapsed
  53.             self.timestamp = timestamp
  54.             return
  55.  
  56.         self.tests[test].report(status, timestamp, elapsed)
  57.         if status == FAIL:
  58.             self.failures += 1
  59.         elif status == SKIP:
  60.             self.skipped += 1
  61.  
  62.     def add_output(self, test, line):
  63.         if test is None:
  64.             self.lines.append(line)
  65.             return
  66.  
  67.         self.tests[test].add_output(line)
  68.  
  69.  
  70. TESTSUITES = defaultdict(Testsuite)
  71.  
  72.  
  73. def debug_event(event):
  74.     pprint.pprint(event)
  75.  
  76.  
  77. def report_test(status, event):
  78.     package = event.get('Package')
  79.     test = event.get('Test')
  80.     elapsed = event.get('Elapsed')
  81.     timestamp = event.get('Time')
  82.     testsuite = TESTSUITES[package]
  83.     testsuite.report(test, status, timestamp, elapsed)
  84.  
  85.  
  86. def action_fail(event):
  87.     report_test(FAIL, event)
  88.  
  89.  
  90. def action_output(event):
  91.     package = event.get('Package')
  92.     test = event.get('Test')
  93.     time = event.get('Time')
  94.     output = event['Output']
  95.     testsuite = TESTSUITES[package]
  96.     testsuite.add_output(test, output)
  97.     if 'Test' in event:
  98.         test = event['Test']
  99.         num = LINENO[test]
  100.         LINENO[test] += 1
  101.         slug = '{}+{}'.format(test, num)
  102.         print('{:32s} {}'.format(slug, event['Output']), end='')
  103.     else:
  104.         print('{}'.format(event['Output']), end='')
  105.  
  106.  
  107. def action_pass(event):
  108.     report_test(PASS, event)
  109.  
  110.  
  111. def action_run(event):
  112.     package = event.get('Package')
  113.     test = event.get('Test')
  114.     time = event.get('Time')
  115.     testsuite = TESTSUITES[package]
  116.     testsuite.run(test)
  117.  
  118.  
  119. def action_skip(event):
  120.     report_test(SKIP, event)
  121.  
  122.  
  123. ACTIONS = {
  124.     'fail': action_fail,
  125.     'output': action_output,
  126.     'pass': action_pass,
  127.     'run': action_run,
  128.     'skip': action_skip,
  129. }
  130.  
  131.  
  132. def process(line):
  133.     try:
  134.         event = json.loads(line)
  135.         action = event['Action']
  136.         ACTIONS.get(action, debug_event)(event)
  137.     except (ValueError, KeyError):
  138.         print('[ParseErr]', line, end='')
  139.  
  140.  
  141. def _createCDATAsections(doc, node, text):
  142.     pos = text.find(']]>')
  143.     while pos >= 0:
  144.         tmp = text[0:pos+2]
  145.         cdata = doc.createCDATASection(tmp)
  146.         node.appendChild(cdata)
  147.         text = text[pos+2:]
  148.         pos = text.find(']]>')
  149.     cdata = doc.createCDATASection(text)
  150.     node.appendChild(cdata)
  151.  
  152.  
  153. def generate_report():
  154.     print('#' * 80)
  155.     print('Report:')
  156.     doc = Document()
  157.     testsuites = doc.createElement('testsuites')
  158.     doc.appendChild(testsuites)
  159.     total, failures, skipped = 0, 0, 0
  160.     for package, suite in TESTSUITES.items():
  161.         testsuite = doc.createElement('testsuite')
  162.         testsuites.appendChild(testsuite)
  163.         testsuite.setAttribute('name', package)
  164.         testsuite.setAttribute('tests', str(suite.total))
  165.         testsuite.setAttribute('time', '{:.3f}'.format(suite.elapsed))
  166.         testsuite.setAttribute('timestamp', suite.timestamp)
  167.         testsuite.setAttribute('failures', str(suite.failures))
  168.         testsuite.setAttribute('skipped', str(suite.skipped))
  169.         testsuite.setAttribute('name', package)
  170.         if suite.lines:
  171.             stdout = doc.createElement('system-out')
  172.             testsuite.appendChild(stdout)
  173.             _createCDATAsections(doc, stdout, ''.join(suite.lines))
  174.         for name, case in suite.tests.items():
  175.             print(case.status, name, '{:.3f}s'.format(case.elapsed))
  176.             testcase = doc.createElement('testcase')
  177.             testsuite.appendChild(testcase)
  178.             testcase.setAttribute('name', name)
  179.             testcase.setAttribute('time', '{:.3f}'.format(case.elapsed))
  180.             testcase.setAttribute('timestamp', case.timestamp)
  181.             if case.status == SKIP:
  182.                 skip = doc.createElement('skipped')
  183.                 testcase.appendChild(skip)
  184.             elif case.status == FAIL:
  185.                 fail = doc.createElement('failure')
  186.                 testcase.appendChild(fail)
  187.             if case.lines:
  188.                 stdout = doc.createElement('system-out')
  189.                 testcase.appendChild(stdout)
  190.                 _createCDATAsections(doc, stdout, ''.join(case.lines))
  191.  
  192.         print(package, 'total={} failed={} skipped={} duration={:.3f}s'.format(
  193.             suite.total, suite.failures, suite.skipped, suite.elapsed))
  194.  
  195.         total += suite.total
  196.         failures += suite.failures
  197.         skipped += suite.skipped
  198.  
  199.     print('Ran {} tests, failed={} skipped={}'.format(total, failures, skipped))
  200.  
  201.     print('#' * 80)
  202.     content = doc.toprettyxml()
  203.     try:
  204.         os.makedirs('build', exist_ok=True)
  205.         with open(RESULT_FILE, 'wb') as junit:
  206.             junit.write(content.encode('utf-8'))
  207.     except Exception:
  208.         print('Could not write {}'.format(RESULT_FILE))
  209.         sys.exit(1)
  210.     print('Successfully wrote {}'.format(RESULT_FILE))
  211.  
  212.  
  213. if __name__ == "__main__":
  214.  
  215.     for line in fileinput.input():
  216.         process(line)
  217.  
  218.     generate_report()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top