Navigation Menu

Skip to content

Commit

Permalink
added proper unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpflug committed May 5, 2015
1 parent 08c6868 commit 1829194
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -34,15 +34,15 @@ for (i = 0, len = args.length; i < len; i++) {
// called on close of a child process
function childClose (code) {
var i, len;
code = code? (code.code || code) : code;
code = code ? (code.code || code) : code;
if (verbose) {
if (code > 0) {
console.error('`' + this.cmd + '` failed with exit code ' + code);
} else {
console.log('`' + this.cmd + '` ended successfully');
}
}
if (code > 0 && !wait) close();
if (code > 0 && !wait) close(code);
status();
}

Expand All @@ -65,7 +65,7 @@ function status () {
}

// closes all children and the process
function close () {
function close (code) {
var i, len;
for (i = 0, len = children.length; i < len; i++) {
if (!children[i].exitCode) {
Expand All @@ -74,7 +74,7 @@ function close () {
if (verbose) console.log('`' + children[i].cmd + '` will now be closed');
}
}
process.exit();
process.exit(code);
}

// cross platform compatibility
Expand Down
12 changes: 6 additions & 6 deletions package.json
Expand Up @@ -15,18 +15,18 @@
"parallelshell": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha"
},
"keywords": [
"parallel",
"shell"
],
"author": "Keith Cirkel <npm@keithcirkel.co.uk> (http://keithcirkel.co.uk/)",
"license": "MIT",
"scripts":{
"test1": "node ./index.js \"node test/willNotExit.js\"",
"test2": "node ./index.js --verbose \"node test/willThrowError.js\" \"node test/willNotExit.js\"",
"test3": "node ./index.js --verbose --wait \"node test/willThrowError.js\" \"node test/willNotExit.js\"",
"test4": "node ./index.js --help"
"devDependencies": {
"bluebird": "^2.9.25",
"chai": "^2.3.0",
"coffee-script": "^1.9.2",
"mocha": "^2.2.4"
}
}
84 changes: 84 additions & 0 deletions test/index.coffee
@@ -0,0 +1,84 @@
chai = require "chai"
should = chai.should()
spawn = require("child_process").spawn;
Promise = require("bluebird")

# cross platform compatibility
if process.platform == "win32"
sh = "cmd";
shFlag = "/c";
else
sh = "sh";
shFlag = "-c";


# children
waitingProcess = "\\\"node -e 'setTimeout(function(){},10000);'\\\""
failingProcess = "\\\"node -e 'throw new Error(\"someError\");'\\\""

usageInfo = """
-h, --help output usage information
-v, --verbose verbose logging
-w, --wait will not close silbling processes on error
""".split("\n")

spawnParallelshell = (cmd) ->
return spawn sh, [shFlag, "node './index.js' " + cmd], {
cwd: process.cwd
}

testOutput = (cmd, expectedOutput) ->
return new Promise (resolve) ->
ps = spawnParallelshell(cmd)
ps.stdout.setEncoding("utf8")
output = []
ps.stdout.on "data", (data) ->
lines = data.split("\n")
lines.pop() if lines[lines.length-1] == ""
output = output.concat(lines)
ps.stdout.on "end", () ->
for line,i in output
line.should.equal expectedOutput[i]
resolve()

describe "parallelshell", ->
it "should print on -h and --help", (done) ->
Promise.all([testOutput("-h", usageInfo), testOutput("-help", usageInfo)])
.finally done

it "should close with exitCode 2 on child error", (done) ->
ps = spawnParallelshell(failingProcess)
ps.on "close", () ->
ps.exitCode.should.equal 2
done()

it "should run with a normal child", (done) ->
ps = spawnParallelshell(waitingProcess)
setTimeout (() ->
should.not.exist(ps.signalCode)
ps.kill()
done()
),100

it "should close silbling processes on child error", (done) ->
ps = spawnParallelshell([waitingProcess,failingProcess,waitingProcess].join(" "))
ps.on "close", () ->
ps.exitCode.should.equal 2
done()

it "should wait for silbling processes on child error when called with -w or --wait", (done) ->
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
ps2 = spawnParallelshell(["--wait",waitingProcess,failingProcess,waitingProcess].join(" "))
setTimeout (() ->
should.not.exist(ps.signalCode)
should.not.exist(ps2.signalCode)
ps.kill()
ps2.kill()
done()
),100
it "should close on CTRL+C / SIGINT", (done) ->
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
ps.on "close", () ->
ps.signalCode.should.equal "SIGINT"
done()
ps.kill("SIGINT")
2 changes: 2 additions & 0 deletions test/mocha.opts
@@ -0,0 +1,2 @@
--compilers coffee:coffee-script/register
--timeout 500
2 changes: 0 additions & 2 deletions test/willNotExit.js

This file was deleted.

1 change: 0 additions & 1 deletion test/willThrowError.js

This file was deleted.

0 comments on commit 1829194

Please sign in to comment.