[6a3a178] | 1 | 'use strict';
|
---|
| 2 | /*
|
---|
| 3 | Copyright 2012-2015, Yahoo Inc.
|
---|
| 4 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
---|
| 5 | */
|
---|
| 6 | const { ReportBase } = require('istanbul-lib-report');
|
---|
| 7 |
|
---|
| 8 | class CloverReport extends ReportBase {
|
---|
| 9 | constructor(opts) {
|
---|
| 10 | super();
|
---|
| 11 |
|
---|
| 12 | this.cw = null;
|
---|
| 13 | this.xml = null;
|
---|
| 14 | this.projectRoot = opts.projectRoot || process.cwd();
|
---|
| 15 | this.file = opts.file || 'clover.xml';
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | onStart(root, context) {
|
---|
| 19 | this.cw = context.writer.writeFile(this.file);
|
---|
| 20 | this.xml = context.getXMLWriter(this.cw);
|
---|
| 21 | this.writeRootStats(root, context);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | onEnd() {
|
---|
| 25 | this.xml.closeAll();
|
---|
| 26 | this.cw.close();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | getTreeStats(node, context) {
|
---|
| 30 | const state = {
|
---|
| 31 | packages: 0,
|
---|
| 32 | files: 0,
|
---|
| 33 | classes: 0
|
---|
| 34 | };
|
---|
| 35 | const visitor = {
|
---|
| 36 | onSummary(node, state) {
|
---|
| 37 | const metrics = node.getCoverageSummary(true);
|
---|
| 38 | if (metrics) {
|
---|
| 39 | state.packages += 1;
|
---|
| 40 | }
|
---|
| 41 | },
|
---|
| 42 | onDetail(node, state) {
|
---|
| 43 | state.classes += 1;
|
---|
| 44 | state.files += 1;
|
---|
| 45 | }
|
---|
| 46 | };
|
---|
| 47 | node.visit(context.getVisitor(visitor), state);
|
---|
| 48 | return state;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | writeRootStats(node, context) {
|
---|
| 52 | this.cw.println('<?xml version="1.0" encoding="UTF-8"?>');
|
---|
| 53 | this.xml.openTag('coverage', {
|
---|
| 54 | generated: Date.now().toString(),
|
---|
| 55 | clover: '3.2.0'
|
---|
| 56 | });
|
---|
| 57 |
|
---|
| 58 | this.xml.openTag('project', {
|
---|
| 59 | timestamp: Date.now().toString(),
|
---|
| 60 | name: 'All files'
|
---|
| 61 | });
|
---|
| 62 |
|
---|
| 63 | const metrics = node.getCoverageSummary();
|
---|
| 64 | this.xml.inlineTag('metrics', {
|
---|
| 65 | statements: metrics.lines.total,
|
---|
| 66 | coveredstatements: metrics.lines.covered,
|
---|
| 67 | conditionals: metrics.branches.total,
|
---|
| 68 | coveredconditionals: metrics.branches.covered,
|
---|
| 69 | methods: metrics.functions.total,
|
---|
| 70 | coveredmethods: metrics.functions.covered,
|
---|
| 71 | elements:
|
---|
| 72 | metrics.lines.total +
|
---|
| 73 | metrics.branches.total +
|
---|
| 74 | metrics.functions.total,
|
---|
| 75 | coveredelements:
|
---|
| 76 | metrics.lines.covered +
|
---|
| 77 | metrics.branches.covered +
|
---|
| 78 | metrics.functions.covered,
|
---|
| 79 | complexity: 0,
|
---|
| 80 | loc: metrics.lines.total,
|
---|
| 81 | ncloc: metrics.lines.total, // what? copied as-is from old report
|
---|
| 82 | ...this.getTreeStats(node, context)
|
---|
| 83 | });
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | writeMetrics(metrics) {
|
---|
| 87 | this.xml.inlineTag('metrics', {
|
---|
| 88 | statements: metrics.lines.total,
|
---|
| 89 | coveredstatements: metrics.lines.covered,
|
---|
| 90 | conditionals: metrics.branches.total,
|
---|
| 91 | coveredconditionals: metrics.branches.covered,
|
---|
| 92 | methods: metrics.functions.total,
|
---|
| 93 | coveredmethods: metrics.functions.covered
|
---|
| 94 | });
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | onSummary(node) {
|
---|
| 98 | if (node.isRoot()) {
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 | const metrics = node.getCoverageSummary(true);
|
---|
| 102 | if (!metrics) {
|
---|
| 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | this.xml.openTag('package', {
|
---|
| 107 | name: asJavaPackage(node)
|
---|
| 108 | });
|
---|
| 109 | this.writeMetrics(metrics);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | onSummaryEnd(node) {
|
---|
| 113 | if (node.isRoot()) {
|
---|
| 114 | return;
|
---|
| 115 | }
|
---|
| 116 | this.xml.closeTag('package');
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | onDetail(node) {
|
---|
| 120 | const fileCoverage = node.getFileCoverage();
|
---|
| 121 | const metrics = node.getCoverageSummary();
|
---|
| 122 | const branchByLine = fileCoverage.getBranchCoverageByLine();
|
---|
| 123 |
|
---|
| 124 | this.xml.openTag('file', {
|
---|
| 125 | name: asClassName(node),
|
---|
| 126 | path: fileCoverage.path
|
---|
| 127 | });
|
---|
| 128 |
|
---|
| 129 | this.writeMetrics(metrics);
|
---|
| 130 |
|
---|
| 131 | const lines = fileCoverage.getLineCoverage();
|
---|
| 132 | Object.entries(lines).forEach(([k, count]) => {
|
---|
| 133 | const attrs = {
|
---|
| 134 | num: k,
|
---|
| 135 | count,
|
---|
| 136 | type: 'stmt'
|
---|
| 137 | };
|
---|
| 138 | const branchDetail = branchByLine[k];
|
---|
| 139 |
|
---|
| 140 | if (branchDetail) {
|
---|
| 141 | attrs.type = 'cond';
|
---|
| 142 | attrs.truecount = branchDetail.covered;
|
---|
| 143 | attrs.falsecount = branchDetail.total - branchDetail.covered;
|
---|
| 144 | }
|
---|
| 145 | this.xml.inlineTag('line', attrs);
|
---|
| 146 | });
|
---|
| 147 |
|
---|
| 148 | this.xml.closeTag('file');
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | function asJavaPackage(node) {
|
---|
| 153 | return node
|
---|
| 154 | .getRelativeName()
|
---|
| 155 | .replace(/\//g, '.')
|
---|
| 156 | .replace(/\\/g, '.')
|
---|
| 157 | .replace(/\.$/, '');
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | function asClassName(node) {
|
---|
| 161 | return node.getRelativeName().replace(/.*[\\/]/, '');
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | module.exports = CloverReport;
|
---|