f73bf9c4fe09b73669b900d48a62c0bf42ab37c6.svn-base
4.14 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
const path = require('path');
function CoberturaReport(opts) {
this.cw = null;
this.xml = null;
this.projectRoot = opts.projectRoot || process.cwd();
this.file = opts.file || 'cobertura-coverage.xml';
}
function asJavaPackage(node) {
return node
.getRelativeName()
.replace(/\//g, '.')
.replace(/\\/g, '.')
.replace(/\.$/, '');
}
function asClassName(node) {
return node.getRelativeName().replace(/.*[\\/]/, '');
}
CoberturaReport.prototype.onStart = function(root, context) {
this.cw = context.writer.writeFile(this.file);
this.xml = context.getXMLWriter(this.cw);
this.writeRootStats(root);
};
CoberturaReport.prototype.onEnd = function() {
this.xml.closeAll();
this.cw.close();
};
CoberturaReport.prototype.writeRootStats = function(node) {
const metrics = node.getCoverageSummary();
this.cw.println('<?xml version="1.0" ?>');
this.cw.println(
'<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">'
);
this.xml.openTag('coverage', {
'lines-valid': metrics.lines.total,
'lines-covered': metrics.lines.covered,
'line-rate': metrics.lines.pct / 100.0,
'branches-valid': metrics.branches.total,
'branches-covered': metrics.branches.covered,
'branch-rate': metrics.branches.pct / 100.0,
timestamp: Date.now().toString(),
complexity: '0',
version: '0.1'
});
this.xml.openTag('sources');
this.xml.inlineTag('source', null, this.projectRoot);
this.xml.closeTag('sources');
this.xml.openTag('packages');
};
CoberturaReport.prototype.onSummary = function(node) {
if (node.isRoot()) {
return;
}
const metrics = node.getCoverageSummary(true);
if (!metrics) {
return;
}
this.xml.openTag('package', {
name: asJavaPackage(node),
'line-rate': metrics.lines.pct / 100.0,
'branch-rate': metrics.branches.pct / 100.0
});
this.xml.openTag('classes');
};
CoberturaReport.prototype.onSummaryEnd = function(node) {
if (node.isRoot()) {
return;
}
this.xml.closeTag('classes');
this.xml.closeTag('package');
};
CoberturaReport.prototype.onDetail = function(node) {
const fileCoverage = node.getFileCoverage();
const metrics = node.getCoverageSummary();
const branchByLine = fileCoverage.getBranchCoverageByLine();
this.xml.openTag('class', {
name: asClassName(node),
filename: path.relative(this.projectRoot, fileCoverage.path),
'line-rate': metrics.lines.pct / 100.0,
'branch-rate': metrics.branches.pct / 100.0
});
this.xml.openTag('methods');
const fnMap = fileCoverage.fnMap;
Object.keys(fnMap).forEach(k => {
const name = fnMap[k].name;
const hits = fileCoverage.f[k];
this.xml.openTag('method', {
name,
hits,
signature: '()V' //fake out a no-args void return
});
this.xml.openTag('lines');
//Add the function definition line and hits so that jenkins cobertura plugin records method hits
this.xml.inlineTag('line', {
number: fnMap[k].decl.start.line,
hits
});
this.xml.closeTag('lines');
this.xml.closeTag('method');
});
this.xml.closeTag('methods');
this.xml.openTag('lines');
const lines = fileCoverage.getLineCoverage();
Object.keys(lines).forEach(k => {
const attrs = {
number: k,
hits: lines[k],
branch: 'false'
};
const branchDetail = branchByLine[k];
if (branchDetail) {
attrs.branch = true;
attrs['condition-coverage'] =
branchDetail.coverage +
'% (' +
branchDetail.covered +
'/' +
branchDetail.total +
')';
}
this.xml.inlineTag('line', attrs);
});
this.xml.closeTag('lines');
this.xml.closeTag('class');
};
module.exports = CoberturaReport;