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 | 1× 1× 1× 1× 1× 1× 1× 1× 8× 8× 2× 2× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 2× 1× 1× 6× 6× 6× 6× 1× 7× 7× 7× 7× 5× 5× 5× 5× 5× 5× 7× 1× 7× 7× 7× 7× 7× 7× 7× 1× 7× 7× 7× 7× 7× 7× 7× 7× 7× 1× | /* global require, module, console */ var _ = { // object get: require('lodash/object/get'), // string camelCase: require('lodash/string/camelCase') }; var debug = require('debug')('mocha:reporters:MultiReporters'); var fs = require('fs'); var mocha = require('mocha'); var objectAssignDeep = require('object-assign-deep'); var path = require('path'); var Q = require('q'); function MultiReporters(runner, options) { var promises = []; if (_.get(options, 'execute', true)) { options = this.getOptions(options); _.get(options, 'reporterEnabled', 'tap').split(',').map( function processReporterEnabled(name, index) { debug(name, index); name = name.trim(); var deferred = Q.defer(); var reporterOptions = this.getReporterOptions(options, name); // // Mocha Reporters // https://github.com/mochajs/mocha/blob/master/lib/reporters/index.js // var Reporter = _.get(mocha, ['reporters', name], null); // // External Reporters. // Try to load reporters from process.cwd() and node_modules. // Iif (Reporter === null) { try { Reporter = require(name); } catch (err) { err.message.indexOf('Cannot find module') !== -1 ? console.warn('"' + name + '" reporter not found') : console.warn('"' + name + '" reporter blew up with error:\n' + err.stack); Reporter = null; } } Eif (Reporter !== null) { new Reporter( runner, { reporterOptions: reporterOptions } ); runner.on('end', function () { deferred.resolve(name); }); } else { console.error('Reporter does not found!', name); } promises.push(deferred); }.bind(this) ); return Q.all(promises); } } MultiReporters.CONFIG_FILE = '../config.json'; MultiReporters.prototype.getOptions = function (options) { debug('options', options); var resultantOptions = objectAssignDeep({}, this.getDefaultOptions(), this.getCustomOptions(options)); debug('options file (resultant)', resultantOptions); return resultantOptions; }; MultiReporters.prototype.getCustomOptions = function (options) { var customOptionsFile = _.get(options, 'reporterOptions.configFile'); debug('options file (custom)', customOptionsFile); var customOptions; if (customOptionsFile) { customOptionsFile = path.resolve(customOptionsFile); debug('options file (custom)', customOptionsFile); customOptions = fs.readFileSync(customOptionsFile).toString(); debug('options (custom)', customOptions); try { customOptions = JSON.parse(customOptions); } catch (e) { console.error(e); throw e; } } return customOptions; }; MultiReporters.prototype.getDefaultOptions = function () { var defaultOptionsFile = require.resolve(MultiReporters.CONFIG_FILE); debug('options file (default)', defaultOptionsFile); var defaultOptions = fs.readFileSync(defaultOptionsFile).toString(); debug('options (default)', defaultOptions); try { defaultOptions = JSON.parse(defaultOptions); } catch (e) { console.error(e); throw e; } return defaultOptions; }; MultiReporters.prototype.getReporterOptions = function (options, name) { var _name = name; var commonReporterOptions = _.get(options, 'reporterOptions', {}); debug('reporter options (common)', _name, commonReporterOptions); name = [_.camelCase(name), 'ReporterOptions'].join(''); var customReporterOptions = _.get(options, [name], {}); debug('reporter options (custom)', _name, customReporterOptions); var resultantReporterOptions = objectAssignDeep({}, commonReporterOptions, customReporterOptions); debug('reporter options (resultant)', _name, resultantReporterOptions); return resultantReporterOptions; }; module.exports = MultiReporters; |