index.js 4.3 KB

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