nodejs/test/parallel/test-repl-editor.js
James M Snell 8caa1dcee6 test: rely less on duplicative common test harness utilities
There are several cleanups here that are not just style nits...

1. The `common.isMainThread` was just a passthrough to the
   `isMainThread` export on the worker_thread module. It's
   use was inconsistent and just obfuscated the fact that
   the test file depend on the `worker_threads` built-in.
   By eliminating it we simplify the test harness a bit and
   make it clearer which tests depend on the worker_threads
   check.
2. The `common.isDumbTerminal` is fairly unnecesary since
   that just wraps a public API check.
3. Several of the `common.skipIf....` checks were inconsistently
   used and really don't need to be separate utility functions.

A key part of the motivation here is to work towards making more
of the tests more self-contained and less reliant on the common
test harness where possible.

PR-URL: https://github.com/nodejs/node/pull/56712
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-01-25 07:23:09 +00:00

130 lines
3.0 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const repl = require('repl');
const ArrayStream = require('../common/arraystream');
if (process.env.TERM === 'dumb') {
common.skip('skipping - dumb terminal');
}
// \u001b[nG - Moves the cursor to n st column
// \u001b[0J - Clear screen
// \u001b[0K - Clear to line end
const terminalCode = '\u001b[1G\u001b[0J> \u001b[3G';
const terminalCodeRegex = new RegExp(terminalCode.replace(/\[/g, '\\['), 'g');
function run({ input, output, event, checkTerminalCodes = true }) {
const stream = new ArrayStream();
let found = '';
stream.write = (msg) => found += msg.replace('\r', '');
let expected =
`${terminalCode}.editor\n` +
'// Entering editor mode (Ctrl+D to finish, Ctrl+C to cancel)\n' +
`${input}${output}\n${terminalCode}`;
const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: stream,
useColors: false
});
stream.emit('data', '.editor\n');
stream.emit('data', input);
replServer.write('', event);
replServer.close();
if (!checkTerminalCodes) {
found = found.replace(terminalCodeRegex, '').replace(/\n/g, '');
expected = expected.replace(terminalCodeRegex, '').replace(/\n/g, '');
}
assert.strictEqual(found, expected);
}
const tests = [
{
input: '',
output: '\n(To exit, press Ctrl+C again or Ctrl+D or type .exit)',
event: { ctrl: true, name: 'c' }
},
{
input: 'let i = 1;',
output: '',
event: { ctrl: true, name: 'c' }
},
{
input: 'let i = 1;\ni + 3',
output: '\n4',
event: { ctrl: true, name: 'd' }
},
{
input: ' let i = 1;\ni + 3',
output: '\n4',
event: { ctrl: true, name: 'd' }
},
{
input: '',
output: '',
checkTerminalCodes: false,
event: null,
},
];
tests.forEach(run);
// Auto code alignment for .editor mode
function testCodeAlignment({ input, cursor = 0, line = '' }) {
const stream = new ArrayStream();
const outputStream = new ArrayStream();
stream.write = () => { throw new Error('Writing not allowed!'); };
const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: outputStream,
useColors: false
});
stream.emit('data', '.editor\n');
input.split('').forEach((ch) => stream.emit('data', ch));
// Test the content of current line and the cursor position
assert.strictEqual(line, replServer.line);
assert.strictEqual(cursor, replServer.cursor);
replServer.write('', { ctrl: true, name: 'd' });
replServer.close();
// Ensure that empty lines are not saved in history
assert.notStrictEqual(replServer.history[0].trim(), '');
}
const codeAlignmentTests = [
{
input: 'let i = 1;\n'
},
{
input: ' let i = 1;\n',
cursor: 2,
line: ' '
},
{
input: ' let i = 1;\n',
cursor: 5,
line: ' '
},
{
input: ' let i = 1;\n let j = 2\n',
cursor: 2,
line: ' '
},
];
codeAlignmentTests.forEach(testCodeAlignment);