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:
Rich Trott 2017-08-03 14:10:37 -07:00
parent e88908d868
commit daf5596c27

View File

@ -87,66 +87,82 @@ function isWarned(emitter) {
} }
[ true, false ].forEach(function(terminal) { [ true, false ].forEach(function(terminal) {
let fi;
let rli;
let called;
// disable history // disable history
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal, const fi = new FakeInput();
historySize: 0 }); const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal, historySize: 0 }
);
assert.strictEqual(rli.historySize, 0); assert.strictEqual(rli.historySize, 0);
fi.emit('data', 'asdf\n'); fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? [] : undefined); assert.deepStrictEqual(rli.history, terminal ? [] : undefined);
rli.close(); rli.close();
}
// default history size 30 // 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); assert.strictEqual(rli.historySize, 30);
fi.emit('data', 'asdf\n'); fi.emit('data', 'asdf\n');
assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined); assert.deepStrictEqual(rli.history, terminal ? ['asdf'] : undefined);
rli.close(); rli.close();
}
// sending a full line // sending a full line
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
called = false; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
called = true; called = true;
assert.strictEqual(line, 'asdf'); assert.strictEqual(line, 'asdf');
}); });
fi.emit('data', 'asdf\n'); fi.emit('data', 'asdf\n');
assert.ok(called); assert.ok(called);
}
// sending a blank line // sending a blank line
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
called = false; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
called = true; called = true;
assert.strictEqual(line, ''); assert.strictEqual(line, '');
}); });
fi.emit('data', '\n'); fi.emit('data', '\n');
assert.ok(called); assert.ok(called);
}
// sending a single character with no newline // sending a single character with no newline
fi = new FakeInput(); {
rli = new readline.Interface(fi, {}); const fi = new FakeInput();
called = false; const rli = new readline.Interface(fi, {});
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
called = true; called = true;
}); });
fi.emit('data', 'a'); fi.emit('data', 'a');
assert.ok(!called); assert.ok(!called);
rli.close(); rli.close();
}
// sending a single character with no newline and then a newline // sending a single character with no newline and then a newline
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
called = false; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
called = true; called = true;
assert.strictEqual(line, 'a'); assert.strictEqual(line, 'a');
@ -156,11 +172,15 @@ function isWarned(emitter) {
fi.emit('data', '\n'); fi.emit('data', '\n');
assert.ok(called); assert.ok(called);
rli.close(); rli.close();
}
// sending multiple newlines at once // sending multiple newlines at once
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
let expectedLines = ['foo', 'bar', 'baz']; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz'];
let callCount = 0; let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
@ -169,12 +189,16 @@ function isWarned(emitter) {
fi.emit('data', `${expectedLines.join('\n')}\n`); fi.emit('data', `${expectedLines.join('\n')}\n`);
assert.strictEqual(callCount, expectedLines.length); assert.strictEqual(callCount, expectedLines.length);
rli.close(); rli.close();
}
// sending multiple newlines at once that does not end with a new line // 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 }); const fi = new FakeInput();
expectedLines = ['foo', 'bar', 'baz', 'bat']; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -182,13 +206,17 @@ function isWarned(emitter) {
fi.emit('data', expectedLines.join('\n')); fi.emit('data', expectedLines.join('\n'));
assert.strictEqual(callCount, expectedLines.length - 1); assert.strictEqual(callCount, expectedLines.length - 1);
rli.close(); rli.close();
}
// sending multiple newlines at once that does not end with a new(empty) // sending multiple newlines at once that does not end with a new(empty)
// line and a `end` event // line and a `end` event
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
expectedLines = ['foo', 'bar', 'baz', '']; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', ''];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -200,15 +228,19 @@ function isWarned(emitter) {
fi.emit('end'); fi.emit('end');
assert.strictEqual(callCount, expectedLines.length); assert.strictEqual(callCount, expectedLines.length);
rli.close(); rli.close();
}
// sending multiple newlines at once that does not end with a new line // sending multiple newlines at once that does not end with a new line
// and a `end` event(last line is) // and a `end` event(last line is)
// \r\n should emit one line event, not two // \r\n should emit one line event, not two
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
expectedLines = ['foo', 'bar', 'baz', 'bat']; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -216,12 +248,16 @@ function isWarned(emitter) {
fi.emit('data', expectedLines.join('\r\n')); fi.emit('data', expectedLines.join('\r\n'));
assert.strictEqual(callCount, expectedLines.length - 1); assert.strictEqual(callCount, expectedLines.length - 1);
rli.close(); rli.close();
}
// \r\n should emit one line event when split across multiple writes. // \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 }); const fi = new FakeInput();
expectedLines = ['foo', 'bar', 'baz', 'bat']; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -232,12 +268,16 @@ function isWarned(emitter) {
}); });
assert.strictEqual(callCount, expectedLines.length); assert.strictEqual(callCount, expectedLines.length);
rli.close(); rli.close();
}
// \r should behave like \n when alone // \r should behave like \n when alone
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: true }); const fi = new FakeInput();
expectedLines = ['foo', 'bar', 'baz', 'bat']; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: true }
);
const expectedLines = ['foo', 'bar', 'baz', 'bat'];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -245,12 +285,16 @@ function isWarned(emitter) {
fi.emit('data', expectedLines.join('\r')); fi.emit('data', expectedLines.join('\r'));
assert.strictEqual(callCount, expectedLines.length - 1); assert.strictEqual(callCount, expectedLines.length - 1);
rli.close(); rli.close();
}
// \r at start of input should output blank line // \r at start of input should output blank line
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: true }); const fi = new FakeInput();
expectedLines = ['', 'foo' ]; const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: true }
);
const expectedLines = ['', 'foo' ];
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -258,6 +302,7 @@ function isWarned(emitter) {
fi.emit('data', '\rfoo\r'); fi.emit('data', '\rfoo\r');
assert.strictEqual(callCount, expectedLines.length); assert.strictEqual(callCount, expectedLines.length);
rli.close(); rli.close();
}
// Emit two line events when the delay // Emit two line events when the delay
// between \r and \n exceeds crlfDelay // 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 // \t when there is no completer function should behave like an ordinary
// character // character
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: true }); const fi = new FakeInput();
called = false; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: true }
);
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, '\t'); assert.strictEqual(line, '\t');
assert.strictEqual(called, false); assert.strictEqual(called, false);
@ -343,17 +391,19 @@ function isWarned(emitter) {
fi.emit('data', '\n'); fi.emit('data', '\n');
assert.ok(called); assert.ok(called);
rli.close(); rli.close();
}
// \t does not become part of the input when there is a completer function // \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]; const completer = (line) => [[], line];
rli = new readline.Interface({ const rli = new readline.Interface({
input: fi, input: fi,
output: fi, output: fi,
terminal: true, terminal: true,
completer: completer completer: completer
}); });
called = false; let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, 'foo'); assert.strictEqual(line, 'foo');
assert.strictEqual(called, false); assert.strictEqual(called, false);
@ -365,9 +415,11 @@ function isWarned(emitter) {
fi.emit('data', '\n'); fi.emit('data', '\n');
assert.ok(called); assert.ok(called);
rli.close(); rli.close();
}
// constructor throws if completer is not a function or undefined // constructor throws if completer is not a function or undefined
fi = new FakeInput(); {
const fi = new FakeInput();
assert.throws(function() { assert.throws(function() {
readline.createInterface({ readline.createInterface({
input: fi, input: fi,
@ -377,18 +429,20 @@ function isWarned(emitter) {
type: TypeError, type: TypeError,
code: 'ERR_INVALID_OPT_VALUE' code: 'ERR_INVALID_OPT_VALUE'
})); }));
}
// duplicate lines are removed from history when // duplicate lines are removed from history when
// `options.removeHistoryDuplicates` is `true` // `options.removeHistoryDuplicates` is `true`
fi = new FakeInput(); {
rli = new readline.Interface({ const fi = new FakeInput();
const rli = new readline.Interface({
input: fi, input: fi,
output: fi, output: fi,
terminal: true, terminal: true,
removeHistoryDuplicates: true removeHistoryDuplicates: true
}); });
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat']; const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
callCount = 0; let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -407,18 +461,20 @@ function isWarned(emitter) {
assert.strictEqual(rli.line, expectedLines[--callCount]); assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0); assert.strictEqual(callCount, 0);
rli.close(); rli.close();
}
// duplicate lines are not removed from history when // duplicate lines are not removed from history when
// `options.removeHistoryDuplicates` is `false` // `options.removeHistoryDuplicates` is `false`
fi = new FakeInput(); {
rli = new readline.Interface({ const fi = new FakeInput();
const rli = new readline.Interface({
input: fi, input: fi,
output: fi, output: fi,
terminal: true, terminal: true,
removeHistoryDuplicates: false removeHistoryDuplicates: false
}); });
expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat']; const expectedLines = ['foo', 'bar', 'baz', 'bar', 'bat', 'bat'];
callCount = 0; let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
assert.strictEqual(line, expectedLines[callCount]); assert.strictEqual(line, expectedLines[callCount]);
callCount++; callCount++;
@ -438,12 +494,16 @@ function isWarned(emitter) {
assert.strictEqual(rli.line, expectedLines[--callCount]); assert.strictEqual(rli.line, expectedLines[--callCount]);
assert.strictEqual(callCount, 0); assert.strictEqual(callCount, 0);
rli.close(); rli.close();
}
// sending a multi-byte utf8 char over multiple writes // sending a multi-byte utf8 char over multiple writes
{
const buf = Buffer.from('☮', 'utf8'); const buf = Buffer.from('☮', 'utf8');
fi = new FakeInput(); const fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const rli = new readline.Interface(
callCount = 0; { input: fi, output: fi, terminal: terminal }
);
let callCount = 0;
rli.on('line', function(line) { rli.on('line', function(line) {
callCount++; callCount++;
assert.strictEqual(line, buf.toString('utf8')); assert.strictEqual(line, buf.toString('utf8'));
@ -455,11 +515,15 @@ function isWarned(emitter) {
fi.emit('data', '\n'); fi.emit('data', '\n');
assert.strictEqual(callCount, 1); assert.strictEqual(callCount, 1);
rli.close(); rli.close();
}
// Regression test for repl freeze, #1968: // Regression test for repl freeze, #1968:
// check that nothing fails if 'keypress' event throws. // 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 = []; const keys = [];
fi.on('keypress', function(key) { fi.on('keypress', function(key) {
keys.push(key); keys.push(key);
@ -473,11 +537,15 @@ function isWarned(emitter) {
fi.emit('data', 'bar'); fi.emit('data', 'bar');
assert.strictEqual(keys.join(''), 'fooXbar'); assert.strictEqual(keys.join(''), 'fooXbar');
rli.close(); rli.close();
}
// calling readline without `new` // calling readline without `new`
fi = new FakeInput(); {
rli = readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
called = false; const rli = readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
let called = false;
rli.on('line', function(line) { rli.on('line', function(line) {
called = true; called = true;
assert.strictEqual(line, 'asdf'); assert.strictEqual(line, 'asdf');
@ -485,32 +553,41 @@ function isWarned(emitter) {
fi.emit('data', 'asdf\n'); fi.emit('data', 'asdf\n');
assert.ok(called); assert.ok(called);
rli.close(); rli.close();
}
if (terminal) { if (terminal) {
// question // question
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
expectedLines = ['foo']; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo'];
rli.question(expectedLines[0], function() { rli.question(expectedLines[0], function() {
rli.close(); rli.close();
}); });
let cursorPos = rli._getCursorPos(); const cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0); assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, expectedLines[0].length); assert.strictEqual(cursorPos.cols, expectedLines[0].length);
rli.close(); rli.close();
}
// sending a multi-line question // sending a multi-line question
fi = new FakeInput(); {
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal }); const fi = new FakeInput();
expectedLines = ['foo', 'bar']; const rli = new readline.Interface(
{ input: fi, output: fi, terminal: terminal }
);
const expectedLines = ['foo', 'bar'];
rli.question(expectedLines.join('\n'), function() { rli.question(expectedLines.join('\n'), function() {
rli.close(); rli.close();
}); });
cursorPos = rli._getCursorPos(); const cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, expectedLines.length - 1); assert.strictEqual(cursorPos.rows, expectedLines.length - 1);
assert.strictEqual(cursorPos.cols, expectedLines.slice(-1)[0].length); assert.strictEqual(cursorPos.cols, expectedLines.slice(-1)[0].length);
rli.close(); rli.close();
} }
}
// isFullWidthCodePoint() should return false for non-numeric values // isFullWidthCodePoint() should return false for non-numeric values
[true, false, null, undefined, {}, [], 'あ'].forEach((v) => { [true, false, null, undefined, {}, [], 'あ'].forEach((v) => {
@ -559,7 +636,10 @@ function isWarned(emitter) {
.getStringWidth('\u001b[31m\u001b[39m'), 0); .getStringWidth('\u001b[31m\u001b[39m'), 0);
assert.strictEqual(internalReadline.getStringWidth('> '), 2); assert.strictEqual(internalReadline.getStringWidth('> '), 2);
{
const fi = new FakeInput();
assert.deepStrictEqual(fi.listeners(terminal ? 'keypress' : 'data'), []); assert.deepStrictEqual(fi.listeners(terminal ? 'keypress' : 'data'), []);
}
// check EventEmitter memory leak // check EventEmitter memory leak
for (let i = 0; i < 12; i++) { 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 // 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) { rli.on('line', function(line) {
called = true; called = true;
assert.strictEqual(line, 'asdf'); assert.strictEqual(line, 'asdf');
@ -602,6 +685,7 @@ function isWarned(emitter) {
rli.close(); rli.close();
}); });
}); });
}
{ {
const expected = terminal ? const expected = terminal ?