UNPKG

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