-
Notifications
You must be signed in to change notification settings - Fork 248
/
reporter.js
163 lines (136 loc) · 4.11 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
var path = require('path');
var fs = require('fs');
var util = require('util');
var istanbul = require('istanbul');
var dateformat = require('dateformat');
var Store = istanbul.Store;
var BasePathStore = function(opts) {
Store.call(this, opts);
opts = opts || {};
this.basePath = opts.basePath;
this.delegate = Store.create('fslookup');
};
BasePathStore.TYPE = 'basePathlookup';
util.inherits(BasePathStore, Store);
Store.mix(BasePathStore, {
keys : function() {
return this.delegate.keys();
},
toKey : function(key) {
if (key.indexOf('./') === 0) { return path.join(this.basePath, key); }
return key;
},
get : function(key) {
return this.delegate.get(this.toKey(key));
},
hasKey : function(key) {
return this.delegate.hasKey(this.toKey(key));
},
set : function(key, contents) {
return this.delegate.set(this.toKey(key), contents);
}
});
// 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;
if (!helper.isDefined(reporters)) {
reporters = [config];
}
this.adapters = [];
var collectors;
var pendingFileWritings = 0;
var fileWritingFinished = function() {};
function writeEnd() {
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(function(browser) {
collectors[browser.id] = new istanbul.Collector();
});
}
};
this.onBrowserStart = function(browser) {
collectors[browser.id] = new istanbul.Collector();
};
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, config.dir, config.subdir)));
helper.mkdirIfNotExists(outputDir, function() {
log.debug('Writing coverage to %s', outputDir);
var options = helper.merge({}, reporterConfig, {
dir : outputDir,
sourceStore : new BasePathStore({
basePath : basePath
})
});
var reporter = istanbul.Report.create(reporterConfig.type || 'html', options);
try {
reporter.writeReport(collector, true);
} catch (e) {
log.error(e);
}
writeEnd();
});
}
});
});
};
this.onExit = function(done) {
if (pendingFileWritings) {
fileWritingFinished = done;
} else {
done();
}
};
};
CoverageReporter.$inject = ['config', 'helper', 'logger'];
// PUBLISH
module.exports = CoverageReporter;