node/test/simple/test-child-process-stdin.js

50 lines
984 B
JavaScript
Raw Normal View History

2010-12-05 07:20:34 +08:00
var common = require('../common');
var assert = require('assert');
2010-03-18 05:00:17 +08:00
var spawn = require('child_process').spawn;
var cat = spawn('cat');
cat.stdin.write('hello');
cat.stdin.write(' ');
cat.stdin.write('world');
cat.stdin.end();
2010-03-18 05:00:17 +08:00
var response = '';
2010-03-18 05:00:17 +08:00
var exitStatus = -1;
var gotStdoutEOF = false;
cat.stdout.setEncoding('utf8');
cat.stdout.addListener('data', function(chunk) {
console.log('stdout: ' + chunk);
2010-03-18 05:00:17 +08:00
response += chunk;
});
cat.stdout.addListener('end', function() {
2010-03-18 05:00:17 +08:00
gotStdoutEOF = true;
});
var gotStderrEOF = false;
cat.stderr.addListener('data', function(chunk) {
2010-03-18 05:00:17 +08:00
// shouldn't get any stderr output
assert.ok(false);
});
cat.stderr.addListener('end', function(chunk) {
2010-03-18 05:00:17 +08:00
gotStderrEOF = true;
});
cat.addListener('exit', function(status) {
console.log('exit event');
2010-03-18 05:00:17 +08:00
exitStatus = status;
assert.equal('hello world', response);
2010-03-18 05:00:17 +08:00
});
process.addListener('exit', function() {
2010-03-18 05:00:17 +08:00
assert.equal(0, exitStatus);
assert.equal('hello world', response);
2010-03-18 05:00:17 +08:00
});