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