Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test for issue #5574 #5841

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions test/parallel/test-readline-interface-tty-readstream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/5574

/**
* Test that characters from stdin are not randomly lost when using a
* `readline.Interface` and a `tty.ReadStream` on `process.stdin`
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please use the // style for these kinds of comments, just for consistency


var Interface = require('readline').Interface;
var Readable = require('stream').Readable;
var ReadStream = require('tty').ReadStream;
var spawn = require('child_process').spawn;
var strictEqual = require('assert').strictEqual;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const for each of the above


var common = require('../common');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be const also, and should be the first import, immediately following the 'use strict' pragma



const kpm = 60;
const input = `1234567890
qwertyuiop
asdfghjklñ
zxcvbnm`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer not using a multiline template literal.



if (process.argv[2] === 'parent')
parent();
else
grandparent();

function grandparent() {
var child = spawn(process.execPath, [__filename, 'parent']);
child.stderr.pipe(process.stderr);

var stdin = Readable({read: function() {}});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const stdin = Readable({ read() {} });

stdin.pipe(child.stdin);

var output = '';
child.stdout.on('data', function(chunk) {
output += chunk;
});
child.stdout.setEncoding('utf8');

var index = 0;
function type() {
var data = input[index] || null;

stdin.push(data);
index++;

if (data !== null)
return setTimeout(type, 60 * 1000 / kpm);
}

type();

child.on('close', common.mustCall(function(code, signal) {
strictEqual(signal, null);
strictEqual(code, 0);
// cat on windows adds a \r\n at the end.
strictEqual(output.trim(), input.trim());
}));
}

function parent() {
Interface(process.stdin, process.stdout);

var stdin = ReadStream(process.stdin.fd);
process.stdin.pause();

var stdio = [stdin, process.stdout];

common.spawnCat({ stdio: stdio });
}