Skip to content

Commit

Permalink
Make writing to output monkey-patchable. #16
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Feb 19, 2017
1 parent 954087f commit fe526ff
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions lib/lcov.js
Expand Up @@ -15,33 +15,45 @@ exports = module.exports = LCov;
*/

function LCov(runner) {
runner.on('end', function(){
// In a browser context, coverage will be in window.$jscoverage or window._$blanket.
var g = typeof(global) != 'undefined' ? global : window;
var cov = g._$jscoverage || g._$blanket;
if (!cov) {
console.error('mocha-lcov-reporter: No coverage data found, make sure your code is properly instrumented');
return;
}
var self = this;

for (var filename in cov) {
var data = cov[filename];
reportFile(filename, data);
}
runner.on('end', function() {
self.onEnd();
});
}

function reportFile(filename, data) {
process.stdout.write('SF:' + filename + '\n');
LCov.prototype.onEnd = function() {
// In a browser context, coverage will be in window.$jscoverage or window._$blanket.
var g = typeof(global) != 'undefined' ? global : window;
var cov = g._$jscoverage || g._$blanket;
if (!cov) {
console.error('mocha-lcov-reporter: No coverage data found, make sure your code is properly instrumented');
return;
}

for (var filename in cov) {
var data = cov[filename];
this.reportFile(filename, data);
}
}

LCov.prototype.reportFile = function(filename, data) {
var self = this;

self.write('SF:' + filename + '\n');

data.source.forEach(function(line, num) {
// increase the line number, as JS arrays are zero-based
num++;

if (data[num] !== undefined) {
process.stdout.write('DA:' + num + ',' + data[num] + '\n');
self.write('DA:' + num + ',' + data[num] + '\n');
}
});

process.stdout.write('end_of_record\n');
self.write('end_of_record\n');
}

LCov.prototype.write = function(string) {
process.stdout.write(string);
}

0 comments on commit fe526ff

Please sign in to comment.