[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const http = require('http')
|
---|
| 4 |
|
---|
| 5 | const constant = require('./constants')
|
---|
| 6 | const EventEmitter = require('events').EventEmitter
|
---|
| 7 | const helper = require('./helper')
|
---|
| 8 | const cfg = require('./config')
|
---|
| 9 | const logger = require('./logger')
|
---|
| 10 | const log = logger.create('runner')
|
---|
| 11 |
|
---|
| 12 | function parseExitCode (buffer, defaultExitCode, failOnEmptyTestSuite) {
|
---|
| 13 | const tailPos = buffer.length - Buffer.byteLength(constant.EXIT_CODE) - 2
|
---|
| 14 |
|
---|
| 15 | if (tailPos < 0) {
|
---|
| 16 | return { exitCode: defaultExitCode, buffer }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | const tail = buffer.slice(tailPos)
|
---|
| 20 | const tailStr = tail.toString()
|
---|
| 21 | if (tailStr.substr(0, tailStr.length - 2) === constant.EXIT_CODE) {
|
---|
| 22 | const emptyInt = parseInt(tailStr.substr(-2, 1), 10)
|
---|
| 23 | let exitCode = parseInt(tailStr.substr(-1), 10)
|
---|
| 24 | if (failOnEmptyTestSuite === false && emptyInt === 0) {
|
---|
| 25 | log.warn('Test suite was empty.')
|
---|
| 26 | exitCode = 0
|
---|
| 27 | }
|
---|
| 28 | return { exitCode, buffer: buffer.slice(0, tailPos) }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | return { exitCode: defaultExitCode, buffer }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | // TODO(vojta): read config file (port, host, urlRoot)
|
---|
| 35 | function run (cliOptionsOrConfig, done) {
|
---|
| 36 | cliOptionsOrConfig = cliOptionsOrConfig || {}
|
---|
| 37 | done = helper.isFunction(done) ? done : process.exit
|
---|
| 38 |
|
---|
| 39 | let config
|
---|
| 40 | if (cliOptionsOrConfig instanceof cfg.Config) {
|
---|
| 41 | config = cliOptionsOrConfig
|
---|
| 42 | } else {
|
---|
| 43 | logger.setupFromConfig({
|
---|
| 44 | colors: cliOptionsOrConfig.colors,
|
---|
| 45 | logLevel: cliOptionsOrConfig.logLevel
|
---|
| 46 | })
|
---|
| 47 | const deprecatedCliOptionsMessage =
|
---|
| 48 | 'Passing raw CLI options to `runner(config, done)` is deprecated. Use ' +
|
---|
| 49 | '`parseConfig(configFilePath, cliOptions, {promiseConfig: true, throwErrors: true})` ' +
|
---|
| 50 | 'to prepare a processed `Config` instance and pass that as the ' +
|
---|
| 51 | '`config` argument instead.'
|
---|
| 52 | log.warn(deprecatedCliOptionsMessage)
|
---|
| 53 | try {
|
---|
| 54 | config = cfg.parseConfig(
|
---|
| 55 | cliOptionsOrConfig.configFile,
|
---|
| 56 | cliOptionsOrConfig,
|
---|
| 57 | {
|
---|
| 58 | promiseConfig: false,
|
---|
| 59 | throwErrors: true
|
---|
| 60 | }
|
---|
| 61 | )
|
---|
| 62 | } catch (parseConfigError) {
|
---|
| 63 | // TODO: change how `done` falls back to exit in next major version
|
---|
| 64 | // SEE: https://github.com/karma-runner/karma/pull/3635#discussion_r565399378
|
---|
| 65 | done(1)
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | let exitCode = 1
|
---|
| 69 | const emitter = new EventEmitter()
|
---|
| 70 | const options = {
|
---|
| 71 | hostname: config.hostname,
|
---|
| 72 | path: config.urlRoot + 'run',
|
---|
| 73 | port: config.port,
|
---|
| 74 | method: 'POST',
|
---|
| 75 | headers: {
|
---|
| 76 | 'Content-Type': 'application/json'
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | const request = http.request(options, function (response) {
|
---|
| 81 | response.on('data', function (buffer) {
|
---|
| 82 | const parsedResult = parseExitCode(buffer, exitCode, config.failOnEmptyTestSuite)
|
---|
| 83 | exitCode = parsedResult.exitCode
|
---|
| 84 | emitter.emit('progress', parsedResult.buffer)
|
---|
| 85 | })
|
---|
| 86 |
|
---|
| 87 | response.on('end', () => done(exitCode))
|
---|
| 88 | })
|
---|
| 89 |
|
---|
| 90 | request.on('error', function (e) {
|
---|
| 91 | if (e.code === 'ECONNREFUSED') {
|
---|
| 92 | log.error('There is no server listening on port %d', options.port)
|
---|
| 93 | done(1, e.code)
|
---|
| 94 | } else {
|
---|
| 95 | throw e
|
---|
| 96 | }
|
---|
| 97 | })
|
---|
| 98 |
|
---|
| 99 | request.end(JSON.stringify({
|
---|
| 100 | args: config.clientArgs,
|
---|
| 101 | removedFiles: config.removedFiles,
|
---|
| 102 | changedFiles: config.changedFiles,
|
---|
| 103 | addedFiles: config.addedFiles,
|
---|
| 104 | refresh: config.refresh,
|
---|
| 105 | colors: config.colors
|
---|
| 106 | }))
|
---|
| 107 |
|
---|
| 108 | return emitter
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | exports.run = run
|
---|