1
common = require("../common");
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');
10
function fixtPath(p) {
11
return path.join(common.fixturesDir, p);
14
var expected = "hello world";
16
// Test the equivalent of:
17
// $ /bin/echo "hello world" > hello.txt
18
var helloPath = fixtPath("hello.txt");
20
function test1(next) {
21
console.log("Test 1...");
22
fs.open(helloPath, 'w', 400, function (err, fd) {
24
var child = spawn('/bin/echo', [expected], {customFds: [-1, fd]});
26
assert.notEqual(child.stdin, null);
27
assert.equal(child.stdout, null);
28
assert.notEqual(child.stderr, null);
30
child.addListener('exit', function (err) {
32
fs.close(fd, function (error) {
33
if (error) throw error;
35
fs.readFile(helloPath, function (err, data) {
37
assert.equal(data.toString(), expected + "\n");
38
console.log(' File was written.');
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]});
55
assert.equal(child.stdin, null);
57
child.stdout.addListener('data', function (data) {
58
actualData += data.toString();
60
child.addListener('exit', function (code) {
62
assert.equal(actualData, "hella warld\n");
63
console.log(" File was filtered successfully");
64
fs.close(fd, function () {
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]]});
79
filter.stdout.addListener('data', function(data) {
80
console.log(" Got data --> " + data);
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");
88
echo.addListener('exit', function(code) {
89
if (code) throw "Return code was " + code;
91
fs.unlinkSync(helloPath);