forked from benbria/karma-coffee-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (82 loc) · 3.27 KB
/
index.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
"use strict";
var fs = require('fs');
var path = require('path');
var coffeeCoverage = require('iced-coffee-coverage');
var CoverageInstrumentor = coffeeCoverage.CoverageInstrumentor;
var ISTANBUL_COVERAGE_VAR = '__coverage__';
/**
* Mimics the `StringStream` class found in `coffee-coverage`. We use it to get init data from all our instrumented
* files written to us.
*/
var StringStream = (function() {
function StringStream() {
this.data = "";
}
StringStream.prototype.write = function(data) {
this.data += data;
};
return StringStream;
})();
/**
* Write a file with all the initialization code for all the files we intend to report on.
*
* Will do all of its work as soon as karma requires it. It will walkn `sourcesBasePath`, instrument it, take all the
* init code and write that code to `dest`. You can then tell karma to include this file to get a zero count for _all_
* files you wish to know about.
*/
var initCoveragePlugin = function(config, logger, helper) {
var log = logger.create('framework.iced-coffee-coverage');
var basePath, dest, defaultOptions, instrumentor, initJs, options, singleOptions;
config = config || {};
config.framwork = config.framwork || {};
if (!config.framework.initAllSources) return;
basePath = config.framework.sourcesBasePath;
dest = config.framework.dest;
if (!basePath || !dest) {
log.warn('Need `sourcesBasePath` and `dest` for framework');
return;
}
defaultOptions = {};
if (config.framework.instrumentor === 'istanbul') {
defaultOptions.coverageVar = ISTANBUL_COVERAGE_VAR;
}
options = helper.merge({}, defaultOptions, config.framework);
instrumentor = new CoverageInstrumentor(options);
initJs = new StringStream();
singleOptions = helper.merge(options, {
initFileStream: initJs
});
instrumentor.instrumentDirectory(basePath, null, singleOptions);
fs.writeFileSync(dest, initJs.data);
};
initCoveragePlugin.$inject = ['config.icedCoffeeCoverage', 'logger', 'helper'];
/**
* Transform a coffee file into js source that is instrumented with iced-coffee-coverage
*/
var createPreprocessor = function(args, config, logger, helper) {
var log = logger.create('preprocessor.iced-coffee-coverage');
var options = {};
config = config || {};
config.preprocessor = config.preprocessor || {};
if (config.preprocessor.instrumentor === 'istanbul') {
options.coverageVar = ISTANBUL_COVERAGE_VAR;
}
options = helper.merge({}, options, config.preprocessor);
var instrumentor = new CoverageInstrumentor(options);
return function (content, file, done) {
try {
var instrumented = instrumentor.instrumentCoffee(path.resolve(file.originalPath), content);
var js = options.noInit ? instrumented.js : instrumented.init + instrumented.js;
file.path = file.originalPath.replace(/\.coffee$/, '.js');
done(js);
} catch (err) {
done(err);
}
}
};
createPreprocessor.$inject = ['args', 'config.icedCoffeeCoverage', 'logger', 'helper'];
// PUBLISH DI MODULE
module.exports = {
'framework:iced-coffee-coverage' : ['factory', initCoveragePlugin],
'preprocessor:iced-coffee-coverage': ['factory', createPreprocessor]
};