node/test/parallel/test-readline.js

152 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
const common = require('../common');
const { PassThrough } = require('stream');
const readline = require('readline');
const assert = require('assert');
test: skip some console tests on dumb terminal Add capabilities to common test module to detect and skip tests on dumb terminals. In some of our build environments, like s390x, the terminal is a dumb terminal meaning it has very rudimentary capabilities. These in turn prevent some of the tests from completing with errors as below. not ok 1777 parallel/test-readline-tab-complete --- duration_ms: 0.365 severity: fail exitcode: 1 stack: |- assert.js:103 throw new AssertionError(obj); ^ AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: '\t' !== '' at /home/abuild/rpmbuild/BUILD/node-git.8698dd98bb/test/parallel/test-readline-tab-complete.js:63:14 at Array.forEach (<anonymous>) at /home/abuild/rpmbuild/BUILD/node-git.8698dd98bb/test/parallel/test-readline-tab-complete.js:18:17 at Array.forEach (<anonymous>) at Object.<anonymous> (/home/abuild/rpmbuild/BUILD/node-git.8698dd98bb/test/parallel/test-readline-tab-complete.js:17:3) at Module._compile (internal/modules/cjs/loader.js:1176:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10) at Module.load (internal/modules/cjs/loader.js:1040:32) at Function.Module._load (internal/modules/cjs/loader.js:929:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '\t', expected: '', operator: 'strictEqual' } ... PR-URL: https://github.com/nodejs/node/pull/33165 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
2020-04-30 10:42:34 +00:00
common.skipIfDumbTerminal();
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
rl.on('line', common.mustCall((data) => {
assert.strictEqual(data, 'abc');
}));
input.end('abc');
}
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
rl.on('line', common.mustNotCall('must not be called before newline'));
input.write('abc');
}
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
rl.on('line', common.mustCall((data) => {
assert.strictEqual(data, 'abc');
}));
input.write('abc\n');
}
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
rl.write('foo');
assert.strictEqual(rl.cursor, 3);
const key = {
xterm: {
home: ['\x1b[H', { ctrl: true, name: 'a' }],
end: ['\x1b[F', { ctrl: true, name: 'e' }],
},
gnome: {
home: ['\x1bOH', { ctrl: true, name: 'a' }],
end: ['\x1bOF', { ctrl: true, name: 'e' }]
},
rxvt: {
home: ['\x1b[7', { ctrl: true, name: 'a' }],
end: ['\x1b[8', { ctrl: true, name: 'e' }]
},
putty: {
home: ['\x1b[1~', { ctrl: true, name: 'a' }],
end: ['\x1b[>~', { ctrl: true, name: 'e' }]
}
};
[key.xterm, key.gnome, key.rxvt, key.putty].forEach(function(key) {
rl.write.apply(rl, key.home);
assert.strictEqual(rl.cursor, 0);
rl.write.apply(rl, key.end);
assert.strictEqual(rl.cursor, 3);
});
}
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
const key = {
xterm: {
home: ['\x1b[H', { ctrl: true, name: 'a' }],
metab: ['\x1bb', { meta: true, name: 'b' }],
metaf: ['\x1bf', { meta: true, name: 'f' }],
}
};
rl.write('foo bar.hop/zoo');
rl.write.apply(rl, key.xterm.home);
[
{ cursor: 4, key: key.xterm.metaf },
{ cursor: 7, key: key.xterm.metaf },
{ cursor: 8, key: key.xterm.metaf },
{ cursor: 11, key: key.xterm.metaf },
{ cursor: 12, key: key.xterm.metaf },
{ cursor: 15, key: key.xterm.metaf },
{ cursor: 12, key: key.xterm.metab },
{ cursor: 11, key: key.xterm.metab },
{ cursor: 8, key: key.xterm.metab },
{ cursor: 7, key: key.xterm.metab },
{ cursor: 4, key: key.xterm.metab },
{ cursor: 0, key: key.xterm.metab },
].forEach(function(action) {
rl.write.apply(rl, action.key);
assert.strictEqual(rl.cursor, action.cursor);
});
}
{
const input = new PassThrough();
const rl = readline.createInterface({
terminal: true,
input: input
});
const key = {
xterm: {
home: ['\x1b[H', { ctrl: true, name: 'a' }],
metad: ['\x1bd', { meta: true, name: 'd' }]
}
};
rl.write('foo bar.hop/zoo');
rl.write.apply(rl, key.xterm.home);
[
'bar.hop/zoo',
'.hop/zoo',
'hop/zoo',
'/zoo',
'zoo',
'',
].forEach(function(expectedLine) {
rl.write.apply(rl, key.xterm.metad);
assert.strictEqual(rl.cursor, 0);
assert.strictEqual(rl.line, expectedLine);
});
}