source: trip-planner-front/node_modules/istanbul-reports/lib/cobertura/index.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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