runner.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Runner middleware is responsible for communication with `karma run`.
  3. *
  4. * It basically triggers a test run and streams stdout back.
  5. */
  6. const _ = require('lodash')
  7. const path = require('path')
  8. const helper = require('../helper')
  9. const log = require('../logger').create()
  10. const constant = require('../constants')
  11. const json = require('body-parser').json()
  12. // TODO(vojta): disable when single-run mode
  13. function createRunnerMiddleware (emitter, fileList, capturedBrowsers, reporter, executor,
  14. /* config.protocol */ protocol, /* config.hostname */ hostname, /* config.port */
  15. port, /* config.urlRoot */ urlRoot, config) {
  16. helper.saveOriginalArgs(config)
  17. return function (request, response, next) {
  18. if (request.url !== '/__run__' && request.url !== urlRoot + 'run') {
  19. return next()
  20. }
  21. log.debug('Execution (fired by runner)')
  22. response.writeHead(200)
  23. if (!capturedBrowsers.length) {
  24. const url = `${protocol}//${hostname}:${port}${urlRoot}`
  25. return response.end(`No captured browser, open ${url}\n`)
  26. }
  27. json(request, response, function () {
  28. if (!capturedBrowsers.areAllReady([])) {
  29. response.write('Waiting for previous execution...\n')
  30. }
  31. const data = request.body
  32. emitter.once('run_start', function () {
  33. const responseWrite = response.write.bind(response)
  34. responseWrite.colors = data.colors
  35. reporter.addAdapter(responseWrite)
  36. // clean up, close runner response
  37. emitter.once('run_complete', function (browsers, results) {
  38. reporter.removeAdapter(responseWrite)
  39. const emptyTestSuite = (results.failed + results.success) === 0 ? 0 : 1
  40. response.end(constant.EXIT_CODE + emptyTestSuite + results.exitCode)
  41. })
  42. })
  43. helper.restoreOriginalArgs(config)
  44. if (_.isEmpty(data.args)) {
  45. log.debug('Ignoring empty client.args from run command')
  46. } else if ((_.isArray(data.args) && _.isArray(config.client.args)) ||
  47. (_.isPlainObject(data.args) && _.isPlainObject(config.client.args))) {
  48. log.debug('Merging client.args with ', data.args)
  49. config.client.args = _.merge(config.client.args, data.args)
  50. } else {
  51. log.warn('Replacing client.args with ', data.args, ' as their types do not match.')
  52. config.client.args = data.args
  53. }
  54. let fullRefresh = true
  55. if (helper.isArray(data.changedFiles)) {
  56. data.changedFiles.forEach(function (filepath) {
  57. fileList.changeFile(path.resolve(config.basePath, filepath))
  58. fullRefresh = false
  59. })
  60. }
  61. if (helper.isArray(data.addedFiles)) {
  62. data.addedFiles.forEach(function (filepath) {
  63. fileList.addFile(path.resolve(config.basePath, filepath))
  64. fullRefresh = false
  65. })
  66. }
  67. if (helper.isArray(data.removedFiles)) {
  68. data.removedFiles.forEach(function (filepath) {
  69. fileList.removeFile(path.resolve(config.basePath, filepath))
  70. fullRefresh = false
  71. })
  72. }
  73. if (fullRefresh && data.refresh !== false) {
  74. log.debug('Refreshing all the files / patterns')
  75. fileList.refresh().then(function () {
  76. // Wait for the file list refresh to complete before starting test run,
  77. // otherwise the context.html generation might not see new/updated files.
  78. if (!config.autoWatch) {
  79. executor.schedule()
  80. }
  81. })
  82. } else {
  83. executor.schedule()
  84. }
  85. })
  86. }
  87. }
  88. createRunnerMiddleware.$inject = ['emitter', 'fileList', 'capturedBrowsers', 'reporter', 'executor',
  89. 'config.protocol', 'config.hostname', 'config.port', 'config.urlRoot', 'config']
  90. // PUBLIC API
  91. exports.create = createRunnerMiddleware