Commit | Line | Data |
---|---|---|
ab847f37 DV |
1 | #!/usr/bin/env node |
2 | /** | |
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. | |
6 | * | |
7 | * Usage: | |
8 | * | |
9 | * transform-coverage.js path/to/soure.map path/to/coverage.lcov > out.lcov | |
10 | */ | |
11 | ||
12 | // TODO: make this a command line argument | |
13 | var SOURCE = 'src/'; // only report files under this directory | |
14 | ||
15 | var assert = require('assert'), | |
16 | fs = require('fs'), | |
17 | lcovParse = require('lcov-parse'), | |
18 | parseDataUri = require('parse-data-uri'), | |
19 | sourcemap = require('source-map'); | |
20 | ||
21 | var sourcemapFile = process.argv[2]; | |
22 | var lcovFile = process.argv[3]; | |
23 | ||
24 | var sourcemapData = fs.readFileSync(sourcemapFile).toString(); | |
25 | var sourcemap = new sourcemap.SourceMapConsumer(sourcemapData); | |
26 | ||
27 | lcovParse(lcovFile, function(err, data) { | |
28 | assert(!err); | |
29 | // TODO: 0 --> the correct file | |
30 | var lines = data[0].lines.details; | |
31 | ||
32 | var fileToCov = {}; // filename -> { line num -> hits } | |
33 | ||
34 | lines.forEach(function(line) { | |
35 | var pos = sourcemap.originalPositionFor({line: line.line, column: 0}); | |
36 | if (pos == null) { | |
37 | return; | |
38 | } | |
39 | ||
40 | var filename = pos.source; | |
41 | ||
42 | // Test coverage of node_modules is never interesting. | |
43 | if (!filename || filename.indexOf('node_modules') >= 0) { | |
44 | return; | |
45 | } | |
46 | ||
47 | // Strip paths down to the source root. | |
48 | var base = filename.indexOf(SOURCE); | |
49 | if (base == -1) return; | |
50 | filename = filename.slice(base); | |
51 | ||
52 | if (!fileToCov[filename]) fileToCov[filename] = []; | |
53 | fileToCov[filename][pos.line] = line.hit; | |
54 | }); | |
55 | ||
56 | // Other LCOV fields to translate: | |
57 | // FN:2454 | |
58 | // FNF:465 | |
59 | // FNH:410 | |
60 | // FNDA:1,(anonymous_1) | |
61 | // LF:4570 | |
62 | // LH:4002 | |
63 | // BRDA:13,1,0,1 | |
64 | // BRF:2213 | |
65 | // BRH:1684 | |
66 | ||
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++) { | |
72 | if (cov[i] != null) { | |
73 | console.log('DA:' + i + ',' + cov[i]); | |
74 | } | |
75 | } | |
76 | console.log('end_of_record'); | |
77 | } | |
78 | }); |