index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. "use strict";
  6. var PCT_COLS = 9,
  7. MISSING_COL = 18,
  8. TAB_SIZE = 1,
  9. DELIM = ' |',
  10. COL_DELIM = '-|';
  11. function padding(num, ch) {
  12. var str = '',
  13. i;
  14. ch = ch || ' ';
  15. for (i = 0; i < num; i += 1) {
  16. str += ch;
  17. }
  18. return str;
  19. }
  20. function fill(str, width, right, tabs) {
  21. tabs = tabs || 0;
  22. str = String(str);
  23. var leadingSpaces = tabs * TAB_SIZE,
  24. remaining = width - leadingSpaces,
  25. leader = padding(leadingSpaces),
  26. fmtStr = '',
  27. fillStr,
  28. strlen = str.length;
  29. if (remaining > 0) {
  30. if (remaining >= strlen) {
  31. fillStr = padding(remaining - strlen);
  32. fmtStr = right ? fillStr + str : str + fillStr;
  33. } else {
  34. fmtStr = str.substring(strlen - remaining);
  35. fmtStr = '... ' + fmtStr.substring(4);
  36. }
  37. }
  38. return leader + fmtStr;
  39. }
  40. function formatName(name, maxCols, level) {
  41. return fill(name, maxCols, false, level);
  42. }
  43. function formatPct(pct, width) {
  44. return fill(pct, width || PCT_COLS, true, 0);
  45. }
  46. function nodeName(node) {
  47. return node.getRelativeName() || 'All files';
  48. }
  49. function depthFor(node) {
  50. var ret = 0;
  51. node = node.getParent();
  52. while (node) {
  53. ret += 1;
  54. node = node.getParent();
  55. }
  56. return ret;
  57. }
  58. function findNameWidth(node, context) {
  59. var last = 0,
  60. compareWidth = function (node) {
  61. var depth = depthFor(node),
  62. idealWidth = TAB_SIZE * depth + nodeName(node).length;
  63. if (idealWidth > last) {
  64. last = idealWidth;
  65. }
  66. },
  67. visitor = {
  68. onSummary: function (node) {
  69. compareWidth(node);
  70. },
  71. onDetail: function (node) {
  72. compareWidth(node);
  73. }
  74. };
  75. node.visit(context.getVisitor(visitor));
  76. return last;
  77. }
  78. function makeLine(nameWidth) {
  79. var name = padding(nameWidth, '-'),
  80. pct = padding(PCT_COLS, '-'),
  81. elements = [];
  82. elements.push(name);
  83. elements.push(pct);
  84. elements.push(pct);
  85. elements.push(pct);
  86. elements.push(pct);
  87. elements.push(padding(MISSING_COL, '-'));
  88. return elements.join(COL_DELIM) + COL_DELIM;
  89. }
  90. function tableHeader(maxNameCols) {
  91. var elements = [];
  92. elements.push(formatName('File', maxNameCols, 0));
  93. elements.push(formatPct('% Stmts'));
  94. elements.push(formatPct('% Branch'));
  95. elements.push(formatPct('% Funcs'));
  96. elements.push(formatPct('% Lines'));
  97. elements.push(formatPct('Uncovered Line #s', MISSING_COL));
  98. return elements.join(' |') + ' |';
  99. }
  100. function missingLines (node, colorizer) {
  101. var missingLines = node.isSummary() ? [] : node.getFileCoverage().getUncoveredLines();
  102. return colorizer(formatPct(missingLines.join(','), MISSING_COL), 'low');
  103. }
  104. function missingBranches (node, colorizer) {
  105. var branches = node.isSummary() ? {} : node.getFileCoverage().getBranchCoverageByLine(),
  106. missingLines = Object.keys(branches).filter(function (key) {
  107. return branches[key].coverage < 100;
  108. }).map(function (key) {
  109. return key;
  110. });
  111. return colorizer(formatPct(missingLines.join(','), MISSING_COL), 'medium');
  112. }
  113. function tableRow(node, context, colorizer, maxNameCols, level) {
  114. var name = nodeName(node),
  115. metrics = node.getCoverageSummary(),
  116. mm = {
  117. statements: metrics.statements.pct,
  118. branches: metrics.branches.pct,
  119. functions: metrics.functions.pct,
  120. lines: metrics.lines.pct,
  121. },
  122. colorize = function (str, key) {
  123. return colorizer(str, context.classForPercent(key, mm[key]));
  124. },
  125. elements = [];
  126. elements.push(colorize(formatName(name, maxNameCols, level),'statements'));
  127. elements.push(colorize(formatPct(mm.statements),'statements'));
  128. elements.push(colorize(formatPct(mm.branches), 'branches'));
  129. elements.push(colorize(formatPct(mm.functions), 'functions'));
  130. elements.push(colorize(formatPct(mm.lines), 'lines'));
  131. if (mm.lines === 100) {
  132. elements.push(missingBranches(node, colorizer));
  133. } else {
  134. elements.push(missingLines(node, colorizer));
  135. }
  136. return elements.join(DELIM) + DELIM;
  137. }
  138. function TextReport(opts) {
  139. opts = opts || {};
  140. this.file = opts.file || null;
  141. this.maxCols = opts.maxCols || 0;
  142. this.cw = null;
  143. }
  144. TextReport.prototype.onStart = function (root, context) {
  145. var line,
  146. statsWidth = 4 * (PCT_COLS + 2) + MISSING_COL,
  147. maxRemaining;
  148. this.cw = context.writer.writeFile(this.file);
  149. this.nameWidth = findNameWidth(root, context);
  150. if (this.maxCols > 0) {
  151. maxRemaining = this.maxCols - statsWidth - 2;
  152. if (this.nameWidth > maxRemaining) {
  153. this.nameWidth = maxRemaining;
  154. }
  155. }
  156. line = makeLine(this.nameWidth);
  157. this.cw.println(line);
  158. this.cw.println(tableHeader(this.nameWidth));
  159. this.cw.println(line);
  160. };
  161. TextReport.prototype.onSummary = function (node, context) {
  162. var nodeDepth = depthFor(node);
  163. this.cw.println(tableRow(node, context, this.cw.colorize.bind(this.cw),this.nameWidth, nodeDepth));
  164. };
  165. TextReport.prototype.onDetail = function (node, context) {
  166. return this.onSummary(node, context);
  167. };
  168. TextReport.prototype.onEnd = function () {
  169. this.cw.println(makeLine(this.nameWidth));
  170. this.cw.close();
  171. };
  172. module.exports = TextReport;