Skip to content

Commit 91de383

Browse files
committed
fix(runner): Merge config.client.args with client.args provided by run
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 #1746
1 parent ca95553 commit 91de383

2 files changed

Lines changed: 104 additions & 21 deletions

File tree

lib/middleware/runner.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* It basically triggers a test run and streams stdout back.
55
*/
66

7+
var _ = require('lodash')
78
var path = require('path')
89
var helper = require('../helper')
910
var log = require('../logger').create()
@@ -47,8 +48,16 @@ var createRunnerMiddleware = function (emitter, fileList, capturedBrowsers, repo
4748
})
4849
})
4950

50-
log.debug('Setting client.args to ', data.args)
51-
config.client.args = data.args
51+
if (_.isEmpty(data.args)) {
52+
log.debug('Ignoring empty client.args from run command')
53+
} else if ((_.isArray(data.args) && _.isArray(config.client.args)) ||
54+
(_.isPlainObject(data.args) && _.isPlainObject(config.client.args))) {
55+
log.debug('Merging client.args with ', data.args)
56+
config.client.args = _.merge(config.client.args, data.args)
57+
} else {
58+
log.warn('Replacing client.args with ', data.args, ' as their types do not match.')
59+
config.client.args = data.args
60+
}
5261

5362
var fullRefresh = true
5463

test/unit/middleware/runner.spec.js

Lines changed: 93 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Promise} from 'bluebird'
55
import Browser from '../../../lib/browser'
66
import BrowserCollection from '../../../lib/browser_collection'
77
import MultReporter from '../../../lib/reporters/multi'
8+
var _ = require('lodash')
89
var createRunnerMiddleware = require('../../../lib/middleware/runner').create
910
var HttpResponseMock = mocks.http.ServerResponse
1011
var HttpRequestMock = mocks.http.ServerRequest
@@ -144,26 +145,99 @@ describe('middleware.runner', () => {
144145
handler(new HttpRequestMock('/__run__'), response, nextSpy)
145146
})
146147

147-
it('should parse body and set client.args', (done) => {
148-
capturedBrowsers.add(new Browser())
149-
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
150-
151-
emitter.once('run_start', () => {
152-
expect(config.client.args).to.deep.equal(['arg1', 'arg2'])
153-
done()
154-
})
155-
156-
var RAW_MESSAGE = '{"args": ["arg1", "arg2"]}'
157-
158-
var request = new HttpRequestMock('/__run__', {
159-
'content-type': 'application/json',
160-
'content-length': RAW_MESSAGE.length
148+
var clientArgsRuns = [
149+
{
150+
desc: 'should parse body and set client.args',
151+
expected: ['arg1', 'arg2'],
152+
rawMessage: '{"args": ["arg1", "arg2"]}'
153+
},
154+
{
155+
desc: 'should set array client args passed by run when there are no existing client.args',
156+
expected: ['my_args'],
157+
rawMessage: '{"args": ["my_args"]}'
158+
},
159+
{
160+
desc: 'should set object client args passed by run when there are no existing client.args',
161+
expected: {arg2: 'fig', arg3: 'chocolate'},
162+
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}'
163+
},
164+
{
165+
desc: 'should overwrite empty array client.args when run passes an array for client.args',
166+
expected: ['user_arg1'],
167+
rawMessage: '{"args": ["user_arg1"]}',
168+
existingConfig: []
169+
},
170+
{
171+
desc: 'should overwrite empty array client.args when run passes an object for client.args',
172+
expected: {arg2: 'figs', arg3: 'chocolates'},
173+
rawMessage: '{"args": {"arg2": "figs", "arg3": "chocolates"}}',
174+
existingConfig: []
175+
},
176+
{
177+
desc: 'should overwrite empty object client.args when run passes an array for client.args',
178+
expected: ['user_arg'],
179+
rawMessage: '{"args": ["user_arg"]}',
180+
existingConfig: {}
181+
},
182+
{
183+
desc: 'should not overwrite existing array client.args when run passes an empty array for client.args',
184+
expected: ['user_arg'],
185+
rawMessage: '{"args": []}',
186+
existingConfig: ['user_arg']
187+
},
188+
{
189+
desc: 'should not overwrite existing array client.args when run passes an empty object for client.args',
190+
expected: ['user_arg'],
191+
rawMessage: '{"args": {}}',
192+
existingConfig: ['user_arg']
193+
},
194+
{
195+
desc: 'should not overwrite existing array client.args when run passes no client.args',
196+
expected: ['user_arg'],
197+
rawMessage: '{}',
198+
existingConfig: ['user_arg']
199+
},
200+
{
201+
desc: 'should merge existing client.args with client.args passed by run',
202+
expected: {arg1: 'cherry', arg2: 'fig', arg3: 'chocolate'},
203+
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}',
204+
existingConfig: {arg1: 'cherry', arg2: 'mango'}
205+
},
206+
{
207+
desc: 'should merge empty client.args with client.args passed by run',
208+
expected: {arg2: 'fig', arg3: 'chocolate'},
209+
rawMessage: '{"args": {"arg2": "fig", "arg3": "chocolate"}}',
210+
existingConfig: {}
211+
}
212+
]
213+
214+
describe('', function () {
215+
clientArgsRuns.forEach(function (run) {
216+
it(run.desc, (done) => {
217+
capturedBrowsers.add(new Browser())
218+
sinon.stub(capturedBrowsers, 'areAllReady', () => true)
219+
if (run.existingConfig) {
220+
config = _.merge(config, {client: {args: run.existingConfig}})
221+
}
222+
223+
emitter.once('run_start', () => {
224+
expect(config.client.args).to.deep.equal(run.expected)
225+
done()
226+
})
227+
228+
var RAW_MESSAGE = run.rawMessage
229+
230+
var request = new HttpRequestMock('/__run__', {
231+
'content-type': 'application/json',
232+
'content-length': RAW_MESSAGE.length
233+
})
234+
235+
handler(request, response, nextSpy)
236+
237+
request.emit('data', RAW_MESSAGE)
238+
request.emit('end')
239+
})
161240
})
162-
163-
handler(request, response, nextSpy)
164-
165-
request.emit('data', RAW_MESSAGE)
166-
request.emit('end')
167241
})
168242

169243
it('should refresh explicit files if specified', (done) => {

0 commit comments

Comments
 (0)