Skip to content

Commit

Permalink
moved to use exec on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpflug committed Jul 28, 2015
1 parent 3b29f57 commit f094e9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
12 changes: 5 additions & 7 deletions index.js
Expand Up @@ -72,11 +72,7 @@ function close (code) {
if (!children[i].exitCode) {
opened++;
children[i].removeAllListeners('close');
if (process.platform === 'win32') {
children[i].kill("SIGINT");
} else {
spawn(sh,[shFlag,'kill -TERM -'+children[i].pid]);
}
children[i].kill("SIGINT");
if (verbose) console.log('`' + children[i].cmd + '` will now be closed');
children[i].on('close', function() {
closed++;
Expand All @@ -86,7 +82,7 @@ function close (code) {
});
}
}
if (opened == closed) process.exit(code);
if (opened == closed) {process.exit(code);}

}

Expand All @@ -102,10 +98,12 @@ if (process.platform === 'win32') {
// start the children
children = [];
cmds.forEach(function (cmd) {
if (process.platform != 'win32') {
cmd = "exec "+cmd;
}
var child = spawn(sh,[shFlag,cmd], {
cwd: process.cwd,
env: process.env,
detached: true,
stdio: ['pipe', process.stdout, process.stderr]
})
.on('close', childClose);
Expand Down
56 changes: 35 additions & 21 deletions test/index.coffee
Expand Up @@ -3,14 +3,15 @@ should = chai.should()
spawn = require("child_process").spawn
Promise = require("bluebird")

verbose = 0

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

shArg = "-c"

# children
waitingProcess = "\"node -e 'setTimeout(function(){},10000);'\""
Expand All @@ -22,53 +23,61 @@ usageInfo = """
-w, --wait will not close sibling processes on error
""".split("\n")

cmdWrapper = (cmd) ->
if process.platform != "win32"
cmd = "exec "+cmd
if verbose
console.log "Calling: "+cmd
return cmd

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

killPs = (ps) ->
if process.platform == 'win32'
ps.kill "SIGINT"
else
spawn(sh,[shFlag,"kill -INT -"+ps.pid])

spyOnPs = (ps) ->
ps.stdout.setEncoding("utf8")
ps.stdout.on "data", (data) ->
console.log data
ps.stderr.setEncoding("utf8")
ps.stderr.on "data", (data) ->
console.log "err: "+data
ps.kill "SIGINT"

spyOnPs = (ps, verbosity=1) ->
if verbose >= verbosity
ps.stdout.setEncoding("utf8")
ps.stdout.on "data", (data) ->
console.log data
ps.stderr.setEncoding("utf8")
ps.stderr.on "data", (data) ->
console.log "err: "+data

testOutput = (cmd, expectedOutput) ->
return new Promise (resolve) ->
ps = spawnParallelshell(cmd)
spyOnPs ps, 3
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]
for line,i in expectedOutput
line.should.equal output[i]
resolve()

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

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

it "should run with a normal child", (done) ->
ps = spawnParallelshell(waitingProcess)
spyOnPs ps, 1
ps.on "close", () ->
ps.signalCode.should.equal "SIGINT"
done()
Expand All @@ -81,13 +90,16 @@ describe "parallelshell", ->

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

it "should wait for sibling 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(" "))
spyOnPs ps,2
spyOnPs ps2,2
setTimeout (() ->
should.not.exist(ps.signalCode)
should.not.exist(ps2.signalCode)
Expand All @@ -96,9 +108,11 @@ describe "parallelshell", ->
),50
Promise.all [new Promise((resolve) -> ps.on("close",resolve)),
new Promise (resolve) -> ps2.on("close",resolve)]
.finally done
.then -> done()
.catch done
it "should close on CTRL+C / SIGINT", (done) ->
ps = spawnParallelshell(["-w",waitingProcess,failingProcess,waitingProcess].join(" "))
spyOnPs ps,2
ps.on "close", () ->
ps.signalCode.should.equal "SIGINT"
done()
Expand Down

0 comments on commit f094e9a

Please sign in to comment.