Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Merge config files on Config.set() #2214

Merged
merged 2 commits into from
Jul 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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