~ubuntu-branches/ubuntu/precise/nodejs/precise

« back to all changes in this revision

Viewing changes to test/simple/test-child-process-custom-fds.js

  • Committer: Bazaar Package Importer
  • Author(s): Jérémy Lal
  • Date: 2010-08-20 11:49:04 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100820114904-lz22w6fkth7yh179
Tags: 0.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
common = require("../common");
 
2
assert = common.assert
 
3
 
 
4
var assert = require('assert');
 
5
var spawn  = require('child_process').spawn;
 
6
var path   = require('path');
 
7
var fs     = require('fs');
 
8
var sys    = require('sys');
 
9
 
 
10
function fixtPath(p) {
 
11
  return path.join(common.fixturesDir, p);
 
12
}
 
13
 
 
14
var expected = "hello world";
 
15
 
 
16
// Test the equivalent of:
 
17
// $ /bin/echo "hello world" > hello.txt
 
18
var helloPath = fixtPath("hello.txt");
 
19
 
 
20
function test1(next) {
 
21
  console.log("Test 1...");
 
22
  fs.open(helloPath, 'w', 400, function (err, fd) {
 
23
    if (err) throw err;
 
24
    var child = spawn('/bin/echo', [expected], {customFds: [-1, fd]});
 
25
 
 
26
    assert.notEqual(child.stdin, null);
 
27
    assert.equal(child.stdout, null);
 
28
    assert.notEqual(child.stderr, null);
 
29
 
 
30
    child.addListener('exit', function (err) {
 
31
      if (err) throw err;
 
32
      fs.close(fd, function (error) {
 
33
        if (error) throw error;
 
34
 
 
35
        fs.readFile(helloPath, function (err, data) {
 
36
          if (err) throw err;
 
37
          assert.equal(data.toString(), expected + "\n");
 
38
          console.log('  File was written.');
 
39
          next(test3);
 
40
        });
 
41
      });
 
42
    });
 
43
  });
 
44
}
 
45
 
 
46
// Test the equivalent of:
 
47
// $ node ../fixture/stdio-filter.js < hello.txt
 
48
function test2(next) {
 
49
  console.log("Test 2...");
 
50
  fs.open(helloPath, 'r', undefined, function (err, fd) {
 
51
    var child = spawn(process.argv[0]
 
52
                     , [fixtPath('stdio-filter.js'), 'o', 'a']
 
53
                     , {customFds: [fd, -1, -1]});
 
54
 
 
55
    assert.equal(child.stdin, null);
 
56
    var actualData = '';
 
57
    child.stdout.addListener('data', function (data) {
 
58
      actualData += data.toString();
 
59
    });
 
60
    child.addListener('exit', function (code) {
 
61
      if (err) throw err;
 
62
      assert.equal(actualData, "hella warld\n");
 
63
      console.log("  File was filtered successfully");
 
64
      fs.close(fd, function () {
 
65
        next(test3);
 
66
      });
 
67
    });
 
68
  });
 
69
}
 
70
 
 
71
// Test the equivalent of:
 
72
// $ /bin/echo "hello world" | ../stdio-filter.js a o
 
73
function test3(next) {
 
74
  console.log("Test 3...");
 
75
  var filter = spawn(process.argv[0]
 
76
                   , [fixtPath('stdio-filter.js'), 'o', 'a']);
 
77
  var echo = spawn('/bin/echo', [expected], {customFds: [-1, filter.fds[0]]});
 
78
  var actualData = '';
 
79
  filter.stdout.addListener('data', function(data) {
 
80
    console.log("  Got data --> " + data);
 
81
    actualData += data;
 
82
  });
 
83
  filter.addListener('exit', function(code) {
 
84
    if (code) throw "Return code was " + code;
 
85
    assert.equal(actualData, "hella warld\n");
 
86
    console.log("  Talked to another process successfully");
 
87
  });
 
88
  echo.addListener('exit', function(code) {
 
89
    if (code) throw "Return code was " + code;
 
90
    filter.stdin.end();
 
91
    fs.unlinkSync(helloPath);
 
92
  });
 
93
}
 
94
 
 
95
test1(test2);