source: trip-planner-front/node_modules/karma/lib/runner.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict'
2
3const http = require('http')
4
5const constant = require('./constants')
6const EventEmitter = require('events').EventEmitter
7const helper = require('./helper')
8const cfg = require('./config')
9const logger = require('./logger')
10const log = logger.create('runner')
11
12function 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)
35function 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
111exports.run = run
Note: See TracBrowser for help on using the repository browser.