Skip to content
Closed
Changes from 1 commit
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
Next Next commit
Test for #5574
Test that characters from stdin are not randomly lost when using a
`readline.Interface` and a `tty.ReadStream` on `process.stdin`
  • Loading branch information
piranna committed Mar 24, 2016
commit 1d68574d50cc0761827b3285d9ba767baa55fd11
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 @@
/**
* 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
Copy Markdown
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


'use strict';

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

var spawnCat = require('../common').spawnCat;


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

Copy link
Copy Markdown
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
Copy Markdown
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', function(code, signal) {
assert.equal(code, 0);
assert.equal(signal, null);
// cat on windows adds a \r\n at the end.
assert.equal(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];

spawnCat({ stdio: stdio });
}