-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This changes the runner to communicate through HTTP. BREAKING CHANGE: `runnerPort` is merged with `port` if you are using `karma run` with custom `--runer-port`, please change that to `--port`.
- Loading branch information
Showing
7 changed files
with
175 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Runner middleware is reponsible for communication with `karma run`. | ||
* | ||
* It basically triggers a test run and streams stdout back. | ||
*/ | ||
|
||
var log = require('../logger').create(); | ||
var constant = require('../constants'); | ||
var json = require('connect').json(); | ||
|
||
var createRunnerMiddleware = function(emitter, fileList, capturedBrowsers, reporter, | ||
/* config.hostname */ hostname, /* config.port */ port, /* config.urlRoot */ urlRoot, config) { | ||
|
||
return function(request, response, next) { | ||
|
||
if (request.url !== '/__run__' && request.url !== urlRoot + 'run') { | ||
return next(); | ||
} | ||
|
||
log.debug('Execution (fired by runner)'); | ||
response.writeHead(200); | ||
|
||
if (!capturedBrowsers.length) { | ||
var url = 'http://' + hostname + ':' + port + urlRoot; | ||
|
||
return response.end('No captured browser, open ' + url + '\n'); | ||
} | ||
|
||
json(request, response, function() { | ||
if (!capturedBrowsers.areAllReady([])) { | ||
response.write('Waiting for previous execution...\n'); | ||
} | ||
|
||
emitter.once('run_start', function() { | ||
var responseWrite = response.write.bind(response); | ||
|
||
reporter.addAdapter(responseWrite); | ||
|
||
// clean up, close runner response | ||
emitter.once('run_complete', function(browsers, results) { | ||
reporter.removeAdapter(responseWrite); | ||
response.end(constant.EXIT_CODE + results.exitCode); | ||
}); | ||
}); | ||
|
||
var clientArgs = request.body.args; | ||
log.debug('Setting client.args to ', clientArgs); | ||
config.client.args = clientArgs; | ||
|
||
log.debug('Refreshing all the files / patterns'); | ||
fileList.refresh(); | ||
}); | ||
}; | ||
}; | ||
|
||
|
||
// PUBLIC API | ||
exports.create = createRunnerMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
describe 'middleware.runner', -> | ||
|
||
mocks = require 'mocks' | ||
HttpResponseMock = mocks.http.ServerResponse | ||
HttpRequestMock = mocks.http.ServerRequest | ||
|
||
EventEmitter = require('events').EventEmitter | ||
Browser = require('../../../lib/browser').Browser | ||
BrowserCollection = require('../../../lib/browser').Collection | ||
MultReporter = require('../../../lib/reporters/Multi') | ||
createRunnerMiddleware = require('../../../lib/middleware/runner').create | ||
|
||
handler = nextSpy = response = mockReporter = capturedBrowsers = emitter = config = null | ||
|
||
beforeEach -> | ||
mockReporter = | ||
adapters: [] | ||
write: (msg) -> @adapters.forEach (adapter) -> adapter msg | ||
|
||
emitter = new EventEmitter | ||
capturedBrowsers = new BrowserCollection emitter | ||
fileListMock = | ||
refresh: -> emitter.emit 'run_start' | ||
nextSpy = sinon.spy() | ||
response = new HttpResponseMock | ||
config = {client: {}} | ||
|
||
handler = createRunnerMiddleware emitter, fileListMock, capturedBrowsers, | ||
new MultReporter([mockReporter]), 'localhost', 8877, '/', config | ||
|
||
|
||
it 'should trigger test run and stream the reporter', (done) -> | ||
capturedBrowsers.add new Browser | ||
sinon.stub capturedBrowsers, 'areAllReady', -> true | ||
|
||
response.once 'end', -> | ||
expect(nextSpy).to.not.have.been.called | ||
expect(response).to.beServedAs 200, 'result\x1FEXIT0' | ||
done() | ||
|
||
handler new HttpRequestMock('/__run__'), response, nextSpy | ||
|
||
mockReporter.write 'result' | ||
emitter.emit 'run_complete', capturedBrowsers, {exitCode: 0} | ||
|
||
|
||
it 'should not run if there is no browser captured', (done) -> | ||
response.once 'end', -> | ||
expect(nextSpy).to.not.have.been.called | ||
expect(response).to.beServedAs 200, 'No captured browser, open http://localhost:8877/\n' | ||
done() | ||
|
||
handler new HttpRequestMock('/__run__'), response, nextSpy | ||
|
||
|
||
it 'should parse body and set client.args', (done) -> | ||
capturedBrowsers.add new Browser | ||
sinon.stub capturedBrowsers, 'areAllReady', -> true | ||
|
||
emitter.once 'run_start', -> | ||
expect(config.client.args).to.deep.equal ['arg1', 'arg2'] | ||
done() | ||
|
||
request = new HttpRequestMock '/__run__', { | ||
'content-type': 'application/json' | ||
'content-length': 1 | ||
} | ||
request.setEncoding = -> null | ||
|
||
handler request, response, nextSpy | ||
|
||
request.emit 'data', '{"args": ["arg1", "arg2"]}' | ||
request.emit 'end' | ||
|
||
|
||
it 'should ignore other urls', (done) -> | ||
handler new HttpRequestMock('/something'), response, -> | ||
expect(response).to.beNotServed() | ||
done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters