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