index.js 4.5 KB

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