Skip to content

Commit

Permalink
applies changes discused in: indexzero#10
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Mar 27, 2015
1 parent b6388a8 commit 876ed3a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
5 changes: 5 additions & 0 deletions bin/ps-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node
'use strict';
require('../')(process.argv[2] || 1, function (err, data) {
console.log(data)
});
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ function childrenOfPid( pid, callback) {
if('number' == typeof pid) {
pid = pid.toString()
}
else{
pid = parseInt(pid, 10).toString();
}

es.connect(
spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm']).stdout,
Expand All @@ -28,7 +31,7 @@ function childrenOfPid( pid, callback) {
}
return cb(null, row)
}
return cb()
return cb();
}),
es.writeArray(function (err, ps) {
var parents = [pid], children = []
Expand All @@ -42,9 +45,3 @@ function childrenOfPid( pid, callback) {
})
).on('error', callback)
}

if(!module.parent) {
childrenOfPid(process.argv[2] || 1, function (err, data) {
console.log(data)
})
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
, "pre-commit": "0.0.9"
, "tape": "^3.0.3"
}
, "pre-commit": [
"coverage"
]
, "engines": {
"node": ">=0.10"
}
Expand Down
9 changes: 4 additions & 5 deletions test/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ var cp = require('child_process');
// var fs = require('fs');
// fs.chmodSync('./index.js', 777);

test(cyan('Directly Execute index.js without requiring the module'), function (t) {
test(cyan('Directly Execute bin/ps-tree.js'), function (t) {
var first = cp.exec("node -v", function(error, stdout, stderr) {
})
var child = cp.exec("node ./index.js", function(error, data) {
console.log('data: ' + data.length);
var child = cp.exec("node ./bin/ps-tree.js", function(error, data) {
// console.log('data: ' + data.length);
if (error !== null) {
console.log(red('exec error: ' + error));
}
})
// console.log(first.pid)
t.true(child, green("✓ Called index.js directly. it worked."));
t.true(child.pid, green("✓ Called ./bin/ps-tree.js directly. worked as expected"));
t.end();
});
34 changes: 13 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ test(cyan('Spawn a Parent Process which has a Two Child Processes'), function (t
t.equal(children.length, 0, green("✓ No more active child processes"));
t.end();
})
},200); // ensure the child process was both started and killed by psTree
},300); // ensure the child process was both started and killed by psTree
});

test(cyan('Attempt to call psTree without supplying a Callback'), function (t) {
test(cyan('FORCE ERROR by calling psTree without supplying a Callback'), function (t) {
var child = cp.exec("node ../index.js 12345", function(error, stdout, stderr) {
})
var errmsg = "Error: childrenOfPid(pid, callback) expects callback"
setTimeout(function(){
// setTimeout(function(){
try {
psTree(child.pid); // attempt to call psTree without a callback
}
Expand All @@ -46,28 +46,20 @@ test(cyan('Attempt to call psTree without supplying a Callback'), function (t) {
}
t.end();

},100); // using setTimeout to ensure the child process gets started
// },100); // using setTimeout to ensure the child process gets started
});


test(cyan('Spawn a Child Process'), function (t) {
var first = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) {
})
var child = cp.exec("node ../index.js '"+first.pid +"'", function(error, stdout, stderr) {
test(cyan('Spawn a Child Process and psTree with a String as pid'), function (t) {
var child = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) { });
psTree(child.pid.toString(), function (err, children) {
if(err){
console.log(err);
}
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
})
setTimeout(function(){
psTree(first.pid, function (err, children) {
if(err){
console.log(err);
}
// console.log("Children: ", children, '\n');
// t.equal(children.length, 2, green("✓ There are "+children.length+" active child processes"));
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
})
},100); // using setTimeout to ensure the child process gets started

setTimeout(function(){
psTree(child.pid, function (err, children) {
psTree(child.pid.toString(), function (err, children) {
if(err){
console.log(err);
}
Expand All @@ -76,5 +68,5 @@ test(cyan('Spawn a Child Process'), function (t) {
t.equal(children.length, 0, green("✓ No more active child processes"));
t.end();
})
},200); // ensure the child process was both started and killed by psTree
},300); // ensure the child process was both started and killed by psTree
});

0 comments on commit 876ed3a

Please sign in to comment.