context.js 3.7 KB

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