nodejs/test/parallel/test-child-process-stdin.js

59 lines
1.2 KiB
JavaScript
Raw Normal View History

'use strict';
2010-12-04 15:20:34 -08:00
var common = require('../common');
var assert = require('assert');
2010-03-17 14:00:17 -07:00
var spawn = require('child_process').spawn;
var cat = spawn(common.isWindows ? 'more' : 'cat');
cat.stdin.write('hello');
cat.stdin.write(' ');
cat.stdin.write('world');
assert.ok(cat.stdin.writable);
assert.ok(!cat.stdin.readable);
cat.stdin.end();
2010-03-17 14:00:17 -07:00
var response = '';
2010-03-17 14:00:17 -07:00
var exitStatus = -1;
var closed = false;
2010-03-17 14:00:17 -07:00
cat.stdout.setEncoding('utf8');
cat.stdout.on('data', function(chunk) {
console.log('stdout: ' + chunk);
2010-03-17 14:00:17 -07:00
response += chunk;
});
cat.stdout.on('end', common.mustCall(function() {}));
2010-03-17 14:00:17 -07:00
cat.stderr.on('data', function(chunk) {
2010-03-17 14:00:17 -07:00
// shouldn't get any stderr output
assert.ok(false);
});
cat.stderr.on('end', common.mustCall(function() {}));
2010-03-17 14:00:17 -07:00
cat.on('exit', function(status) {
console.log('exit event');
2010-03-17 14:00:17 -07:00
exitStatus = status;
});
cat.on('close', function() {
closed = true;
if (common.isWindows) {
assert.equal('hello world\r\n', response);
} else {
assert.equal('hello world', response);
}
2010-03-17 14:00:17 -07:00
});
process.on('exit', function() {
2010-03-17 14:00:17 -07:00
assert.equal(0, exitStatus);
assert(closed);
if (common.isWindows) {
assert.equal('hello world\r\n', response);
} else {
assert.equal('hello world', response);
}
2010-03-17 14:00:17 -07:00
});