context.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2012-2015, Yahoo Inc.
  3. Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
  4. */
  5. var FileWriter = require('./file-writer'),
  6. XMLWriter = require('./xml-writer'),
  7. tree = require('./tree'),
  8. watermarks = require('./watermarks'),
  9. fs = require('fs');
  10. function defaultSourceLookup(path) {
  11. try {
  12. return fs.readFileSync(path, 'utf8');
  13. } catch (ex) {
  14. throw new Error('Unable to lookup source: ' + path + '(' + ex.message + ')');
  15. }
  16. }
  17. function mergeWatermarks(specified, defaults) {
  18. specified = specified || {};
  19. Object.keys(defaults).forEach(function (k) {
  20. var specValue = specified[k];
  21. if (!(specValue && Array.isArray(specValue) && specValue.length === 2)) {
  22. specified[k] = defaults[k];
  23. }
  24. });
  25. return specified;
  26. }
  27. /**
  28. * A reporting context that is passed to report implementations
  29. * @param {Object} [opts=null] opts options
  30. * @param {String} [opts.dir='coverage'] opts.dir the reporting directory
  31. * @param {Object} [opts.watermarks=null] opts.watermarks watermarks for
  32. * statements, lines, branches and functions
  33. * @param {Function} [opts.sourceFinder=fsLookup] opts.sourceFinder a
  34. * function that returns source code given a file path. Defaults to
  35. * filesystem lookups based on path.
  36. * @constructor
  37. */
  38. function Context(opts) {
  39. opts = opts || {};
  40. this.dir = opts.dir || 'coverage';
  41. this.watermarks = mergeWatermarks(opts.watermarks, watermarks.getDefault());
  42. this.sourceFinder = opts.sourceFinder || defaultSourceLookup;
  43. this.data = {};
  44. }
  45. Object.defineProperty(Context.prototype, 'writer', {
  46. enumerable: true,
  47. get: function () {
  48. if (!this.data.writer) {
  49. this.data.writer = new FileWriter(this.dir);
  50. }
  51. return this.data.writer;
  52. }
  53. });
  54. /**
  55. * returns a FileWriter implementation for reporting use. Also available
  56. * as the `writer` property on the context.
  57. * @returns {Writer}
  58. */
  59. Context.prototype.getWriter = function () {
  60. return this.writer;
  61. };
  62. /**
  63. * returns the source code for the specified file path or throws if
  64. * the source could not be found.
  65. * @param {String} filePath the file path as found in a file coverage object
  66. * @returns {String} the source code
  67. */
  68. Context.prototype.getSource = function (filePath) {
  69. return this.sourceFinder(filePath);
  70. };
  71. /**
  72. * returns the coverage class given a coverage
  73. * types and a percentage value.
  74. * @param {String} type - the coverage type, one of `statements`, `functions`,
  75. * `branches`, or `lines`
  76. * @param {Number} value - the percentage value
  77. * @returns {String} one of `high`, `medium` or `low`
  78. */
  79. Context.prototype.classForPercent = function (type, value) {
  80. var watermarks = this.watermarks[type];
  81. if (!watermarks) {
  82. return 'unknown';
  83. }
  84. if (value < watermarks[0]) {
  85. return 'low';
  86. }
  87. if (value >= watermarks[1]) {
  88. return 'high';
  89. }
  90. return 'medium';
  91. };
  92. /**
  93. * returns an XML writer for the supplied content writer
  94. * @param {ContentWriter} contentWriter the content writer to which the returned XML writer
  95. * writes data
  96. * @returns {XMLWriter}
  97. */
  98. Context.prototype.getXMLWriter = function (contentWriter) {
  99. return new XMLWriter(contentWriter);
  100. };
  101. /**
  102. * returns a full visitor given a partial one.
  103. * @param {Object} partialVisitor a partial visitor only having the functions of
  104. * interest to the caller. These functions are called with a scope that is the
  105. * supplied object.
  106. * @returns {Visitor}
  107. */
  108. Context.prototype.getVisitor = function (partialVisitor) {
  109. return new tree.Visitor(partialVisitor);
  110. };
  111. module.exports = {
  112. create: function (opts) {
  113. return new Context(opts);
  114. }
  115. };