index.js 4.5 KB

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