| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
const path = require('path'); |
| 7 |
const { escape } = require('html-escaper'); |
| 8 |
const { ReportBase } = require('istanbul-lib-report'); |
| 9 |
|
| 10 |
class CoberturaReport extends ReportBase { |
| 11 |
constructor(opts) { |
| 12 |
super(); |
| 13 |
|
| 14 |
opts = opts || {}; |
| 15 |
|
| 16 |
this.cw = null; |
| 17 |
this.xml = null; |
| 18 |
this.timestamp = opts.timestamp || Date.now().toString(); |
| 19 |
this.projectRoot = opts.projectRoot || process.cwd(); |
| 20 |
this.file = opts.file || 'cobertura-coverage.xml'; |
| 21 |
} |
| 22 |
|
| 23 |
onStart(root, context) { |
| 24 |
this.cw = context.writer.writeFile(this.file); |
| 25 |
this.xml = context.getXMLWriter(this.cw); |
| 26 |
this.writeRootStats(root); |
| 27 |
} |
| 28 |
|
| 29 |
onEnd() { |
| 30 |
this.xml.closeAll(); |
| 31 |
this.cw.close(); |
| 32 |
} |
| 33 |
|
| 34 |
writeRootStats(node) { |
| 35 |
const metrics = node.getCoverageSummary(); |
| 36 |
this.cw.println('<?xml version="1.0" ?>'); |
| 37 |
this.cw.println( |
| 38 |
'<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">' |
| 39 |
); |
| 40 |
this.xml.openTag('coverage', { |
| 41 |
'lines-valid': metrics.lines.total, |
| 42 |
'lines-covered': metrics.lines.covered, |
| 43 |
'line-rate': metrics.lines.pct / 100.0, |
| 44 |
'branches-valid': metrics.branches.total, |
| 45 |
'branches-covered': metrics.branches.covered, |
| 46 |
'branch-rate': metrics.branches.pct / 100.0, |
| 47 |
timestamp: this.timestamp, |
| 48 |
complexity: '0', |
| 49 |
version: '0.1' |
| 50 |
}); |
| 51 |
this.xml.openTag('sources'); |
| 52 |
this.xml.inlineTag('source', null, this.projectRoot); |
| 53 |
this.xml.closeTag('sources'); |
| 54 |
this.xml.openTag('packages'); |
| 55 |
} |
| 56 |
|
| 57 |
onSummary(node) { |
| 58 |
const metrics = node.getCoverageSummary(true); |
| 59 |
if (!metrics) { |
| 60 |
return; |
| 61 |
} |
| 62 |
this.xml.openTag('package', { |
| 63 |
name: node.isRoot() ? 'main' : escape(asJavaPackage(node)), |
| 64 |
'line-rate': metrics.lines.pct / 100.0, |
| 65 |
'branch-rate': metrics.branches.pct / 100.0 |
| 66 |
}); |
| 67 |
this.xml.openTag('classes'); |
| 68 |
} |
| 69 |
|
| 70 |
onSummaryEnd(node) { |
| 71 |
const metrics = node.getCoverageSummary(true); |
| 72 |
if (!metrics) { |
| 73 |
return; |
| 74 |
} |
| 75 |
this.xml.closeTag('classes'); |
| 76 |
this.xml.closeTag('package'); |
| 77 |
} |
| 78 |
|
| 79 |
onDetail(node) { |
| 80 |
const fileCoverage = node.getFileCoverage(); |
| 81 |
const metrics = node.getCoverageSummary(); |
| 82 |
const branchByLine = fileCoverage.getBranchCoverageByLine(); |
| 83 |
|
| 84 |
this.xml.openTag('class', { |
| 85 |
name: escape(asClassName(node)), |
| 86 |
filename: path.relative(this.projectRoot, fileCoverage.path), |
| 87 |
'line-rate': metrics.lines.pct / 100.0, |
| 88 |
'branch-rate': metrics.branches.pct / 100.0 |
| 89 |
}); |
| 90 |
|
| 91 |
this.xml.openTag('methods'); |
| 92 |
const fnMap = fileCoverage.fnMap; |
| 93 |
Object.entries(fnMap).forEach(([k, { name, decl }]) => { |
| 94 |
const hits = fileCoverage.f[k]; |
| 95 |
this.xml.openTag('method', { |
| 96 |
name: escape(name), |
| 97 |
hits, |
| 98 |
signature: '()V' |
| 99 |
}); |
| 100 |
this.xml.openTag('lines'); |
| 101 |
|
| 102 |
this.xml.inlineTag('line', { |
| 103 |
number: decl.start.line, |
| 104 |
hits |
| 105 |
}); |
| 106 |
this.xml.closeTag('lines'); |
| 107 |
this.xml.closeTag('method'); |
| 108 |
}); |
| 109 |
this.xml.closeTag('methods'); |
| 110 |
|
| 111 |
this.xml.openTag('lines'); |
| 112 |
const lines = fileCoverage.getLineCoverage(); |
| 113 |
Object.entries(lines).forEach(([k, hits]) => { |
| 114 |
const attrs = { |
| 115 |
number: k, |
| 116 |
hits, |
| 117 |
branch: 'false' |
| 118 |
}; |
| 119 |
const branchDetail = branchByLine[k]; |
| 120 |
|
| 121 |
if (branchDetail) { |
| 122 |
attrs.branch = true; |
| 123 |
attrs['condition-coverage'] = |
| 124 |
branchDetail.coverage + |
| 125 |
'% (' + |
| 126 |
branchDetail.covered + |
| 127 |
'/' + |
| 128 |
branchDetail.total + |
| 129 |
')'; |
| 130 |
} |
| 131 |
this.xml.inlineTag('line', attrs); |
| 132 |
}); |
| 133 |
|
| 134 |
this.xml.closeTag('lines'); |
| 135 |
this.xml.closeTag('class'); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
function asJavaPackage(node) { |
| 140 |
return node |
| 141 |
.getRelativeName() |
| 142 |
.replace(/\//g, '.') |
| 143 |
.replace(/\\/g, '.') |
| 144 |
.replace(/\.$/, ''); |
| 145 |
} |
| 146 |
|
| 147 |
function asClassName(node) { |
| 148 |
return node.getRelativeName().replace(/.*[\\/]/, ''); |
| 149 |
} |
| 150 |
|
| 151 |
module.exports = CoberturaReport; |