Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / karma / lib / reporter.js
1 var util = require('util')
2 var log = require('./logger').create('reporter')
3 var MultiReporter = require('./reporters/multi')
4 var baseReporterDecoratorFactory = require('./reporters/base').decoratorFactory
5 var SourceMapConsumer = require('source-map').SourceMapConsumer
6
7 var createErrorFormatter = function (basePath, emitter, SourceMapConsumer) {
8   var lastServedFiles = []
9
10   emitter.on('file_list_modified', function (filesPromise) {
11     filesPromise.then(function (files) {
12       lastServedFiles = files.served
13     })
14   })
15
16   var findFile = function (path) {
17     for (var i = 0; i < lastServedFiles.length; i++) {
18       if (lastServedFiles[i].path === path) {
19         return lastServedFiles[i]
20       }
21     }
22     return null
23   }
24
25   var URL_REGEXP = new RegExp('http:\\/\\/[^\\/]*\\/' +
26     '(base|absolute)' + // prefix
27     '((?:[A-z]\\:)?[^\\?\\s\\:]*)' + // path
28     '(\\?\\w*)?' + // sha
29     '(\\:(\\d+))?' + // line
30     '(\\:(\\d+))?' + // column
31     '', 'g')
32
33   return function (msg, indentation) {
34     // remove domain and timestamp from source files
35     // and resolve base path / absolute path urls into absolute path
36     msg = (msg || '').replace(URL_REGEXP, function (_, prefix, path, __, ___, line, ____, column) {
37       if (prefix === 'base') {
38         path = basePath + path
39       }
40
41       var file = findFile(path)
42
43       if (file && file.sourceMap) {
44         line = parseInt(line || '0', 10)
45         column = parseInt(column || '0', 10)
46
47         var smc = new SourceMapConsumer(file.sourceMap)
48         try {
49           var original = smc.originalPositionFor({line: line, column: column})
50
51           return util.format('%s:%d:%d <- %s:%d:%d', path, line, column, original.source,
52             original.line, original.column)
53         } catch (e) {
54           log.warn('SourceMap position not found for trace: %s', msg)
55         // Fall back to non-source-mapped formatting.
56         }
57       }
58
59       return path + (line ? ':' + line : '') + (column ? ':' + column : '')
60     })
61
62     // indent every line
63     if (indentation) {
64       msg = indentation + msg.replace(/\n/g, '\n' + indentation)
65     }
66
67     return msg + '\n'
68   }
69 }
70
71 var createReporters = function (names, config, emitter, injector) {
72   var errorFormatter = createErrorFormatter(config.basePath, emitter, SourceMapConsumer)
73   var reporters = []
74
75   // TODO(vojta): instantiate all reporters through DI
76   names.forEach(function (name) {
77     if (['dots', 'progress'].indexOf(name) !== -1) {
78       var Cls = require('./reporters/' + name + (config.colors ? '_color' : ''))
79       return reporters.push(new Cls(errorFormatter, config.reportSlowerThan))
80     }
81
82     var locals = {
83       baseReporterDecorator: ['factory', baseReporterDecoratorFactory],
84       formatError: ['value', errorFormatter]
85     }
86
87     try {
88       reporters.push(injector.createChild([locals], ['reporter:' + name]).get('reporter:' + name))
89     } catch (e) {
90       if (e.message.indexOf('No provider for "reporter:' + name + '"') !== -1) {
91         log.warn('Can not load "%s", it is not registered!\n  ' +
92           'Perhaps you are missing some plugin?', name)
93       } else {
94         log.warn('Can not load "%s"!\n  ' + e.stack, name)
95       }
96     }
97   })
98
99   // bind all reporters
100   reporters.forEach(function (reporter) {
101     emitter.bind(reporter)
102   })
103
104   return new MultiReporter(reporters)
105 }
106
107 createReporters.$inject = ['config.reporters', 'config', 'emitter', 'injector']
108
109 // PUBLISH
110 exports.createReporters = createReporters