index.js 4.4 KB

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