all files / javascript-test-reporter/ formatter.js

90.38% Statements 47/52
56.25% Branches 9/16
100% Functions 10/10
90.38% Lines 47/52
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                                                                                           817×                          
var lcovParse = require('lcov-parse');
var path = require('path');
var fs = require('fs');
var pjson = require('./package.json');
var git  = require("./git_info");
var ci  = require("./ci_info");
var async = require("async");
var gocoverParse = require('./gocover_parse');
 
function Formatter(options) {
  this.options = options || {};
}
 
Formatter.prototype.rootDirectory = function() {
  return this.options.rootDirectory || process.cwd();
};
 
Formatter.prototype.parse = function(data, callback) {
    if (/^SF:/m.test(data)) {
        lcovParse(data, callback);
    } else Eif (/^mode:/.test(data)) {
        gocoverParse(data, callback);
    } else {
        callback("Unknown input coverage format", null);
    }
}
 
Formatter.prototype.format = function(coverageData, callback) {
  var self = this;
 
  self.parse(coverageData, function(parseError, data) {
    Iif (parseError) {
      throw parseError;
    }
 
    var result = {
      source_files: self.sourceFiles(data),
      run_at: Date.now(),
      partial: false,
      environment: {
        pwd: process.cwd().split(path.sep).join('/'),
        package_version: pjson.version
      },
      ci_service: ci.getInfo()
    };
    async.parallel({
      head: git.head,
      branch: git.branch,
      committed_at: git.committedAt
    },
    function(err, results) {
      Iif (err) {
        console.error(err.message);
      }
      result.git = {
        head: results.head,
        branch: results.branch,
        committed_at: results.committed_at
      };
      return callback(parseError, result);
    });
  });
};
 
Formatter.prototype.sourceFiles = function(data) {
  var source_files = [];
  var self = this;
  data.forEach(function(elem, index) {
    var content;
    try {
      content = fs.readFileSync(elem.file).toString();
    } catch (e) {
      Eif (e.code === 'ENOENT') {
        console.log('The file ' + elem.file + ' does not exist and will be skipped.');
        content = '';
      } else {
        throw e;
      }
    }
    var numLines = content.split("\n").size;
 
    var coverage = new Array(numLines);
    coverage.forEach(function(elem, index, arr) {
      arr[index] = null;
    });
    elem.lines.details.forEach(function(lineDetail) {
      coverage[lineDetail.line - 1] = lineDetail.hit;
    });
 
    var fileName = path.relative(self.rootDirectory(), elem.file);
    Iif (path.sep !== '/') {
      fileName = fileName.split(path.sep).join('/');
    }
 
    source_files.push({
      name: fileName,
      blob_id: git.blobId(elem.file, content),
      coverage: JSON.stringify(coverage)
    });
  });
  return source_files;
};
 
module.exports = Formatter;