11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst ArrayStream = require('../common/arraystream');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst repl = require('repl');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cilet count = 0;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cifunction run({ command, expected, useColors = false }) {
101cb0ef41Sopenharmony_ci  let accum = '';
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const output = new ArrayStream();
131cb0ef41Sopenharmony_ci  output.write = (data) => accum += data.replace('\r', '');
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  const r = repl.start({
161cb0ef41Sopenharmony_ci    prompt: '',
171cb0ef41Sopenharmony_ci    input: new ArrayStream(),
181cb0ef41Sopenharmony_ci    output,
191cb0ef41Sopenharmony_ci    terminal: false,
201cb0ef41Sopenharmony_ci    useColors
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  r.write(`${command}\n`);
241cb0ef41Sopenharmony_ci  if (typeof expected === 'string') {
251cb0ef41Sopenharmony_ci    assert.strictEqual(accum, expected);
261cb0ef41Sopenharmony_ci  } else {
271cb0ef41Sopenharmony_ci    assert.match(accum, expected);
281cb0ef41Sopenharmony_ci  }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  // Verify that the repl is still working as expected.
311cb0ef41Sopenharmony_ci  accum = '';
321cb0ef41Sopenharmony_ci  r.write('1 + 1\n');
331cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-control-regex
341cb0ef41Sopenharmony_ci  assert.strictEqual(accum.replace(/\u001b\[[0-9]+m/g, ''), '2\n');
351cb0ef41Sopenharmony_ci  r.close();
361cb0ef41Sopenharmony_ci  count++;
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst tests = [
401cb0ef41Sopenharmony_ci  {
411cb0ef41Sopenharmony_ci    useColors: true,
421cb0ef41Sopenharmony_ci    command: 'x',
431cb0ef41Sopenharmony_ci    expected: 'Uncaught ReferenceError: x is not defined\n'
441cb0ef41Sopenharmony_ci  },
451cb0ef41Sopenharmony_ci  {
461cb0ef41Sopenharmony_ci    useColors: true,
471cb0ef41Sopenharmony_ci    command: 'throw { foo: "test" }',
481cb0ef41Sopenharmony_ci    expected: "Uncaught { foo: \x1B[32m'test'\x1B[39m }\n"
491cb0ef41Sopenharmony_ci  },
501cb0ef41Sopenharmony_ci  {
511cb0ef41Sopenharmony_ci    command: 'process.on("uncaughtException", () => console.log("Foobar"));\n',
521cb0ef41Sopenharmony_ci    expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
531cb0ef41Sopenharmony_ci  },
541cb0ef41Sopenharmony_ci  {
551cb0ef41Sopenharmony_ci    command: 'x;\n',
561cb0ef41Sopenharmony_ci    expected: 'Uncaught ReferenceError: x is not defined\n'
571cb0ef41Sopenharmony_ci  },
581cb0ef41Sopenharmony_ci  {
591cb0ef41Sopenharmony_ci    command: 'process.on("uncaughtException", () => console.log("Foobar"));' +
601cb0ef41Sopenharmony_ci             'console.log("Baz");\n',
611cb0ef41Sopenharmony_ci    expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci  {
641cb0ef41Sopenharmony_ci    command: 'console.log("Baz");' +
651cb0ef41Sopenharmony_ci             'process.on("uncaughtException", () => console.log("Foobar"));\n',
661cb0ef41Sopenharmony_ci    expected: /^Baz\nUncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]:.*uncaughtException/
671cb0ef41Sopenharmony_ci  },
681cb0ef41Sopenharmony_ci];
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciprocess.on('exit', () => {
711cb0ef41Sopenharmony_ci  // To actually verify that the test passed we have to make sure no
721cb0ef41Sopenharmony_ci  // `uncaughtException` listeners exist anymore.
731cb0ef41Sopenharmony_ci  process.removeAllListeners('uncaughtException');
741cb0ef41Sopenharmony_ci  assert.strictEqual(count, tests.length);
751cb0ef41Sopenharmony_ci});
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_citests.forEach(run);
78