/
index.js
157 lines (137 loc) · 4.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// @ts-check
'use strict'
const la = require('lazy-ass')
const is = require('check-more-types')
const execa = require('execa')
const waitOn = require('wait-on')
const Promise = require('bluebird')
const psTree = require('ps-tree')
const debug = require('debug')('start-server-and-test')
/**
* Used for timeout (ms)
*/
const fiveMinutes = 5 * 60 * 1000
const twoSeconds = 2000
const waitOnTimeout = process.env.WAIT_ON_TIMEOUT
? Number(process.env.WAIT_ON_TIMEOUT)
: fiveMinutes
const waitOnInterval = process.env.WAIT_ON_INTERVAL
? Number(process.env.WAIT_ON_INTERVAL)
: twoSeconds
const isDebug = () =>
process.env.DEBUG && process.env.DEBUG.indexOf('start-server-and-test') !== -1
const isInsecure = () => process.env.START_SERVER_AND_TEST_INSECURE
function waitAndRun ({ start, url, runFn, namedArguments }) {
la(is.unemptyString(start), 'missing start script name', start)
la(is.fn(runFn), 'missing test script name', runFn)
la(
is.unemptyString(url) || is.unemptyArray(url),
'missing url to wait on',
url
)
const isSuccessfulHttpCode = status =>
(status >= 200 && status < 300) || status === 304
const validateStatus = namedArguments.expect
? status => status === namedArguments.expect
: isSuccessfulHttpCode
debug('starting server with command "%s", verbose mode?', start, isDebug())
const server = execa(start, { shell: true, stdio: 'inherit' })
let serverStopped
function stopServer () {
debug('getting child processes')
if (!serverStopped) {
serverStopped = true
return Promise.fromNode(cb => psTree(server.pid, cb))
.then(children => {
debug('stopping child processes')
children.forEach(child => {
try {
process.kill(child.PID, 'SIGINT')
} catch (e) {
if (e.code === 'ESRCH') {
console.log(
`Child process ${child.PID} exited before trying to stop it`
)
} else {
throw e
}
}
})
})
.then(() => {
debug('stopping server')
server.kill()
})
}
}
const waited = new Promise((resolve, reject) => {
const onClose = () => {
reject(new Error('server closed unexpectedly'))
}
server.on('close', onClose)
debug('starting waitOn %s', url)
const options = {
resources: Array.isArray(url) ? url : [url],
interval: waitOnInterval,
window: 1000,
timeout: waitOnTimeout,
verbose: isDebug(),
strictSSL: !isInsecure(),
log: isDebug(),
headers: {
Accept: 'text/html, application/json, text/plain, */*'
},
validateStatus
}
debug('wait-on options %o', options)
waitOn(options, err => {
if (err) {
debug('error waiting for url', url)
debug(err.message)
return reject(err)
}
debug('waitOn finished successfully')
server.removeListener('close', onClose)
resolve()
})
})
return waited
.tapCatch(stopServer)
.then(runFn)
.finally(stopServer)
}
const runTheTests = testCommand => () => {
debug('running test script command: %s', testCommand)
return execa(testCommand, { shell: true, stdio: 'inherit' })
}
/**
* Starts a single service and runs tests or recursively
* runs a service, then goes to the next list, until it reaches 1 service and runs test.
*/
function startAndTest ({ services, test, namedArguments }) {
if (services.length === 0) {
throw new Error('Got zero services to start ...')
}
if (services.length === 1) {
const runTests = runTheTests(test)
debug('single service "%s" to run and test', services[0].start)
return waitAndRun({
start: services[0].start,
url: services[0].url,
namedArguments,
runFn: runTests
})
}
return waitAndRun({
start: services[0].start,
url: services[0].url,
namedArguments,
runFn: () => {
debug('previous service started, now going to the next one')
return startAndTest({ services: services.slice(1), test, namedArguments })
}
})
}
module.exports = {
startAndTest
}