0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-child-process-stdin.js

42 lines
965 B
JavaScript
Raw Normal View History

'use strict';
const common = require('../common');
const assert = require('assert');
2010-03-17 22:00:17 +01:00
const spawn = require('child_process').spawn;
2010-03-17 22:00:17 +01:00
const cat = spawn(common.isWindows ? 'more' : 'cat');
cat.stdin.write('hello');
cat.stdin.write(' ');
cat.stdin.write('world');
assert.strictEqual(true, cat.stdin.writable);
assert.strictEqual(false, cat.stdin.readable);
cat.stdin.end();
2010-03-17 22:00:17 +01:00
let response = '';
2010-03-17 22:00:17 +01:00
cat.stdout.setEncoding('utf8');
cat.stdout.on('data', function(chunk) {
console.log('stdout: ' + chunk);
2010-03-17 22:00:17 +01:00
response += chunk;
});
cat.stdout.on('end', common.mustCall(function() {}));
2010-03-17 22:00:17 +01:00
cat.stderr.on('data', common.mustNotCall());
2010-03-17 22:00:17 +01:00
cat.stderr.on('end', common.mustCall(function() {}));
2010-03-17 22:00:17 +01:00
cat.on('exit', common.mustCall(function(status) {
assert.strictEqual(0, status);
}));
2010-03-17 22:00:17 +01:00
cat.on('close', common.mustCall(function() {
if (common.isWindows) {
assert.strictEqual('hello world\r\n', response);
} else {
assert.strictEqual('hello world', response);
}
}));