mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Defeat the exec node test dragons
This commit is contained in:
parent
fdea19a45b
commit
325c6135cf
@ -32,7 +32,6 @@ module.exports = function(RED) {
|
||||
var node = this;
|
||||
|
||||
var cleanup = function(p) {
|
||||
//console.log("CLEANUP!!!",p);
|
||||
node.activeProcesses[p].kill();
|
||||
node.status({fill:"red",shape:"dot",text:"timeout"});
|
||||
node.error("Exec node timeout");
|
||||
@ -53,24 +52,28 @@ module.exports = function(RED) {
|
||||
/* istanbul ignore else */
|
||||
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
|
||||
child = spawn(cmd,arg);
|
||||
var unknownCommand = (child.pid === undefined);
|
||||
if (node.timer !== 0) {
|
||||
child.tout = setTimeout(function() { cleanup(child.pid); }, node.timer);
|
||||
}
|
||||
node.activeProcesses[child.pid] = child;
|
||||
child.stdout.on('data', function (data) {
|
||||
//console.log('[exec] stdout: ' + data);
|
||||
if (node.activeProcesses.hasOwnProperty(child.pid) && node.activeProcesses[child.pid] !== null) {
|
||||
// console.log('[exec] stdout: ' + data,child.pid);
|
||||
if (isUtf8(data)) { msg.payload = data.toString(); }
|
||||
else { msg.payload = data; }
|
||||
node.send([RED.util.cloneMessage(msg),null,null]);
|
||||
}
|
||||
});
|
||||
child.stderr.on('data', function (data) {
|
||||
//console.log('[exec] stderr: ' + data);
|
||||
if (node.activeProcesses.hasOwnProperty(child.pid) && node.activeProcesses[child.pid] !== null) {
|
||||
if (isUtf8(data)) { msg.payload = data.toString(); }
|
||||
else { msg.payload = new Buffer(data); }
|
||||
node.send([null,RED.util.cloneMessage(msg),null]);
|
||||
}
|
||||
});
|
||||
child.on('close', function (code) {
|
||||
//console.log('[exec] result: ' + code);
|
||||
if (unknownCommand || (node.activeProcesses.hasOwnProperty(child.pid) && node.activeProcesses[child.pid] !== null)) {
|
||||
delete node.activeProcesses[child.pid];
|
||||
if (child.tout) { clearTimeout(child.tout); }
|
||||
msg.payload = code;
|
||||
@ -79,11 +82,14 @@ module.exports = function(RED) {
|
||||
else if (code < 0) { node.status({fill:"red",shape:"dot",text:"rc: "+code}); }
|
||||
else { node.status({fill:"yellow",shape:"dot",text:"rc: "+code}); }
|
||||
node.send([null,null,RED.util.cloneMessage(msg)]);
|
||||
}
|
||||
});
|
||||
child.on('error', function (code) {
|
||||
delete node.activeProcesses[child.pid];
|
||||
if (child.tout) { clearTimeout(child.tout); }
|
||||
delete node.activeProcesses[child.pid];
|
||||
if (node.activeProcesses.hasOwnProperty(child.pid) && node.activeProcesses[child.pid] !== null) {
|
||||
node.error(code,RED.util.cloneMessage(msg));
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
@ -120,7 +126,10 @@ module.exports = function(RED) {
|
||||
/* istanbul ignore else */
|
||||
if (node.activeProcesses.hasOwnProperty(pid)) {
|
||||
if (node.activeProcesses[pid].tout) { clearTimeout(node.activeProcesses[pid].tout); }
|
||||
node.activeProcesses[pid].kill();
|
||||
// console.log("KILLLING",pid);
|
||||
var process = node.activeProcesses[pid];
|
||||
node.activeProcesses[pid] = null;
|
||||
process.kill();
|
||||
}
|
||||
}
|
||||
node.activeProcesses = {};
|
||||
|
@ -327,6 +327,7 @@ describe('exec node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
if (!/^v0.10/.test(process.version)) {
|
||||
it('should return an error for a bad command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"madeupcommandshouldfail", addpay:false, append:"", useSpawn:true},
|
||||
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
|
||||
@ -336,14 +337,21 @@ describe('exec node', function() {
|
||||
var n3 = helper.getNode("n3");
|
||||
var n4 = helper.getNode("n4");
|
||||
n4.on("input", function(msg) {
|
||||
if (/^v0.10/.test(process.version)) {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Number();
|
||||
msg.payload.should.be.below(0);
|
||||
} else {
|
||||
msg.should.have.property("payload");
|
||||
msg.payload.should.be.a.Number();
|
||||
msg.payload.should.be.below(0);
|
||||
}
|
||||
done();
|
||||
});
|
||||
n1.receive({payload:null});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
it('should return an error for a failing command', function(done) {
|
||||
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"mkdir /foo/bar/doo/dah", addpay:false, append:"", useSpawn:true},
|
||||
|
Loading…
Reference in New Issue
Block a user