3 * This script applies a source map to LCOV data. If you have coverage data for
4 * a concatenated file, plus a source map, this will output LCOV data for your
5 * original source files.
9 * transform-coverage.js path/to/soure.map path/to/coverage.lcov > out.lcov
12 // TODO: make this a command line argument
13 var SOURCE
= 'src/'; // only report files under this directory
15 var assert = require('assert
'),
17 lcovParse = require('lcov
-parse
'),
18 parseDataUri = require('parse
-data
-uri
'),
19 sourcemap = require('source
-map
');
21 var sourcemapFile = process.argv[2];
22 var lcovFile = process.argv[3];
24 var sourcemapData = fs.readFileSync(sourcemapFile).toString();
25 var sourcemap = new sourcemap.SourceMapConsumer(sourcemapData);
27 lcovParse(lcovFile, function(err, data) {
29 // TODO: 0 --> the correct file
30 var lines = data[0].lines.details;
32 var fileToCov = {}; // filename -> { line num -> hits }
34 lines.forEach(function(line) {
35 var pos = sourcemap.originalPositionFor({line: line.line, column: 0});
40 var filename = pos.source;
42 // Test coverage of node_modules is never interesting.
43 if (!filename || filename.indexOf('node_modules
') >= 0) {
47 // Strip paths down to the source root.
48 var base = filename.indexOf(SOURCE);
49 if (base == -1) return;
50 filename = filename.slice(base);
52 if (!fileToCov[filename]) fileToCov[filename] = [];
53 fileToCov[filename][pos.line] = line.hit;
56 // Other LCOV fields to translate:
60 // FNDA:1,(anonymous_1)
67 // Convert to LCOV format
68 for (var filename in fileToCov) {
69 var cov = fileToCov[filename]
70 console.log('SF
:' + filename);
71 for (var i = 0; i < cov.length; i++) {
73 console.log('DA
:' + i + ',' + cov[i]);
76 console.log('end_of_record
');