All files / piscosour/lib/reporters junit.js

0% Statements 0/63
0% Branches 0/27
0% Functions 0/14
0% Lines 0/60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174                                                                                                                                                                                                                                                                                                                                                           
'use strict';
 
const _ = require('lodash');
const xml = require('xml');
 
const templates = {
  testcase: () => ({
    testcase: [
      {
        _attr: {
          time: 0
        }
      }
    ]
  }),
  testsuite: () => ({
    testsuite: [
      {
        _attr: {
          tests: 0,
          failures: 0,
          errors: 0,
          warnings: 0,
          time: 0
        }
      }
    ]
  }),
  testsuites: () => ({
    testsuites: [
      {
        _attr: {
          tests: 0,
          errors: 0,
          failures: 0,
          warnings: 0,
          time: 0
        }
      }
    ]
  })
};
 
const xmlNodeType = node => {
  if (node.type === 'command') {
    return { node: templates.testsuites(), key: 'testsuites' };
  } else if (node.children.length > 0) {
    return { node: templates.testsuite(), key: 'testsuite' };
  } else if (node.children.length === 0) {
    return { node: templates.testcase(), key: 'testcase' };
  }
};
 
const xmlNodeDescription = node => {
  let description = '';
  switch (node.type) {
    case 'command':
      description = `command: ${node.id}`;
      break;
 
    case 'flow':
      description = `flow: ${node.data.description}`;
      break;
 
    case 'step':
      description = node.data.description
        ? `step: ${node.data.context}::${node.id} (${node.data.description})`
        : `step: ${node.data.context}::${node.id}`;
      break;
 
    case 'stage':
      description = `stage: ${node.data.order} (${node.id})`;
      break;
  }
  return description;
};
 
const xmlNodeData = node => ({
  name: xmlNodeDescription(node),
  time: (node.endTime - node.initTime) / 1000,
  timestamp: Date(node.initTime)
});
 
const xmlNodeStats = node => node.isLeaf ?
{} :
{
  tests: node.stats.executed(),
  warnings: node.stats.warnings(),
  failures: node.stats.failures(),
  errors: node.stats.errors()
};
 
const xmlNodeClassname = node => node.isLeaf ?
{
  classname: `${_.padStart(node.parent.data.order, 3, '0')} ${node.parent.data.context}::${node.parent.id}`
} :
{};
 
const xmlNodeOutput = node => {
  const outputNodes = {
    systemOut: 'system-out',
    warning: 'warning',
    failure: 'failure',
    error: 'error'
  };
 
  if (node.output && node.isLeaf) {
    let outputNode;
    switch (node.status) {
      case 0:
        outputNode = {
          [outputNodes.systemOut]: {
            _cdata: node.output
          }
        };
        break;
 
      case 1:
        outputNode = {
          [node.data.keep ? outputNodes.failure : outputNodes.error]: {
            _cdata: node.output
          }
        };
        break;
 
      case 2:
        outputNode = {
          [outputNodes.warning]: {
            _cdata: node.output
          }
        };
        break;
    }
 
    return outputNode;
  }
 
  return null;
};
 
const XmlNode = node => {
  const xmlNode = xmlNodeType(node);
  const data = xmlNodeData(node);
  const stats = xmlNodeStats(node);
  const className = xmlNodeClassname(node);
  const outputNode = xmlNodeOutput(node);
 
  Object.assign(xmlNode.node[xmlNode.key][0]._attr, data, stats, className);
 
  if (outputNode) {
    xmlNode.node[xmlNode.key].push(outputNode);
  }
 
  return xmlNode;
};
 
const xmlNodeTransform = node => {
  const xmlNode = XmlNode(node);
  node.children
    .map(child => xmlNodeTransform(child))
    .forEach(child => xmlNode.node[xmlNode.key].push(child));
 
  return xmlNode.node;
};
 
const write = tree => {
  const xmlTree = xml(xmlNodeTransform(tree), { indent: '  ' });
  return xmlTree;
};
 
module.exports = {
  write: write
};