API Docs for: 0.3.0
Show:

File: lib/report/text-summary.js

  1. /*
  2. Copyright (c) 2012, Yahoo! Inc. All rights reserved.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5.  
  6. var path = require('path'),
  7. util = require('util'),
  8. mkdirp = require('mkdirp'),
  9. defaults = require('./common/defaults'),
  10. fs = require('fs'),
  11. utils = require('../object-utils'),
  12. Report = require('./index');
  13.  
  14. /**
  15. * a `Report` implementation that produces text output for overall coverage in summary format.
  16. *
  17. * Usage
  18. * -----
  19. *
  20. * var report = require('istanbul').Report.create('text-summary');
  21. *
  22. * @class TextSummaryReport
  23. * @extends Report
  24. * @module report
  25. * @constructor
  26. * @param {Object} opts optional
  27. * @param {String} [opts.dir] the directory in which to the text coverage report will be written, when writing to a file
  28. * @param {String} [opts.file] the filename for the report. When omitted, the report is written to console
  29. */
  30. function TextSummaryReport(opts) {
  31. Report.call(this);
  32. opts = opts || {};
  33. this.dir = opts.dir || process.cwd();
  34. this.file = opts.file;
  35. this.watermarks = opts.watermarks || defaults.watermarks();
  36. }
  37.  
  38. TextSummaryReport.TYPE = 'text-summary';
  39. util.inherits(TextSummaryReport, Report);
  40.  
  41. function lineForKey(summary, key, watermarks) {
  42. var metrics = summary[key],
  43. skipped,
  44. result,
  45. clazz = defaults.classFor(key, summary, watermarks);
  46. key = key.substring(0, 1).toUpperCase() + key.substring(1);
  47. if (key.length < 12) { key += ' '.substring(0, 12 - key.length); }
  48. result = [ key , ':', metrics.pct + '%', '(', metrics.covered + '/' + metrics.total, ')'].join(' ');
  49. skipped = metrics.skipped;
  50. if (skipped > 0) {
  51. result += ', ' + skipped + ' ignored';
  52. }
  53. return defaults.colorize(result, clazz);
  54. }
  55.  
  56. Report.mix(TextSummaryReport, {
  57. synopsis: function () {
  58. return 'text report that prints a coverage summary across all files, typically to console';
  59. },
  60. getDefaultConfig: function () {
  61. return { file: null };
  62. },
  63. writeReport: function (collector /*, sync */) {
  64. var summaries = [],
  65. finalSummary,
  66. lines = [],
  67. watermarks = this.watermarks,
  68. text;
  69. collector.files().forEach(function (file) {
  70. summaries.push(utils.summarizeFileCoverage(collector.fileCoverageFor(file)));
  71. });
  72. finalSummary = utils.mergeSummaryObjects.apply(null, summaries);
  73. lines.push('');
  74. lines.push('=============================== Coverage summary ===============================');
  75. lines.push.apply(lines, [
  76. lineForKey(finalSummary, 'statements', watermarks),
  77. lineForKey(finalSummary, 'branches', watermarks),
  78. lineForKey(finalSummary, 'functions', watermarks),
  79. lineForKey(finalSummary, 'lines', watermarks)
  80. ]);
  81. lines.push('================================================================================');
  82. text = lines.join('\n');
  83. if (this.file) {
  84. mkdirp.sync(this.dir);
  85. fs.writeFileSync(path.join(this.dir, this.file), text, 'utf8');
  86. } else {
  87. console.log(text);
  88. }
  89. this.emit('done');
  90. }
  91. });
  92.  
  93. module.exports = TextSummaryReport;
  94.