-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtest-shell.ts
363 lines (319 loc) · 10.2 KB
/
test-shell.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import assert from 'assert';
import type {
ChildProcess,
ChildProcessWithoutNullStreams,
} from 'child_process';
import { spawn } from 'child_process';
import { once } from 'events';
import { inspect } from 'util';
import path from 'path';
import stripAnsi from 'strip-ansi';
import { EJSON } from 'bson';
import { eventually } from '../../../testing/eventually';
/* eslint-disable mocha/no-exports -- This file export hooks wrapping Mocha's Hooks APIs */
export type TestShellStartupResult =
| { state: 'prompt' }
| { state: 'exit'; exitCode: number };
type SignalType = ChildProcess extends { kill: (signal: infer T) => any }
? T
: never;
// Assume that prompt strings are those that end in '> ' but do not contain
// < or > (so that e.g. '- <repl>' in a stack trace is not considered a prompt).
const PROMPT_PATTERN = /^([^<>]*> ?)+$/m;
const ERROR_PATTERN_1 = /Thrown:\n([^>]*)/gm; // node <= 12.14
const ERROR_PATTERN_2 = /Uncaught[:\n ]+([^>]*)/gm;
function matches(str: string, pattern: string | RegExp): boolean {
return typeof pattern === 'string'
? str.includes(pattern)
: pattern.test(str);
}
export interface TestShellOptions {
args: string[];
env?: Record<string, string>;
removeSigintListeners?: boolean;
cwd?: string;
forceTerminal?: boolean;
consumeStdio?: boolean;
}
/**
* Test shell helper class.
*/
export class TestShell {
private static _openShells: Set<TestShell> = new Set();
/**
* Starts a test shell.
*
* Beware that the caller is responsible for calling {@link kill} (and potentially {@link waitForExit}).
*
* Consider calling the `startTestShell` function on a {@link Mocha.Context} instead, as that manages the lifetime the shell
* and ensures it gets killed eventually.
*/
static start(options: TestShellOptions = { args: [] }): TestShell {
let shellProcess: ChildProcessWithoutNullStreams;
let env = options.env || process.env;
if (options.forceTerminal) {
env = { ...env, MONGOSH_FORCE_TERMINAL: '1' };
}
const args = [...options.args];
if (process.env.MONGOSH_TEST_E2E_FORCE_FIPS) {
args.push('--tlsFIPSMode');
}
if (process.env.MONGOSH_TEST_EXECUTABLE_PATH) {
shellProcess = spawn(process.env.MONGOSH_TEST_EXECUTABLE_PATH, args, {
stdio: ['pipe', 'pipe', 'pipe'],
env: env,
cwd: options.cwd,
});
} else {
if (options.removeSigintListeners) {
// We set CLEAR_SIGINT_LISTENERS to remove all `process.on('SIGINT')`
// listeners during Shell startup. This is unfortunately necessary,
// because nyc registers a listener that is used to gather coverage
// in case of an unclean exit for several signals, but this particular
// one interferes with testing the actual process.on('SIGINT')
// functionality here.
env = { ...env, CLEAR_SIGINT_LISTENERS: '1' };
}
shellProcess = spawn(
'node',
[
path.resolve(__dirname, '..', '..', 'cli-repl', 'bin', 'mongosh.js'),
...args,
],
{
stdio: ['pipe', 'pipe', 'pipe'],
env: env,
cwd: options.cwd,
}
);
}
const shell = new TestShell(shellProcess, options.consumeStdio);
TestShell._openShells.add(shell);
void shell.waitForExit().then(() => {
TestShell._openShells.delete(shell);
});
return shell;
}
debugInformation() {
return {
pid: this.process.pid,
output: this.output,
rawOutput: this.rawOutput,
exitCode: this.process.exitCode,
signal: this.process.signalCode,
};
}
static printShells() {
for (const shell of TestShell._openShells) {
console.error(shell.debugInformation());
}
}
static assertNoOpenShells() {
const debugInformation = [...TestShell._openShells].map((shell) =>
shell.debugInformation()
);
assert.strictEqual(
TestShell._openShells.size,
0,
`Expected no open shells, found: ${JSON.stringify(
debugInformation,
null,
2
)}`
);
}
private _process: ChildProcessWithoutNullStreams;
private _output: string;
private _rawOutput: string;
private _onClose: Promise<number>;
constructor(
shellProcess: ChildProcessWithoutNullStreams,
consumeStdio = true
) {
this._process = shellProcess;
this._output = '';
this._rawOutput = '';
if (consumeStdio) {
shellProcess.stdout.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});
shellProcess.stderr.setEncoding('utf8').on('data', (chunk) => {
this._output += stripAnsi(chunk);
this._rawOutput += chunk;
});
}
this._onClose = (async () => {
const [code] = await once(shellProcess, 'close');
return code;
})();
}
get output(): string {
return this._output;
}
get rawOutput(): string {
return this._rawOutput;
}
get process(): ChildProcessWithoutNullStreams {
return this._process;
}
async waitForLine(pattern: RegExp, start = 0): Promise<void> {
await eventually(() => {
const output = this._output.slice(start);
const lines = output.split('\n');
const found = !!lines.filter((l) => pattern.exec(l));
if (!found) {
throw new assert.AssertionError({
message: 'expected line',
expected: pattern.toString(),
actual:
this._output.slice(0, start) + '[line search starts here]' + output,
});
}
});
}
async waitForPrompt(start = 0): Promise<void> {
await eventually(
() => {
const output = this._output.slice(start);
const lines = output.split('\n');
const found = !!lines
.filter((l) => PROMPT_PATTERN.exec(l)) // a line that is the prompt must at least match the pattern
.find((l) => {
// in some situations the prompt occurs multiple times in the line (but only in tests!)
const prompts = l
.trim()
.replace(/>$/g, '')
.split('>')
.map((m) => m.trim());
// if there are multiple prompt parts they must all equal
if (prompts.length > 1) {
for (const p of prompts) {
if (p !== prompts[0]) {
return false;
}
}
}
return true;
});
if (!found) {
throw new assert.AssertionError({
message: 'expected prompt',
expected: PROMPT_PATTERN.toString(),
actual:
this._output.slice(0, start) +
'[prompt search starts here]' +
output,
});
}
},
{
// Increased timeout to account for slow startup on MacOS x64 hosts
timeout: 20_000,
}
);
}
waitForExit(): Promise<number> {
return this._onClose;
}
/**
* Waits for the shell to exit, asserts no errors and returns the output.
*/
async waitForCleanOutput(): Promise<string> {
await this.waitForExit();
this.assertNoErrors();
return this.output;
}
async waitForPromptOrExit(): Promise<TestShellStartupResult> {
return Promise.race([
this.waitForPrompt().then(() => ({ state: 'prompt' } as const)),
this.waitForExit().then((c) => ({ state: 'exit', exitCode: c } as const)),
]);
}
kill(signal?: SignalType): void {
this._process.kill(signal);
}
writeInput(chars: string, { end = false } = {}): void {
this._process.stdin.write(chars);
if (end) this._process.stdin.end();
}
writeInputLine(chars: string): void {
this.writeInput(`${chars}\n`);
}
async executeLine(line: string): Promise<string> {
const previousOutputLength = this._output.length;
this.writeInputLine(line);
await this.waitForPrompt(previousOutputLength);
return this._output.slice(previousOutputLength);
}
async executeLineWithJSONResult(line: string): Promise<any> {
const output = await this.executeLine(
`">>>>>>" + EJSON.stringify(${line}, {relaxed:false}) + "<<<<<<"`
);
const matching = output.match(/>>>>>>(.+)<<<<<</)?.[1];
if (!matching)
throw new Error(`Could not parse output from line: '${output}'`);
return EJSON.parse(matching);
}
assertNoErrors(): void {
const allErrors = this._getAllErrors();
if (allErrors.length) {
throw new assert.AssertionError({
message: `Expected no errors in stdout but got: ${allErrors[0]}`,
expected: '',
actual: this._output,
});
}
}
assertContainsOutput(expectedOutput: string | RegExp): void {
const onlyOutputLines = this._getOutputLines();
if (!matches(onlyOutputLines.join('\n'), expectedOutput)) {
throw new assert.AssertionError({
message: `Expected shell output to include ${inspect(expectedOutput)}`,
actual: this._output,
expected: expectedOutput,
});
}
}
assertContainsError(expectedError: string | RegExp): void {
const allErrors = this._getAllErrors();
if (!allErrors.find((error) => matches(error, expectedError))) {
throw new assert.AssertionError({
message: `Expected shell errors to include ${inspect(expectedError)}`,
actual: this._output,
expected: expectedError,
});
}
}
assertNotContainsOutput(unexpectedOutput: string | RegExp): void {
const onlyOutputLines = this._getOutputLines();
if (matches(onlyOutputLines.join('\n'), unexpectedOutput)) {
throw new assert.AssertionError({
message: `Expected shell output not to include ${inspect(
unexpectedOutput
)}`,
actual: this._output,
expected: `NOT ${unexpectedOutput}`,
});
}
}
private _getOutputLines(): string[] {
return this._output.split('\n');
}
private _getAllErrors(): string[] {
const output = this._output as any;
return [
...output.matchAll(ERROR_PATTERN_1),
...output.matchAll(ERROR_PATTERN_2),
].map((m) => m[1].trim());
}
get logId(): string | null {
const match = /^Current Mongosh Log ID:\s*(?<logId>[a-z0-9]{24})$/m.exec(
this._output
);
if (!match) {
return null;
}
return match.groups!.logId;
}
}