mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Signed-off-by: Aviv Keller <me@aviv.sh> PR-URL: https://github.com/nodejs/node/pull/64034 Fixes: https://github.com/nodejs/node/issues/64523 Closes: https://github.com/nodejs/node/pull/64534 Fixes: https://github.com/nodejs/node/issues/39387 Closes: https://github.com/nodejs/node/pull/39392 Fixes: https://github.com/nodejs/node/issues/61390 Fixes: https://github.com/nodejs/node/issues/63126 Fixes: https://github.com/nodejs/node/issues/38503 Fixes: https://github.com/nodejs/node/issues/37445 Fixes: https://github.com/nodejs/node/issues/38145 Fixes: https://github.com/nodejs/node/issues/33369 Fixes: https://github.com/nodejs/node/issues/48131 Fixes: https://github.com/nodejs/node/issues/8309 Fixes: https://github.com/nodejs/node/issues/39689 Fixes: https://github.com/nodejs/node/issues/18931 Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
106 lines
3.1 KiB
JavaScript
106 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { describe, it } = require('node:test');
|
|
|
|
common.skipIfInspectorDisabled();
|
|
|
|
const { startNewREPLServer } = require('../common/repl');
|
|
|
|
const testingReplPrompt = '_REPL_TESTING_PROMPT_>';
|
|
|
|
// Processes some input in a REPL instance and returns a promise that
|
|
// resolves to the produced output (as a string).
|
|
function getReplRunOutput(inputStr, replOptions) {
|
|
return new Promise((resolve) => {
|
|
const { replServer, input, output } = startNewREPLServer({ prompt: testingReplPrompt, ...replOptions });
|
|
|
|
output.accumulator = '';
|
|
|
|
output.write = (chunk) => {
|
|
output.accumulator += chunk;
|
|
// The prompt appears after the input has been processed
|
|
if (output.accumulator.includes(testingReplPrompt)) {
|
|
replServer.close();
|
|
resolve(output.accumulator);
|
|
}
|
|
};
|
|
|
|
input.emit('data', inputStr);
|
|
|
|
if (!replOptions.preview) {
|
|
input.run(['']);
|
|
return;
|
|
}
|
|
|
|
const afterEcho = output.accumulator;
|
|
let attempts = 0;
|
|
(function waitForPreview() {
|
|
if (output.accumulator !== afterEcho || ++attempts > 100) {
|
|
input.run(['']);
|
|
return;
|
|
}
|
|
setImmediate(waitForPreview);
|
|
})();
|
|
});
|
|
}
|
|
|
|
describe('with previews', () => {
|
|
it("doesn't show previews by default", async () => {
|
|
const input = "'Hello custom' + ' eval World!'";
|
|
const output = await getReplRunOutput(
|
|
input,
|
|
{
|
|
terminal: true,
|
|
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)),
|
|
},
|
|
);
|
|
const lines = getSingleCommandLines(output);
|
|
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
|
|
assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
|
|
assert.strictEqual(lines.result, "'Hello custom eval World!'");
|
|
assert.strictEqual(lines.preview, undefined);
|
|
});
|
|
|
|
it('does show previews if `preview` is set to `true`', async () => {
|
|
const input = "'Hello custom' + ' eval World!'";
|
|
const output = await getReplRunOutput(
|
|
input,
|
|
{
|
|
terminal: true,
|
|
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)),
|
|
preview: true,
|
|
},
|
|
);
|
|
const lines = getSingleCommandLines(output);
|
|
assert.match(lines.command, /^'Hello custom' \+ ' eval World!'/);
|
|
assert.match(lines.prompt, new RegExp(`${RegExp.escape(testingReplPrompt)}$`));
|
|
assert.strictEqual(lines.result, "'Hello custom eval World!'");
|
|
assert.match(lines.preview, /'Hello custom eval World!'/);
|
|
});
|
|
});
|
|
|
|
function getSingleCommandLines(output) {
|
|
const outputLines = output.split('\n');
|
|
|
|
// The first line contains the command being run
|
|
const command = outputLines.shift();
|
|
|
|
// The last line contains the prompt (asking for some new input)
|
|
const prompt = outputLines.pop();
|
|
|
|
// The line before the last one contains the result of the command
|
|
const result = outputLines.pop();
|
|
|
|
// The line before that contains the preview of the command
|
|
const preview = outputLines.shift();
|
|
|
|
return {
|
|
command,
|
|
prompt,
|
|
result,
|
|
preview,
|
|
};
|
|
}
|