Skip to content

Commit

Permalink
fix(runner): Merge config.client.args with client.args provided by run
Browse files Browse the repository at this point in the history
Before this change, calling `karma run` would overwrite any value in
config.client.args with the value provided in the `karma run` request,
even if that value was an empty array. This commit does a _.merge to
merge the two values together.

Fixes karma-runner#1746
  • Loading branch information
danielcompton committed Feb 29, 2016
1 parent a751293 commit e715839
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/middleware/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* It basically triggers a test run and streams stdout back.
*/

var _ = require('lodash')
var path = require('path')
var helper = require('../helper')
var log = require('../logger').create()
Expand Down Expand Up @@ -48,7 +49,7 @@ var createRunnerMiddleware = function (emitter, fileList, capturedBrowsers, repo
})

log.debug('Setting client.args to ', data.args)
config.client.args = data.args
config = _.merge(config, {client: {args: data.args}})

var fullRefresh = true

Expand Down
115 changes: 115 additions & 0 deletions test/unit/middleware/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Promise} from 'bluebird'
import Browser from '../../../lib/browser'
import BrowserCollection from '../../../lib/browser_collection'
import MultReporter from '../../../lib/reporters/multi'
var _ = require('lodash')
var createRunnerMiddleware = require('../../../lib/middleware/runner').create
var HttpResponseMock = mocks.http.ServerResponse
var HttpRequestMock = mocks.http.ServerRequest
Expand Down Expand Up @@ -166,6 +167,120 @@ describe('middleware.runner', () => {
request.emit('end')
})

it('should not overwrite existing client.args when run passes an empty array for client.args', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
config = _.merge(config, {client: {args: ['clientArg']}})

emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal(['clientArg'])
done()
})

var RAW_MESSAGE = '{"args": []}'

var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})

handler(request, response, nextSpy)

request.emit('data', RAW_MESSAGE)
request.emit('end')
})

it('should not overwrite existing client.args when run passes no client.args', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
config = _.merge(config, {client: {args: ['clientArg']}})

emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal(['clientArg'])
done()
})

var RAW_MESSAGE = '{}'

var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})

handler(request, response, nextSpy)

request.emit('data', RAW_MESSAGE)
request.emit('end')
})

it('should merge existing client.args with client.args passed by run', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
config = _.merge(config, {client: {args: {arg1: 'cherry', arg2: 'mango'}}})

emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal({arg1: 'cherry', arg2: 'fig', arg3: 'chocolate'})
done()
})

var RAW_MESSAGE = '{"args": {"arg2": "fig", "arg3": "chocolate"}}'

var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})

handler(request, response, nextSpy)

request.emit('data', RAW_MESSAGE)
request.emit('end')
})

it('should set client args passed by run when there are no existing client.args', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)

emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal({arg2: 'fig', arg3: 'chocolate'})
done()
})

var RAW_MESSAGE = '{"args": {"arg2": "fig", "arg3": "chocolate"}}'

var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})

handler(request, response, nextSpy)

request.emit('data', RAW_MESSAGE)
request.emit('end')
})

it('should merge empty client.args with client.args passed by run', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
config = _.merge(config, {})

emitter.once('run_start', () => {
expect(config.client.args).to.deep.equal({arg2: 'fig', arg3: 'chocolate'})
done()
})

var RAW_MESSAGE = '{"args": {"arg2": "fig", "arg3": "chocolate"}}'

var request = new HttpRequestMock('/__run__', {
'content-type': 'application/json',
'content-length': RAW_MESSAGE.length
})

handler(request, response, nextSpy)

request.emit('data', RAW_MESSAGE)
request.emit('end')
})

it('should refresh explicit files if specified', (done) => {
capturedBrowsers.add(new Browser())
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
Expand Down

0 comments on commit e715839

Please sign in to comment.