-
Notifications
You must be signed in to change notification settings - Fork 8
/
lcov.js
47 lines (38 loc) · 1.3 KB
/
lcov.js
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
/**
* Expose `LCov`.
*/
exports = module.exports = LCov;
/**
* Initialize a new LCOV reporter.
* File format of LCOV can be found here: http://ltp.sourceforge.net/coverage/lcov/geninfo.1.php
* The reporter is built after this parser: https://raw.github.com/SonarCommunity/sonar-javascript/master/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/coverage/LCOVParser.java
*
* @param {Runner} runner
* @api public
*/
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;
}
for (var filename in cov) {
var data = cov[filename];
reportFile(filename, data);
}
});
}
function reportFile(filename, data) {
process.stdout.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');
}
});
process.stdout.write('end_of_record\n');
}