Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma-coverage / lib / reporter.js
1 var path = require('path');
2 var fs = require('fs');
3 var util = require('util');
4 var istanbul = require('istanbul');
5 var dateformat = require('dateformat');
6 var globalSourceCache = require('./sourceCache');
7
8
9 var Store = istanbul.Store;
10
11 var SourceCacheStore = function(opts) {
12   Store.call(this, opts);
13   opts = opts || {};
14   this.sourceCache = opts.sourceCache;
15 };
16 SourceCacheStore.TYPE = 'sourceCacheLookup';
17 util.inherits(SourceCacheStore, Store);
18
19 Store.mix(SourceCacheStore, {
20   keys : function() {
21     throw 'not implemented';
22   },
23   get : function(key) {
24     return this.sourceCache[key];
25   },
26   hasKey : function(key) {
27     return this.sourceCache.hasOwnProperty(key);
28   },
29   set : function(key, contents) {
30     throw 'not applicable';
31   }
32 });
33
34
35 // TODO(vojta): inject only what required (config.basePath, config.coverageReporter)
36 var CoverageReporter = function(rootConfig, helper, logger) {
37   var log = logger.create('coverage');
38   var config = rootConfig.coverageReporter || {};
39   var basePath = rootConfig.basePath;
40   var reporters = config.reporters;
41   var sourceCache = globalSourceCache.getByBasePath(basePath);
42
43   if (!helper.isDefined(reporters)) {
44     reporters = [config];
45   }
46
47   this.adapters = [];
48   var collectors;
49   var pendingFileWritings = 0;
50   var fileWritingFinished = function() {};
51
52   function writeEnd() {
53     if (!--pendingFileWritings) {
54       // cleanup collectors
55       Object.keys(collectors).forEach(function(key) {
56          collectors[key].dispose();
57       });
58       fileWritingFinished();
59     }
60   }
61
62   /**
63    * Generate the output directory from the `coverageReporter.dir` and
64    * `coverageReporter.subdir` options.
65    *
66    * @param {String} browserName - The browser name
67    * @param {String} dir - The given option
68    * @param {String|Function} subdir - The given option
69    *
70    * @return {String} - The output directory
71    */
72   function generateOutputDir(browserName, dir, subdir) {
73     dir = dir || 'coverage';
74     subdir = subdir || browserName;
75
76     if (typeof subdir === 'function') {
77       subdir = subdir(browserName);
78     }
79
80     return path.join(dir, subdir);
81   }
82
83   this.onRunStart = function(browsers) {
84     collectors = Object.create(null);
85
86     // TODO(vojta): remove once we don't care about Karma 0.10
87     if (browsers) {
88       browsers.forEach(function(browser) {
89         collectors[browser.id] = new istanbul.Collector();
90       });
91     }
92   };
93
94   this.onBrowserStart = function(browser) {
95     collectors[browser.id] = new istanbul.Collector();
96   };
97
98   this.onBrowserComplete = function(browser, result) {
99     var collector = collectors[browser.id];
100
101     if (!collector) {
102       return;
103     }
104
105     if (result && result.coverage) {
106       collector.add(result.coverage);
107     }
108   };
109
110   this.onSpecComplete = function(browser, result) {
111     if (result.coverage) {
112       collectors[browser.id].add(result.coverage);
113     }
114   };
115
116   this.onRunComplete = function(browsers) {
117     reporters.forEach(function(reporterConfig) {
118       browsers.forEach(function(browser) {
119
120         var collector = collectors[browser.id];
121         if (collector) {
122           pendingFileWritings++;
123
124           var outputDir = helper.normalizeWinPath(path.resolve(basePath, generateOutputDir(browser.name,
125                                                                                            reporterConfig.dir || config.dir,
126                                                                                            reporterConfig.subdir || config.subdir)));
127
128           helper.mkdirIfNotExists(outputDir, function() {
129             log.debug('Writing coverage to %s', outputDir);
130             var options = helper.merge({}, reporterConfig, {
131               dir : outputDir,
132               sourceStore : new SourceCacheStore({
133                 sourceCache: sourceCache
134               })
135             });
136             var reporter = istanbul.Report.create(reporterConfig.type || 'html', options);
137             try {
138               reporter.writeReport(collector, true);
139             } catch (e) {
140               log.error(e);
141             }
142             writeEnd();
143           });
144         }
145
146       });
147     });
148   };
149
150   this.onExit = function(done) {
151     if (pendingFileWritings) {
152       fileWritingFinished = done;
153     } else {
154       done();
155     }
156   };
157 };
158
159 CoverageReporter.$inject = ['config', 'helper', 'logger'];
160
161 // PUBLISH
162 module.exports = CoverageReporter;