1 var istanbul = require('istanbul'),
2 ibrik = require('ibrik'),
3 minimatch = require('minimatch'),
4 globalSourceCache = require('./sourceCache');
6 var createCoveragePreprocessor = function(logger, basePath, reporters, coverageReporter) {
7 var log = logger.create('preprocessor.coverage');
8 var instrumenterOverrides = (coverageReporter && coverageReporter.instrumenter) || {};
9 var instrumenters = {istanbul: istanbul, ibrik: ibrik};
10 var sourceCache = globalSourceCache.getByBasePath(basePath);
12 // if coverage reporter is not used, do not preprocess the files
13 if (reporters.indexOf('coverage') === -1) {
14 return function(content, _, done) {
19 // check instrumenter override requests
20 function checkInstrumenters() {
22 for (var pattern in instrumenterOverrides) {
23 literal = String(instrumenterOverrides[pattern]).toLowerCase();
24 if (literal !== 'istanbul' && literal !== 'ibrik') {
25 log.error('Unknown instrumenter: %s', literal);
31 if (!checkInstrumenters()) {
32 return function(content, _, done) {
37 return function(content, file, done) {
38 log.debug('Processing "%s".', file.originalPath);
40 var jsPath = file.originalPath.replace(basePath + '/', './');
41 var instrumenterLiteral = jsPath.match(/\.coffee$/) ? 'ibrik' : 'istanbul';
43 for (var pattern in instrumenterOverrides) {
44 if (minimatch(file.originalPath, pattern, {dot: true})) {
45 instrumenterLiteral = String(instrumenterOverrides[pattern]).toLowerCase();
49 var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter();
51 instrumenter.instrument(content, jsPath, function(err, instrumentedCode) {
53 log.error('%s\n at %s', err.message, file.originalPath);
56 if (instrumenterLiteral === 'ibrik') {
57 file.path = file.path.replace(/\.coffee$/, '.js');
60 // remember the actual immediate instrumented JS for given original path
61 sourceCache[jsPath] = content;
63 done(instrumentedCode);
68 createCoveragePreprocessor.$inject = ['logger',
71 'config.coverageReporter'];
73 module.exports = createCoveragePreprocessor;