all files / lib/ istanbulReportParser.js

96.3% Statements 26/27
90% Branches 9/10
100% Functions 4/4
96.3% Lines 26/27
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                                                                                           
"use strict";
 
var fs = require("fs");
var XMLSplitter = require("xml-splitter");
var coberturaParser = require("./coberturaReportParser");
 
module.exports = parseIstanbulReport;
 
/**
 * Parses a given istanbul report file path and returns the XML "coverage" element containing the report results.
 *
 * @param {String} reportFilePath The location where the istanbul report in cobertura xml format is located.
 * @param {Function} callback Th regular CPS function.
 */
function parseIstanbulReport(reportFilePath, callback) {
  if (!reportFilePath) {
    return callback(new Error("The istanbul report file path must be provided!"));
  }
 
  // Read the XML cobertura report
  fs.readFile(reportFilePath, function(err, coberturaXmlReport) {
    if (err && err.code === "ENOENT") {
      return callback(new Error("The istanbul report file '" + reportFilePath + "' does not exist!"));
 
    } else Iif (err) {
      return callback(new Error("Error while reading the istanbul file '" + reportFilePath + "': " + err.message));
    }
 
    // Parse the cobertura root element.
    var xs = new XMLSplitter("/coverage");
    var errorOnce = false;
 
    // Calculate the final percent based on the line and branch rates
    xs.on("data", function parsingCoverageTag(xml) {
 
      // Calculate the function rate based on the functions collected.
      var methods = {
        total: 0,
        passed: 0
      };
 
      // Parse the xml in regards to the methods above.
      coberturaParser(xml, methods);
 
      // All the rates for the final result.
      var functionRate = parseFloat(methods.passed / methods.total);
      var lineRate = parseFloat(xml["line-rate"]);
      var branchRate = parseFloat(xml["branch-rate"]);
 
      // Calculate the final percent based on the line and branch rates$
      var percent = Math.floor(((functionRate + lineRate + branchRate) / 3) * 100);
 
      return callback(null, {
        overallPercent: percent,
        functionRate: functionRate,
        lineRate: lineRate,
        branchRate: branchRate
      });
 
    }).on("error", function handlingError(error) {
      var parsingError = new Error("Error parsing the given istanbul report (" + reportFilePath + "): ");
      parsingError.stack = error.stack;
      if (!errorOnce) {
        errorOnce = true;
        return callback(parsingError);
      }
 
    }).parseString(coberturaXmlReport);
 
  });
}