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