test: add block scoping to test-readline-interface
Use block-scoping in test-readline-interface to avoid side effects and make tests more modular. (Some contain race conditions and will need to be moved to the sequential directory if they can't be refactored to avoid the race condition.) PR-URL: https://github.com/nodejs/node/pull/14615 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
e88908d868
commit
daf5596c27
@ -87,66 +87,82 @@ function isWarned(emitter) {
|
||||
}
|
||||
|
||||
[ true, false ].forEach(function(terminal) {
|
||||
let fi;
|
||||
let rli;
|
||||
let called;
|
||||
|
||||
// disable history
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal,
|
||||
historySize: 0 });
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal, historySize: 0 }
|
||||
);
|
||||
assert.strictEqual(rli.historySize, 0);
|
||||
|
||||
fi.emit('data', 'asdf\n');
|
||||
assert.deepStrictEqual(rli.history, terminal ? [] : undefined);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// default history size 30
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
assert.strictEqual(rli.historySize, 30);
|
||||
|
||||
fi.emit('data', 'asdf\n');
|
||||
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending a full line
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
assert.strictEqual(line, 'asdf');
|
||||
});
|
||||
fi.emit('data', 'asdf\n');
|
||||
assert.ok(called);
|
||||
}
|
||||
|
||||
// sending a blank line
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
assert.strictEqual(line, '');
|
||||
});
|
||||
fi.emit('data', '\n');
|
||||
assert.ok(called);
|
||||
}
|
||||
|
||||
// sending a single character with no newline
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface(fi, {});
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(fi, {});
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
});
|
||||
fi.emit('data', 'a');
|
||||
assert.ok(!called);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending a single character with no newline and then a newline
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
assert.strictEqual(line, 'a');
|
||||
@ -156,11 +172,15 @@ function isWarned(emitter) {
|
||||
fi.emit('data', '\n');
|
||||
assert.ok(called);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending multiple newlines at once
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
let expectedLines = ['foo', 'bar', 'baz'];
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
@ -169,12 +189,16 @@ function isWarned(emitter) {
|
||||
fi.emit('data', `${expectedLines.join('\n')}\n`);
|
||||
assert.strictEqual(callCount, expectedLines.length);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending multiple newlines at once that does not end with a new line
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -182,13 +206,17 @@ function isWarned(emitter) {
|
||||
fi.emit('data', expectedLines.join('\n'));
|
||||
assert.strictEqual(callCount, expectedLines.length - 1);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending multiple newlines at once that does not end with a new(empty)
|
||||
// line and a `end` event
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo', 'bar', 'baz', ''];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz', ''];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -200,15 +228,19 @@ function isWarned(emitter) {
|
||||
fi.emit('end');
|
||||
assert.strictEqual(callCount, expectedLines.length);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending multiple newlines at once that does not end with a new line
|
||||
// and a `end` event(last line is)
|
||||
|
||||
// \r\n should emit one line event, not two
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -216,12 +248,16 @@ function isWarned(emitter) {
|
||||
fi.emit('data', expectedLines.join('\r\n'));
|
||||
assert.strictEqual(callCount, expectedLines.length - 1);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// \r\n should emit one line event when split across multiple writes.
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -232,12 +268,16 @@ function isWarned(emitter) {
|
||||
});
|
||||
assert.strictEqual(callCount, expectedLines.length);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// \r should behave like \n when alone
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: true }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -245,12 +285,16 @@ function isWarned(emitter) {
|
||||
fi.emit('data', expectedLines.join('\r'));
|
||||
assert.strictEqual(callCount, expectedLines.length - 1);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// \r at start of input should output blank line
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
|
||||
expectedLines = ['', 'foo' ];
|
||||
callCount = 0;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: true }
|
||||
);
|
||||
const expectedLines = ['', 'foo' ];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -258,6 +302,7 @@ function isWarned(emitter) {
|
||||
fi.emit('data', '\rfoo\r');
|
||||
assert.strictEqual(callCount, expectedLines.length);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// Emit two line events when the delay
|
||||
// between \r and \n exceeds crlfDelay
|
||||
@ -331,9 +376,12 @@ function isWarned(emitter) {
|
||||
|
||||
// \t when there is no completer function should behave like an ordinary
|
||||
// character
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: true }
|
||||
);
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, '\t');
|
||||
assert.strictEqual(called, false);
|
||||
@ -343,17 +391,19 @@ function isWarned(emitter) {
|
||||
fi.emit('data', '\n');
|
||||
assert.ok(called);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// \t does not become part of the input when there is a completer function
|
||||
fi = new FakeInput();
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const completer = (line) => [[], line];
|
||||
rli = new readline.Interface({
|
||||
const rli = new readline.Interface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
terminal: true,
|
||||
completer: completer
|
||||
});
|
||||
called = false;
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, 'foo');
|
||||
assert.strictEqual(called, false);
|
||||
@ -365,9 +415,11 @@ function isWarned(emitter) {
|
||||
fi.emit('data', '\n');
|
||||
assert.ok(called);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// constructor throws if completer is not a function or undefined
|
||||
fi = new FakeInput();
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
assert.throws(function() {
|
||||
readline.createInterface({
|
||||
input: fi,
|
||||
@ -377,18 +429,20 @@ function isWarned(emitter) {
|
||||
type: TypeError,
|
||||
code: 'ERR_INVALID_OPT_VALUE'
|
||||
}));
|
||||
}
|
||||
|
||||
// duplicate lines are removed from history when
|
||||
// `options.removeHistoryDuplicates` is `true`
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
terminal: true,
|
||||
removeHistoryDuplicates: true
|
||||
});
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
|
||||
callCount = 0;
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -407,18 +461,20 @@ function isWarned(emitter) {
|
||||
assert.strictEqual(rli.line, expectedLines[--callCount]);
|
||||
assert.strictEqual(callCount, 0);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// duplicate lines are not removed from history when
|
||||
// `options.removeHistoryDuplicates` is `false`
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface({
|
||||
input: fi,
|
||||
output: fi,
|
||||
terminal: true,
|
||||
removeHistoryDuplicates: false
|
||||
});
|
||||
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
|
||||
callCount = 0;
|
||||
const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
assert.strictEqual(line, expectedLines[callCount]);
|
||||
callCount++;
|
||||
@ -438,12 +494,16 @@ function isWarned(emitter) {
|
||||
assert.strictEqual(rli.line, expectedLines[--callCount]);
|
||||
assert.strictEqual(callCount, 0);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending a multi-byte utf8 char over multiple writes
|
||||
{
|
||||
const buf = Buffer.from('☮', 'utf8');
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
callCount = 0;
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
let callCount = 0;
|
||||
rli.on('line', function(line) {
|
||||
callCount++;
|
||||
assert.strictEqual(line, buf.toString('utf8'));
|
||||
@ -455,11 +515,15 @@ function isWarned(emitter) {
|
||||
fi.emit('data', '\n');
|
||||
assert.strictEqual(callCount, 1);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// Regression test for repl freeze, #1968:
|
||||
// check that nothing fails if 'keypress' event throws.
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: true }
|
||||
);
|
||||
const keys = [];
|
||||
fi.on('keypress', function(key) {
|
||||
keys.push(key);
|
||||
@ -473,11 +537,15 @@ function isWarned(emitter) {
|
||||
fi.emit('data', 'bar');
|
||||
assert.strictEqual(keys.join(''), 'fooXbar');
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// calling readline without `new`
|
||||
fi = new FakeInput();
|
||||
rli = readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
called = false;
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
assert.strictEqual(line, 'asdf');
|
||||
@ -485,32 +553,41 @@ function isWarned(emitter) {
|
||||
fi.emit('data', 'asdf\n');
|
||||
assert.ok(called);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
if (terminal) {
|
||||
// question
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo'];
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo'];
|
||||
rli.question(expectedLines[0], function() {
|
||||
rli.close();
|
||||
});
|
||||
let cursorPos = rli._getCursorPos();
|
||||
const cursorPos = rli._getCursorPos();
|
||||
assert.strictEqual(cursorPos.rows, 0);
|
||||
assert.strictEqual(cursorPos.cols, expectedLines[0].length);
|
||||
rli.close();
|
||||
}
|
||||
|
||||
// sending a multi-line question
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
|
||||
expectedLines = ['foo', 'bar'];
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: fi, terminal: terminal }
|
||||
);
|
||||
const expectedLines = ['foo', 'bar'];
|
||||
rli.question(expectedLines.join('\n'), function() {
|
||||
rli.close();
|
||||
});
|
||||
cursorPos = rli._getCursorPos();
|
||||
const cursorPos = rli._getCursorPos();
|
||||
assert.strictEqual(cursorPos.rows, expectedLines.length - 1);
|
||||
assert.strictEqual(cursorPos.cols, expectedLines.slice(-1)[0].length);
|
||||
rli.close();
|
||||
}
|
||||
}
|
||||
|
||||
// isFullWidthCodePoint() should return false for non-numeric values
|
||||
[true, false, null, undefined, {}, [], 'あ'].forEach((v) => {
|
||||
@ -559,7 +636,10 @@ function isWarned(emitter) {
|
||||
.getStringWidth('\u001b[31m\u001b[39m'), 0);
|
||||
assert.strictEqual(internalReadline.getStringWidth('> '), 2);
|
||||
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
assert.deepStrictEqual(fi.listeners(terminal ? 'keypress' : 'data'), []);
|
||||
}
|
||||
|
||||
// check EventEmitter memory leak
|
||||
for (let i = 0; i < 12; i++) {
|
||||
@ -573,10 +653,13 @@ function isWarned(emitter) {
|
||||
}
|
||||
|
||||
// can create a new readline Interface with a null output arugument
|
||||
fi = new FakeInput();
|
||||
rli = new readline.Interface({ input: fi, output: null, terminal: terminal });
|
||||
{
|
||||
const fi = new FakeInput();
|
||||
const rli = new readline.Interface(
|
||||
{ input: fi, output: null, terminal: terminal }
|
||||
);
|
||||
|
||||
called = false;
|
||||
let called = false;
|
||||
rli.on('line', function(line) {
|
||||
called = true;
|
||||
assert.strictEqual(line, 'asdf');
|
||||
@ -602,6 +685,7 @@ function isWarned(emitter) {
|
||||
rli.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
const expected = terminal ?
|
||||
|
Loading…
x
Reference in New Issue
Block a user