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

child_process: set stdin properties when execed #2336

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,12 @@
stdin._handle.readStop();
});

stdin.isTTY = true;
stdin.isRaw = false;
stdin.setRawMode = function setRawMode() {
throw new Error('Not a raw device');
};
Copy link
Member

Choose a reason for hiding this comment

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

This looks incorrect to me. .isTTY should only be true when stdin refers to a real tty, not when it's a file/pipe/socket/etc.

You could wrap it in a if (!process.stdin.hasOwnProperty('isTTY')) { ... } block, although that feels kind of icky (and the fact that .isTTY is an own property is something of an implementation detail, so if (!('isTTY' in process.stdin)) { .. } is probably more correct.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, stdin.isTTY should definitely be false in the PIPE case. I corrected that in my latest diff suggestion.

The whole change is more of a correctness thing. Most scripts do just if (stdin.isTTY), in which case it wouldn't matter if it's undefined or false.


return stdin;
});

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/child-process-stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
console.log(process.stdin.isTTY);
console.log(process.stdin.isRaw);
try {
process.stdin.setRawMode();
} catch (ex) {
console.error(ex);
}
18 changes: 17 additions & 1 deletion test/parallel/test-child-process-stdin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
var common = require('../common');
var assert = require('assert');

const path = require('path');
var spawn = require('child_process').spawn;
const exec = require('child_process').exec;

var cat = spawn(common.isWindows ? 'more' : 'cat');
cat.stdin.write('hello');
Expand Down Expand Up @@ -66,3 +67,18 @@ process.on('exit', function() {
assert.equal('hello world', response);
}
});

// Regression test for https://github.com/nodejs/io.js/issues/2333
const cpFile = path.join(common.fixturesDir, 'child-process-stdin.js');
const nodeBinary = process.argv[0];

exec(`${nodeBinary} ${cpFile}`, function(err, stdout, stderr) {
const stdoutLines = stdout.split('\n');
assert.strictEqual(stdoutLines[0], 'true');
assert.strictEqual(stdoutLines[1], 'false');
assert.strictEqual(stdoutLines.length, 3);

const stderrLines = stderr.split('\n');
assert.strictEqual(stderrLines[0], '[Error: Not a raw device]');
assert.strictEqual(stderrLines.length, 2);
});