summaryrefslogtreecommitdiff
path: root/test/simple/test-child-process-stdin.js
blob: 5271ecd700c6a72cad6d2e0af2a88e9bbf03fbb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var common = require('../common');
var assert = require('assert');

var spawn = require('child_process').spawn;

var cat = spawn('cat');
cat.stdin.write('hello');
cat.stdin.write(' ');
cat.stdin.write('world');
cat.stdin.end();

var response = '';
var exitStatus = -1;

var gotStdoutEOF = false;

cat.stdout.setEncoding('utf8');
cat.stdout.addListener('data', function(chunk) {
  console.log('stdout: ' + chunk);
  response += chunk;
});

cat.stdout.addListener('end', function() {
  gotStdoutEOF = true;
});


var gotStderrEOF = false;

cat.stderr.addListener('data', function(chunk) {
  // shouldn't get any stderr output
  assert.ok(false);
});

cat.stderr.addListener('end', function(chunk) {
  gotStderrEOF = true;
});


cat.addListener('exit', function(status) {
  console.log('exit event');
  exitStatus = status;
  assert.equal('hello world', response);
});

process.addListener('exit', function() {
  assert.equal(0, exitStatus);
  assert.equal('hello world', response);
});