diff --git a/lib/config.js b/lib/config.js index 591549c08..252a79b5c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -129,10 +129,40 @@ var normalizeConfig = function(config, configFilePath) { [preprocessors[pattern]] : preprocessors[pattern]; }); + + // define custom launchers/preprocessors/reporters - create an inlined plugin + var module = Object.create(null); + ['launcher', 'preprocessor', 'reporter'].forEach(function(type) { + var definitions = config['custom' + helper.ucFirst(type) + 's'] || {}; + + Object.keys(definitions).forEach(function(name) { + var definition = definitions[name]; + + if (!helper.isObject(definition)) { + return log.warn('Can not define %s %s. Definition has to be an object.', type, name); + } + + if (!helper.isString(definition.base)) { + return log.warn('Can not define %s %s. Missing base %s.', type, name, type); + } + + var token = type + ':' + definition.base; + var locals = { + args: ['value', definition] + }; + + module[type + ':' + name] = ['factory', function(injector) { + return injector.createChild([locals], [token]).get(token); + }]; + }); + }); + + config.plugins.push(module); + return config; }; -// TODO(vojta): remove +// TODO(vojta): remove in 0.11 var CONST_ERR = '%s is not supported anymore.\n\tPlease use `frameworks = ["%s"];` instead.'; ['JASMINE', 'MOCHA', 'QUNIT'].forEach(function(framework) { [framework, framework + '_ADAPTER'].forEach(function(name) { @@ -161,29 +191,34 @@ var CONST_ERR = '%s is not supported anymore.\n\tPlease use `frameworks = ["%s"] }}); }); -var KarmaDsl = function(config) { +var Config = function() { + var config = this; + this.LOG_DISABLE = constant.LOG_DISABLE; this.LOG_ERROR = constant.LOG_ERROR; this.LOG_WARN = constant.LOG_WARN; this.LOG_INFO = constant.LOG_INFO; this.LOG_DEBUG = constant.LOG_DEBUG; - this.configure = function(newConfig) { + this.set = function(newConfig) { Object.keys(newConfig).forEach(function(key) { config[key] = newConfig[key]; }); }; - // this.defineLauncher - // this.defineReporter - // this.definePreprocessor + // TODO(vojta): remove this in 0.10 + this.configure = function(newConfig) { + log.warn('config.configure() is deprecated, please use config.set() instead.'); + this.set(newConfig); + }; + + // TODO(vojta): remove this in 0.10 ['launcher', 'reporter', 'preprocessor'].forEach(function(type) { - this['define' + helper.ucFirst(type)] = function(name, base, options) { - var module = Object.create(null); - var token = type + ':' + base; - var locals = { - args: ['value', options] - }; + var methodName = 'define' + helper.ucFirst(type); + var propertyName = 'custom' + helper.ucFirst(type) + 's'; + + config[methodName] = function(name, base, options) { + log.warn('config.%s is deprecated, please use "%s" instead.', methodName, propertyName); if (!helper.isString(name)) { return log.warn('Can not define %s. Name has to be a string.', type); @@ -197,13 +232,48 @@ var KarmaDsl = function(config) { return log.warn('Can not define %s %s. Arguments has to be an object.', type, name); } - module[type + ':' + name] = ['factory', function(injector) { - return injector.createChild([locals], [token]).get(token); - }]; - - config.plugins.push(module); + config[propertyName] = config[propertyName] || {}; + config[propertyName][name] = options; + options.base = base; }; - }, this); + }); + + + // DEFAULT CONFIG + this.frameworks = []; + this.port = constant.DEFAULT_PORT; + this.runnerPort = constant.DEFAULT_RUNNER_PORT; + this.hostname = constant.DEFAULT_HOSTNAME; + this.basePath = ''; + this.files = []; + this.exclude = []; + this.logLevel = constant.LOG_INFO; + this.colors = true; + this.autoWatch = false; + this.reporters = ['progress']; + this.singleRun = false; + this.browsers = []; + this.captureTimeout = 60000; + this.proxies = {}; + this.proxyValidateSSL = true; + this.preprocessors = {'**/*.coffee': 'coffee'}; + this.urlRoot = '/'; + this.reportSlowerThan = 0; + this.loggers = [constant.CONSOLE_APPENDER]; + this.transports = ['websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling']; + this.plugins = ['karma-*']; + + // TODO(vojta): remove in 0.10 + this.junitReporter = { + outputFile: 'test-results.xml', + suite: '' + }; + + // TODO(vojta): remove in 0.10 + this.coverageReporter = { + type: 'html', + dir: 'coverage' + }; }; var parseConfig = function(configFilePath, cliOptions) { @@ -228,50 +298,17 @@ var parseConfig = function(configFilePath, cliOptions) { configModule = function() {}; } - var config = { - frameworks: [], - port: constant.DEFAULT_PORT, - runnerPort: constant.DEFAULT_RUNNER_PORT, - hostname: constant.DEFAULT_HOSTNAME, - basePath: '', - files: [], - exclude: [], - logLevel: constant.LOG_INFO, - colors: true, - autoWatch: false, - reporters: ['progress'], - singleRun: false, - browsers: [], - captureTimeout: 60000, - proxies: {}, - proxyValidateSSL: true, - preprocessors: {'**/*.coffee': 'coffee'}, - urlRoot: '/', - reportSlowerThan: 0, - // TODO(vojta): remove - junitReporter: { - outputFile: 'test-results.xml', - suite: '' - }, - // TODO(vojta): remove - coverageReporter: { - type: 'html', - dir: 'coverage/' - }, - loggers: [ constant.CONSOLE_APPENDER ], - transports: [ 'websocket', 'flashsocket', 'xhr-polling', 'jsonp-polling' ], - plugins: [ 'karma-*' ] - }; - var dsl = new KarmaDsl(config); + var config = new Config(); + try { - configModule(dsl); + configModule(config); } catch(e) { log.error('Error in config file!\n', e); return process.exit(1); } // merge the config from config file and cliOptions (precendense) - dsl.configure(cliOptions); + config.set(cliOptions); // configure the logger as soon as we can logger.setup(config.logLevel, config.colors, config.loggers); diff --git a/test/client/karma.conf.js b/test/client/karma.conf.js index a71411151..5df350c0d 100644 --- a/test/client/karma.conf.js +++ b/test/client/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ // base path, that will be used to resolve files and exclude basePath: '../..', @@ -40,7 +40,7 @@ module.exports = function(karma) { // level of logging // possible values: karma.LOG_DISABLE || karma.LOG_ERROR || karma.LOG_WARN || karma.LOG_INFO || karma.LOG_DEBUG // CLI --log-level debug - logLevel: karma.LOG_INFO, + logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes // CLI --auto-watch --no-auto-watch diff --git a/test/e2e/angular-scenario/karma.conf.js b/test/e2e/angular-scenario/karma.conf.js index 299699102..0591e1ace 100644 --- a/test/e2e/angular-scenario/karma.conf.js +++ b/test/e2e/angular-scenario/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['ng-scenario'], files: [ diff --git a/test/e2e/basic/karma.conf.js b/test/e2e/basic/karma.conf.js index d7216fbb5..823733d25 100644 --- a/test/e2e/basic/karma.conf.js +++ b/test/e2e/basic/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['jasmine'], files: [ diff --git a/test/e2e/coffee-config/karma.conf.coffee b/test/e2e/coffee-config/karma.conf.coffee index 36e616e53..be3e9835b 100644 --- a/test/e2e/coffee-config/karma.conf.coffee +++ b/test/e2e/coffee-config/karma.conf.coffee @@ -1,5 +1,5 @@ -module.exports = (karma) -> - karma.configure +module.exports = (config) -> + config.set frameworks: ['jasmine'] @@ -14,9 +14,8 @@ module.exports = (karma) -> reporters: ['dots'] - preprocessors: { + preprocessors: '**/*.coffee': 'coffee' - } plugins: [ 'karma-jasmine' diff --git a/test/e2e/coffee/karma.conf.js b/test/e2e/coffee/karma.conf.js index b1ac1fb74..229377900 100644 --- a/test/e2e/coffee/karma.conf.js +++ b/test/e2e/coffee/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['jasmine'], files: [ diff --git a/test/e2e/coverage/karma.conf.js b/test/e2e/coverage/karma.conf.js index 2df95b6b8..9f0a5aa7f 100644 --- a/test/e2e/coverage/karma.conf.js +++ b/test/e2e/coverage/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['jasmine'], files: [ diff --git a/test/e2e/coverageQunit/karma.conf.js b/test/e2e/coverageQunit/karma.conf.js index 04256752c..462d1ac93 100644 --- a/test/e2e/coverageQunit/karma.conf.js +++ b/test/e2e/coverageQunit/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['qunit'], files: [ diff --git a/test/e2e/coverageRequirejs/karma.conf.js b/test/e2e/coverageRequirejs/karma.conf.js index ed1784340..31e0d3546 100644 --- a/test/e2e/coverageRequirejs/karma.conf.js +++ b/test/e2e/coverageRequirejs/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['mocha', 'requirejs'], files: [ diff --git a/test/e2e/junit/karma.conf.js b/test/e2e/junit/karma.conf.js index 73c9e9cb8..cb2b068e0 100644 --- a/test/e2e/junit/karma.conf.js +++ b/test/e2e/junit/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['jasmine'], files: [ @@ -12,7 +12,7 @@ module.exports = function(karma) { reporters: ['dots', 'junit'], - logLevel: karma.LOG_DEBUG, + logLevel: config.LOG_DEBUG, junitReporter: { outputFile: 'test-results.xml' diff --git a/test/e2e/mocha/karma.conf.js b/test/e2e/mocha/karma.conf.js index c5963071b..89b3d6fb2 100644 --- a/test/e2e/mocha/karma.conf.js +++ b/test/e2e/mocha/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['mocha'], files: [ diff --git a/test/e2e/qunit/karma.conf.js b/test/e2e/qunit/karma.conf.js index edb7cb5b1..bfd5a1422 100644 --- a/test/e2e/qunit/karma.conf.js +++ b/test/e2e/qunit/karma.conf.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['qunit'], files: [ diff --git a/test/e2e/requirejs/karma.conf.js b/test/e2e/requirejs/karma.conf.js index e4436e4fc..2e2c4d940 100644 --- a/test/e2e/requirejs/karma.conf.js +++ b/test/e2e/requirejs/karma.conf.js @@ -1,8 +1,8 @@ // Karma configuration // Generated on Thu Jul 26 2012 14:35:23 GMT-0700 (PDT) -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ // base path, that will be used to resolve files and exclude basePath: '', @@ -36,7 +36,7 @@ module.exports = function(karma) { // level of logging // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG - logLevel: karma.LOG_INFO, + logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes diff --git a/test/e2e/syntax-error/karma.conf.ignore.js b/test/e2e/syntax-error/karma.conf.ignore.js index d20e781e3..5dc244299 100644 --- a/test/e2e/syntax-error/karma.conf.ignore.js +++ b/test/e2e/syntax-error/karma.conf.ignore.js @@ -1,5 +1,5 @@ -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ frameworks: ['jasmine'], // files to load @@ -9,7 +9,7 @@ module.exports = function(karma) { autoWatch: true, autoWatchInterval: 1, - logLevel: karma.LOG_INFO, + logLevel: config.LOG_INFO, logColors: true, browsers: ['Chrome'], diff --git a/test/e2e/timeout/karma.conf.ignore.js b/test/e2e/timeout/karma.conf.ignore.js index c3ef50e45..e8ddf3a4e 100644 --- a/test/e2e/timeout/karma.conf.ignore.js +++ b/test/e2e/timeout/karma.conf.ignore.js @@ -1,8 +1,8 @@ // Karma configuration // Generated on Sun Sep 30 2012 22:44:01 GMT-0700 (PDT) -module.exports = function(karma) { - karma.configure({ +module.exports = function(config) { + config.set({ // base path, that will be used to resolve files and exclude basePath: '', @@ -34,7 +34,7 @@ module.exports = function(karma) { // level of logging // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG - logLevel: karma.LOG_INFO, + logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes diff --git a/test/unit/config.spec.coffee b/test/unit/config.spec.coffee index 420b5f71a..67c4a6efe 100644 --- a/test/unit/config.spec.coffee +++ b/test/unit/config.spec.coffee @@ -15,6 +15,7 @@ describe 'config', -> cfg.exclude = [] if not cfg.exclude cfg.junitReporter = {} if not cfg.junitReporter cfg.coverageReporter = {} if not cfg.coverageReporter + cfg.plugins = [] if not cfg.plugins m.normalizeConfig cfg # extract only pattern properties from list of pattern objects @@ -22,8 +23,8 @@ describe 'config', -> list.map (pattern) -> pattern.pattern wrapCfg = (cfg) -> - return (karma) -> - karma.configure cfg + return (config) -> + config.set cfg beforeEach -> mocks = {} @@ -66,9 +67,7 @@ describe 'config', -> beforeEach -> logSpy = sinon.spy() - logger = require '../../lib/logger.js' - logger.create('config').on 'log', logSpy @@ -165,30 +164,6 @@ describe 'config', -> ] - it 'should return only config, no globals', -> - config = e.parseConfig '/home/config1.js', {port: 456} - - expect(config.port).to.equal 456 - expect(config.basePath).to.equal resolveWinPath('/home/base') - expect(config.reporters).to.deep.equal ['dots'] - - # defaults - expect(config.files).to.deep.equal [] - expect(config.exclude).to.deep.equal [resolveWinPath('/home/config1.js')] - expect(config.logLevel).to.exist - expect(config.autoWatch).to.equal false - expect(config.singleRun).to.equal false - expect(config.browsers).to.deep.equal [] - expect(config.reportSlowerThan).to.equal 0 - expect(config.captureTimeout).to.equal 60000 - expect(config.proxies).to.deep.equal {} - - expect(config.LOG_DISABLE).to.not.exist - expect(config.JASMINE).to.not.exist - expect(config.console).to.not.exist - expect(config.require).to.not.exist - - it 'should normalize urlRoot config', -> config = normalizeConfigWithDefaults {urlRoot: ''} expect(config.urlRoot).to.equal '/' @@ -346,9 +321,8 @@ describe 'config', -> expect(pattern.served).to.equal false - describe 'DSL', -> + describe 'custom', -> di = require 'di' - dsl = config = null forwardArgsFactory = (args) -> args @@ -358,13 +332,12 @@ describe 'config', -> 'launcher:base': ['type', forwardArgsFactory] 'reporter:base': ['type', forwardArgsFactory] - beforeEach -> - config = {plugins: []} - dsl = new m.KarmaDsl config - - it 'should define a custom launcher', -> - dsl.defineLauncher 'custom', 'base', {first: 123, whatever: 'aaa'} + config = normalizeConfigWithDefaults + customLaunchers: custom: + base: 'base' + first: 123 + whatever: 'aaa' injector = new di.Injector([baseModule].concat config.plugins) injectedArgs = injector.get 'launcher:custom' @@ -375,7 +348,11 @@ describe 'config', -> it 'should define a custom preprocessor', -> - dsl.definePreprocessor 'custom', 'base', {second: 123, whatever: 'bbb'} + config = normalizeConfigWithDefaults + customPreprocessors: custom: + base: 'base' + second: 123 + whatever: 'bbb' injector = new di.Injector([baseModule].concat config.plugins) injectedArgs = injector.get 'preprocessor:custom' @@ -386,7 +363,11 @@ describe 'config', -> it 'should define a custom reporter', -> - dsl.defineReporter 'custom', 'base', {third: 123, whatever: 'ccc'} + config = normalizeConfigWithDefaults + customReporters: custom: + base: 'base' + third: 123 + whatever: 'ccc' injector = new di.Injector([baseModule].concat config.plugins) injectedArgs = injector.get 'reporter:custom'