/
index.babel.js
152 lines (143 loc) · 3.71 KB
/
index.babel.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
const childProcess = require('child_process');
const defaults = {
args: [],
fork: true,
encoding: 'utf8',
debug: false,
stderrThrow: true
};
/**
* TTY test helper. Forks or spawns a process and keeps stdout history in an array. Throws by default if it receives stderr from the child.
*
* @param cmd {string} Which command to fork / spawn.
* @param [opts] {object} Default options:
* args: [] - Arguments for child process.
* fork: true - If child process should fork or spawn.
* encoding: 'utf8' - Encoding of stdout / stderr.
* debug: false - Print debug messages.
* stderrThrow: true - Throws on stderr.
*
* @returns {object}
*/
module.exports = function ttyTestHelper (cmd, opts) {
let child;
opts = Object.assign({}, defaults, opts);
if (opts.fork) {
// silent: true -> do not pipe child.stdout to process.stdout
child = childProcess.fork(cmd, opts.args, { silent: true });
} else {
child = childProcess.spawn(cmd, opts.args, { silent: true });
}
// keep output history in an array
const stdout = [];
child.stdout.setEncoding(opts.encoding);
child.stdout.on('data', (d) => {
stdout.push(d);
if (opts.debug) {
console.log(`${cmd} stdout: ${d}`);
}
});
const stderr = [];
child.stderr.setEncoding(opts.encoding);
child.stderr.on('data', (d) => {
if (opts.debug) {
console.log(`${cmd} stderr: ${d}`);
}
if (opts.stderrThrow) {
// fail hard on stderr
throw new Error(d);
} else {
stderr.push(d);
}
});
/**
* Wait until what is found in array.
*
* @param what {string|regex} What to look for.
* @param [arr] {array} Defaults to stdout history of child.
* @param [onlyNew] {bool} Search only in newly added items in array.
* @param [timeout] {int} How long to wait before rejecting.
* @returns {Promise}
*/
function waitFor (what = '', arr = stdout, onlyNew = false, timeout = 1000) {
return new Promise((resolve, reject) => {
let _timeout, interval, l;
// check every 10 ms
if (onlyNew) {
l = what.length;
}
interval = setInterval(() => {
if (onlyNew && what.length === l) {
return;
}
if (last(arr) && last(arr).indexOf(what) !== -1) {
clearTimeout(_timeout);
clearInterval(interval);
resolve(last(arr));
}
}, 10);
// or time out
_timeout = setTimeout(() => {
clearInterval(interval);
reject(`timed out after ${timeout} milliseconds, did not find "${what}" in "${last(arr)}"`);
}, timeout);
})
}
/**
* Get next item added to array.
*
* @param [arr] {array} Defaults to stdout history of child.
* @param [timeout] {int} How long to wait before rejecting.
* @returns {Promise}
*/
function next (arr = stdout, timeout = 1000) {
// TODO: EventEmitter?
return new Promise((resolve, reject) => {
let _timeout, interval;
const l = arr.length;
// check every 10 ms
interval = setInterval(() => {
if (arr.length === l) {
return;
}
clearTimeout(_timeout);
clearInterval(interval);
resolve(last(arr));
}, 10);
// or time out
_timeout = setTimeout(() => {
clearInterval(interval);
reject(`next timed out after ${timeout} milliseconds`);
}, timeout);
})
}
return {
child,
stdout,
stderr,
stdin: child.stdin,
waitFor,
next,
last,
wait
}
}
/**
* Get last item of array.
*
* @returns `undefined` if array is empty.
*/
function last (arr) {
return arr.slice(arr.length - 1)[0]
}
/**
* Resolve after `time`.
*
* @param time {int} Milliseconds to wait before resolving.
* @returns {Promise}
*/
function wait (time = 1) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}