Skip to content

Commit

Permalink
Merge pull request #2214 from gnail/patch-1
Browse files Browse the repository at this point in the history
fix: Merge config files on Config.set()
  • Loading branch information
dignifiedquire authored Jul 11, 2016
2 parents f6cdab5 + 123a576 commit 1e526e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var log = logger.create('config')
var helper = require('./helper')
var constant = require('./constants')

var _ = require('lodash')

var COFFEE_SCRIPT_AVAILABLE = false
var LIVE_SCRIPT_AVAILABLE = false
var TYPE_SCRIPT_AVAILABLE = false
Expand Down Expand Up @@ -232,8 +234,11 @@ var Config = function () {
this.LOG_DEBUG = constant.LOG_DEBUG

this.set = function (newConfig) {
Object.keys(newConfig).forEach(function (key) {
config[key] = newConfig[key]
_.merge(config, newConfig, function (obj, src) {
// Overwrite arrays to keep consistent with #283
if (_.isArray(src)) {
return src
}
})
}

Expand Down
11 changes: 10 additions & 1 deletion test/unit/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('config', () => {
'/home/config6.js': wrapCfg({reporters: 'junit'}),
'/home/config7.js': wrapCfg({browsers: ['Chrome', 'Firefox']}),
'/home/config8.js': (config) => config.set({ files: config.suite === 'e2e' ? ['tests/e2e.spec.js'] : ['tests/unit.spec.js'] }),
'/home/config9.js': wrapCfg({client: {useIframe: false}}),
'/conf/invalid.js': () => { throw new SyntaxError('Unexpected token =') },
'/conf/exclude.js': wrapCfg({exclude: ['one.js', 'sub/two.js']}),
'/conf/absolute.js': wrapCfg({files: ['http://some.com', 'https://more.org/file.js']}),
Expand Down Expand Up @@ -138,13 +139,21 @@ describe('config', () => {
expect(config.basePath).to.equal(resolveWinPath('/abs/base'))
})

it('should override config with cli options, but not deep merge', () => {
it('should override config with cli options if the config property is an array', () => {
// regression https://github.com/karma-runner/karma/issues/283
var config = e.parseConfig('/home/config7.js', {browsers: ['Safari']})

expect(config.browsers).to.deep.equal(['Safari'])
})

it('should merge config with cli options if the config property is an object', () => {
// regression https://github.com/karma-runner/grunt-karma/issues/165
var config = e.parseConfig('/home/config9.js', {client: {captureConsole: false}})

expect(config.client.useIframe).to.equal(false)
expect(config.client.captureConsole).to.equal(false)
})

it('should have access to cli options in the config file', () => {
var config = e.parseConfig('/home/config8.js', {suite: 'e2e'})
expect(patternsFrom(config.files)).to.deep.equal([resolveWinPath('/home/tests/e2e.spec.js')])
Expand Down

0 comments on commit 1e526e3

Please sign in to comment.