Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma-coverage / lib / preprocessor.js
1 var istanbul  = require('istanbul'),
2     ibrik     = require('ibrik'),
3     minimatch = require('minimatch'),
4     globalSourceCache = require('./sourceCache');
5
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);
11
12   // if coverage reporter is not used, do not preprocess the files
13   if (reporters.indexOf('coverage') === -1) {
14     return function(content, _, done) {
15       done(content);
16     };
17   }
18
19   // check instrumenter override requests
20   function checkInstrumenters() {
21     var literal;
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);
26         return false;
27       }
28     }
29     return true;
30   }
31   if (!checkInstrumenters()) {
32     return function(content, _, done) {
33       return done(1);
34     };
35   }
36
37   return function(content, file, done) {
38     log.debug('Processing "%s".', file.originalPath);
39
40     var jsPath = file.originalPath.replace(basePath + '/', './');
41     var instrumenterLiteral = jsPath.match(/\.coffee$/) ? 'ibrik' : 'istanbul';
42
43     for (var pattern in instrumenterOverrides) {
44       if (minimatch(file.originalPath, pattern, {dot: true})) {
45         instrumenterLiteral = String(instrumenterOverrides[pattern]).toLowerCase();
46       }
47     }
48
49     var instrumenter = new instrumenters[instrumenterLiteral].Instrumenter();
50
51     instrumenter.instrument(content, jsPath, function(err, instrumentedCode) {
52       if (err) {
53         log.error('%s\n  at %s', err.message, file.originalPath);
54       }
55
56       if (instrumenterLiteral === 'ibrik') {
57         file.path = file.path.replace(/\.coffee$/, '.js');
58       }
59
60       // remember the actual immediate instrumented JS for given original path
61       sourceCache[jsPath] = content;
62
63       done(instrumentedCode);
64     });
65   };
66 };
67
68 createCoveragePreprocessor.$inject = ['logger',
69                                       'config.basePath',
70                                       'config.reporters',
71                                       'config.coverageReporter'];
72
73 module.exports = createCoveragePreprocessor;