diff --git a/bin/ps-tree.js b/bin/ps-tree.js new file mode 100644 index 0000000..1a008a4 --- /dev/null +++ b/bin/ps-tree.js @@ -0,0 +1,5 @@ +#!/usr/bin/env node +'use strict'; +require('../')(process.argv[2] || 1, function (err, data) { + console.log(data) +}); diff --git a/index.js b/index.js index 3023de7..bb4a425 100755 --- a/index.js +++ b/index.js @@ -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, @@ -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 = [] @@ -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) - }) -} diff --git a/package.json b/package.json index f7adee5..a1ab89a 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,9 @@ , "pre-commit": "0.0.9" , "tape": "^3.0.3" } +, "pre-commit": [ + "coverage" + ] , "engines": { "node": ">=0.10" } diff --git a/test/direct.js b/test/direct.js index 6a0a407..4263332 100644 --- a/test/direct.js +++ b/test/direct.js @@ -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(); }); diff --git a/test/test.js b/test/test.js index e130ce8..ccf8848 100644 --- a/test/test.js +++ b/test/test.js @@ -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 } @@ -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); } @@ -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 });