Skip to content

Commit bc3ae65

Browse files
committed
Merge pull request #2 from nelsonic/tests
Tests
2 parents 43dc800 + c39c4ee commit bc3ae65

File tree

10 files changed

+206
-24
lines changed

10 files changed

+206
-24
lines changed

.gitignore

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Commenting this out is preferred by some people, see
24+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
125
node_modules
2-
node_modules/*
3-
npm_debug.log
26+
27+
# Users Environment Variables
28+
.lock-wscript

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- 0.10

bin/ps-tree.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
require('../')(process.argv[2] || 1, function (err, data) {
4+
console.log(data)
5+
});

index.js

100644100755
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
var spawn = require('child_process').spawn,
1+
var spawn = require('child_process').spawn,
22
es = require('event-stream');
33

44
module.exports = childrenOfPid
55

66
function childrenOfPid( pid, callback) {
77
var headers = null;
88

9-
if('function' !== typeof callback)
9+
if('function' !== typeof callback)
1010
throw new Error('childrenOfPid(pid, callback) expects callback')
11-
if('number' == typeof pid)
11+
if('number' == typeof pid) {
1212
pid = pid.toString()
13-
13+
}
14+
else {
15+
pid = parseInt(pid, 10).toString(); // force string
16+
}
17+
1418
es.connect(
1519
spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm']).stdout,
1620
es.split(),
@@ -20,14 +24,14 @@ function childrenOfPid( pid, callback) {
2024
headers = columns
2125
else {
2226
var row = {}
23-
//for each header,
27+
//for each header,
2428
var h = headers.slice()
2529
while (h.length) {
2630
row[h.shift()] = h.length ? columns.shift() : columns.join(' ')
2731
}
2832
return cb(null, row)
2933
}
30-
return cb()
34+
return cb();
3135
}),
3236
es.writeArray(function (err, ps) {
3337
var parents = [pid], children = []
@@ -37,13 +41,7 @@ function childrenOfPid( pid, callback) {
3741
children.push(proc)
3842
}
3943
})
40-
callback(null, children)
44+
callback(null, children)
4145
})
4246
).on('error', callback)
4347
}
44-
45-
if(!module.parent) {
46-
childrenOfPid(process.argv[2] || 1, function (err, data) {
47-
console.log(data)
48-
})
49-
}

package.json

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
{ "name": "ps-tree"
2-
, "version": "0.0.3"
3-
, "description": "get all children of a pid"
2+
, "version": "0.0.4"
3+
, "description": "Get all children of a pid"
44
, "homepage": "http://github.com/indexzero/ps-tree"
5-
, "repository":
5+
, "repository":
66
{ "type": "git"
7-
, "url": "https://github.com/indexzero/ps-tree.git" }
7+
, "url": "https://github.com/indexzero/ps-tree.git" }
88
, "dependencies": {
9-
"event-stream": "~0.5"
9+
"event-stream": "~3.3.0"
1010
}
1111
, "author": "Charlie Robbins"
12-
}
12+
, "directories": {
13+
"test": "test"
14+
}
15+
, "scripts": {
16+
"quick": "./node_modules/tape/bin/tape ./test/*.js"
17+
, "test": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./test/*.js"
18+
, "direct": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./test/direct.js"
19+
, "coverage": "./node_modules/.bin/istanbul cover ./node_modules/tape/bin/tape ./test/*.js && ./node_modules/.bin/istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100"
20+
, "jshint": "./node_modules/jshint/bin/jshint -c .jshintrc --exclude-path .gitignore ."
21+
, "codeclimate": "CODECLIMATE_REPO_TOKEN=84436b4f13c70ace9c62e7f04928bf23c234eb212c0232d39d7fb1535beb2da5 ./node_modules/codeclimate-test-reporter/bin/codeclimate.js < ./coverage/lcov.info"
22+
}
23+
, "devDependencies": {
24+
"chalk": "^1.0.0"
25+
, "codeclimate-test-reporter": "0.0.4"
26+
, "ignored": "^2.0.2"
27+
, "istanbul": "^0.3.4"
28+
, "jshint": "^2.5.10"
29+
, "mkdirp": "^0.5.0"
30+
, "pre-commit": "0.0.9"
31+
, "tape": "^3.0.3"
32+
}
33+
, "pre-commit": [
34+
"coverage"
35+
]
36+
, "engines": {
37+
"node": ">=0.10"
38+
}
39+
}

readme.markdown

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ solution: use `ps-tree` to get all processes that a child_process may have start
1111
``` js
1212
var cp = require('child_process'),
1313
psTree = require('ps-tree')
14-
14+
1515
var child = cp.exec("node -e 'while (true);'",function () {...})
1616

1717
child.kill() //this will not actually kill the child it will kill the `sh` process.
@@ -38,11 +38,25 @@ used ps tree like this:
3838

3939
var cp = require('child_process'),
4040
psTree = require('ps-tree')
41-
41+
4242
var child = cp.exec("node -e 'while (true);'",function () {...})
4343

4444
psTree(child.pid, function (err, children) {
4545
cp.spawn('kill', ['-9'].concat(children.map(function (p) {return p.PID})))
4646
})
4747

48-
```
48+
```
49+
50+
If you prefer to run **psTree** from the command line,
51+
use: `node ./bin/ps-tree.js`
52+
53+
54+
<br /> <!-- badges -->
55+
[![Build Status](https://travis-ci.org/nelsonic/ps-tree.svg)](https://travis-ci.org/nelsonic/ps-tree)
56+
[![Code Climate](https://codeclimate.com/github/nelsonic/ps-tree/badges/gpa.svg)](https://codeclimate.com/github/nelsonic/ps-tree)
57+
[![Test Coverage](https://codeclimate.com/github/nelsonic/ps-tree/badges/coverage.svg)](https://codeclimate.com/github/nelsonic/ps-tree)
58+
[![npm version](https://badge.fury.io/js/ps-tree.svg)](http://badge.fury.io/js/ps-tree)
59+
[![Node.js Version][node-version-image]][node-version-url]
60+
[![Dependency Status](https://david-dm.org/nelsonic/ps-tree.svg)](https://david-dm.org/nelsonic/ps-tree)
61+
[node-version-image]: https://img.shields.io/node/v/listdirs.svg?style=flat
62+
[node-version-url]: http://nodejs.org/download/

test/direct.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var test = require('tape');
2+
var chalk = require('chalk');
3+
var red = chalk.red, green = chalk.green, cyan = chalk.cyan;
4+
5+
var cp = require('child_process');
6+
// var fs = require('fs');
7+
// fs.chmodSync('./index.js', 777);
8+
9+
test(cyan('Directly Execute bin/ps-tree.js'), function (t) {
10+
var first = cp.exec("node -v", function(error, stdout, stderr) {
11+
})
12+
var child = cp.exec("node ./bin/ps-tree.js", function(error, data) {
13+
// console.log('data: ' + data.length);
14+
if (error !== null) {
15+
console.log(red('exec error: ' + error));
16+
}
17+
})
18+
t.true(child.pid, green("✓ Called ./bin/ps-tree.js directly. worked as expected"));
19+
t.end();
20+
});

test/exec/child.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// a basic node http server
2+
var port = Math.floor(Math.random() * 60000) + 1000;
3+
// require('http').createServer(function (req, res) {
4+
// res.writeHead(200, {"Content-Type": "text/html"});
5+
// res.end('Hai');
6+
// }).listen(port);
7+
console.log("Visit: http://127.0.0.1:"+port);
8+
console.log("process.id: "+process.pid);
9+
console.log(" - - - - - - - - - - - - - - - - - - - - - - - ");

test/exec/parent.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var cp = require('child_process');
2+
var chalk = require('chalk');
3+
var red = chalk.red, green = chalk.green, cyan = chalk.cyan;
4+
var count = 0;
5+
while(count < 10) {
6+
var child = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) {
7+
console.log('stdout: ' + stdout);
8+
console.log(red('stderr: ' + stderr));
9+
if (error !== null) {
10+
console.log(red('exec error: ' + error));
11+
}
12+
})
13+
console.log("child pid: "+child.pid + " | count: "+count);
14+
count++
15+
}

test/test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var test = require('tape');
2+
var chalk = require('chalk');
3+
var red = chalk.red, green = chalk.green, cyan = chalk.cyan;
4+
5+
var cp = require('child_process'),
6+
psTree = require('../')
7+
8+
test(cyan('Spawn a Parent process which has a Two Child Processes'), function (t) {
9+
var parent = cp.exec("node ./test/exec/parent.js", function(error, stdout, stderr) {
10+
})
11+
setTimeout(function(){
12+
psTree(parent.pid, function (err, children) {
13+
if(err){
14+
console.log(err);
15+
}
16+
console.log(red("Children: "), children, '\n');
17+
t.true(children.length > 0, green("✓ There are "+children.length+" active child processes"));
18+
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
19+
})
20+
setTimeout(function(){
21+
psTree(parent.pid, function (err, children) {
22+
if(err){
23+
console.log(err);
24+
}
25+
// console.log("Children: ", children, '\n');
26+
// console.log(' ')
27+
t.equal(children.length, 0, green("✓ No more active child processes (we killed them)"));
28+
t.end();
29+
})
30+
},1000); // give psTree time to kill the processes
31+
},200); // give the child process time to spawn
32+
});
33+
34+
test(cyan('FORCE ERROR by calling psTree without supplying a Callback'), function (t) {
35+
var errmsg = "Error: childrenOfPid(pid, callback) expects callback"
36+
try {
37+
psTree(1234); // attempt to call psTree without a callback
38+
}
39+
catch(e){
40+
// console.log(red(e));
41+
t.equal(e.toString(), errmsg, green("✓ Fails when no callback supplied (as expected)"))
42+
}
43+
t.end();
44+
});
45+
46+
47+
test(cyan('Spawn a Child Process and psTree with a String as pid'), function (t) {
48+
var child = cp.exec("node ./test/exec/child.js", function(error, stdout, stderr) { });
49+
setTimeout(function(){
50+
psTree(child.pid.toString(), function (err, children) {
51+
if(err){
52+
console.log(err);
53+
}
54+
cp.spawn('kill', ['-9'].concat(children.map(function (p) { return p.PID })))
55+
})
56+
setTimeout(function() {
57+
psTree(child.pid.toString(), function (err, children) {
58+
if(err){
59+
console.log(err);
60+
}
61+
t.equal(children.length, 0, green("✓ No more active child processes"));
62+
t.end();
63+
});
64+
},1000); // give psTree time to kill the processes
65+
},200); // give the child process time to spawn
66+
});

0 commit comments

Comments
 (0)