-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoverage-csv.js
83 lines (74 loc) · 2.75 KB
/
coverage-csv.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
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
var path = require('path'),
fs = require('fs'),
istanbul = require('istanbul'),
Report = istanbul.Report,
Collector = istanbul.Collector,
mkdirp = require('./node_modules/mkdirp'),
utils = require('./node_modules/istanbul/lib/object-utils'),
filesFor = require('./node_modules/istanbul/lib/util/file-matcher').filesFor;
function CsvSummaryReport(opts) {
Report.call(this);
opts = opts || {};
this.dir = opts.dir || process.cwd();
this.file = opts.file;
}
CsvSummaryReport.TYPE = 'csv-summary';
Report.register(CsvSummaryReport);
function headingsForKey(key) {
return [key + '-coverage', key + '-covered', key + '-total'].join(',');
}
function metricsForKey(summary, key) {
var metrics = summary[key];
return [metrics.pct, metrics.covered, metrics.total].join(',');
}
Report.mix(CsvSummaryReport, {
writeReport: function(collector) {
var summaries = [],
finalSummary,
headings = [],
values = [],
text;
collector.files().forEach(function(file) {
summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
});
finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
headings.push(headingsForKey('statements'));
headings.push(headingsForKey('branches'));
headings.push(headingsForKey('functions'));
headings.push(headingsForKey('lines'));
values.push(metricsForKey(finalSummary, 'statements'));
values.push(metricsForKey(finalSummary, 'branches'));
values.push(metricsForKey(finalSummary, 'functions'));
values.push(metricsForKey(finalSummary, 'lines'));
text = headings.join(',') + '\n' + values.join(',') + '\n';
if (this.file) {
mkdirp.sync(this.dir);
fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
} else {
console.log(text);
}
}
});
var buildreports_location = process.argv[2] || 'buildreports';
var reporter = Report.create('csv-summary', {root: buildreports_location,
dir: buildreports_location, file: 'coverage.csv'});
var collector = new Collector();
filesFor({
root: buildreports_location,
includes: ['coverage.json']
}, function(err, files) {
if (!err && !files.length) {
console.log("ERROR: Could not find coverage.json in \"" + buildreports_location + "\"");
return 1;
}
console.log('Using reporter [csv-summary]');
if (err) {
throw err;
}
files.forEach(function(file) {
var coverageObject = JSON.parse(fs.readFileSync(file, 'utf8'));
collector.add(coverageObject);
});
reporter.writeReport(collector);
console.log('Done');
});