From 5d70af8c1a0c44841604417ba4cd636fdbc41bcb Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sun, 9 Aug 2015 21:37:31 +0530 Subject: [PATCH 1/2] child_process: set stdin properties when `exec`ed When a child process is created, the `stdin` will not have `isTTY`, `isRaw` and `setRawMode` properties. Because, `uv_guess_handle` in `guessHandleType` call returns `PIPE` for fd 0. So, we create a `net.Socket` and return. But normally it will return `TTY` and we create `tty.ReadStream` and return where all those properties are properly set. This path explicitly sets the above mentioned properties on the returned socket object. Fixes: https://github.com/nodejs/io.js/issues/2333 PR-URL: https://github.com/nodejs/io.js/pull/2336 --- src/node.js | 6 ++++++ test/fixtures/child-process-stdin.js | 7 +++++++ test/parallel/test-child-process-stdin.js | 19 ++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/child-process-stdin.js diff --git a/src/node.js b/src/node.js index af66b1784e7181..dfe35996305566 100644 --- a/src/node.js +++ b/src/node.js @@ -717,6 +717,12 @@ stdin._handle.readStop(); }); + stdin.isTTY = true; + stdin.isRaw = false; + stdin.setRawMode = function setRawMode() { + throw new Error('Not a raw device'); + }; + return stdin; }); diff --git a/test/fixtures/child-process-stdin.js b/test/fixtures/child-process-stdin.js new file mode 100644 index 00000000000000..3498949486c1c1 --- /dev/null +++ b/test/fixtures/child-process-stdin.js @@ -0,0 +1,7 @@ +console.log(process.stdin.isTTY); +console.log(process.stdin.isRaw); +try { + process.stdin.setRawMode(); +} catch (ex) { + console.error(ex); +} diff --git a/test/parallel/test-child-process-stdin.js b/test/parallel/test-child-process-stdin.js index c12b24579375dc..ddcfda01dc8fbc 100644 --- a/test/parallel/test-child-process-stdin.js +++ b/test/parallel/test-child-process-stdin.js @@ -1,8 +1,10 @@ 'use strict'; var common = require('../common'); var assert = require('assert'); - +const os = require('os'); +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'); @@ -66,3 +68,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(os.EOL); + assert.strictEqual(stdoutLines[0], 'true'); + assert.strictEqual(stdoutLines[1], 'false'); + assert.strictEqual(stdoutLines.length, 3); + + const stderrLines = stderr.split(os.EOL); + assert.strictEqual(stderrLines[0], '[Error: Not a raw device]'); + assert.strictEqual(stderrLines.length, 2); +}); From fa32a5b40cbe6ec061c73e17fb9aae84fc1d250f Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sun, 9 Aug 2015 22:06:07 +0530 Subject: [PATCH 2/2] Splitting with \n --- test/parallel/test-child-process-stdin.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-child-process-stdin.js b/test/parallel/test-child-process-stdin.js index ddcfda01dc8fbc..bc75b6b1b9a946 100644 --- a/test/parallel/test-child-process-stdin.js +++ b/test/parallel/test-child-process-stdin.js @@ -1,7 +1,6 @@ 'use strict'; var common = require('../common'); var assert = require('assert'); -const os = require('os'); const path = require('path'); var spawn = require('child_process').spawn; const exec = require('child_process').exec; @@ -74,12 +73,12 @@ 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(os.EOL); + const stdoutLines = stdout.split('\n'); assert.strictEqual(stdoutLines[0], 'true'); assert.strictEqual(stdoutLines[1], 'false'); assert.strictEqual(stdoutLines.length, 3); - const stderrLines = stderr.split(os.EOL); + const stderrLines = stderr.split('\n'); assert.strictEqual(stderrLines[0], '[Error: Not a raw device]'); assert.strictEqual(stderrLines.length, 2); });