1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

try to increase coverage for exec and template nodes

(works locally - may fail on travis - lets see…)
This commit is contained in:
Dave Conway-Jones 2016-04-23 17:09:46 +01:00
parent 2801838ffa
commit 8dacf72b3c
3 changed files with 133 additions and 55 deletions

View File

@ -44,6 +44,7 @@ module.exports = function(RED) {
// slice whole line by spaces (trying to honour quotes);
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g);
var cmd = arg.shift();
/* istanbul ignore else */
if (RED.settings.verbose) { node.log(cmd+" ["+arg+"]"); }
if (cmd.indexOf(" ") == -1) {
var ex = spawn(cmd,arg);
@ -78,8 +79,9 @@ module.exports = function(RED) {
}
else {
var cl = node.cmd;
if ((node.addpay === true) && ((msg.payload.toString() || "").trim() !== "")) { cl += " "+msg.payload; }
if ((node.addpay === true) && ((msg.payload || "").toString().trim() !== "")) { cl += " "+msg.payload; }
if (node.append.trim() !== "") { cl += " "+node.append; }
/* istanbul ignore else */
if (RED.settings.verbose) { node.log(cl); }
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
msg.payload = new Buffer(stdout,"binary");
@ -100,12 +102,13 @@ module.exports = function(RED) {
node.send([msg,msg2,msg3]);
delete node.activeProcesses[child.pid];
});
child.on('error',function(){})
child.on('error',function() {});
node.activeProcesses[child.pid] = child;
}
});
this.on('close',function() {
for (var pid in node.activeProcesses) {
/* istanbul ignore else */
if (node.activeProcesses.hasOwnProperty(pid)) {
node.activeProcesses[pid].kill();
}

View File

@ -1,5 +1,5 @@
/**
* Copyright 2015 IBM Corp.
* Copyright 2015,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -51,7 +51,7 @@ describe('exec node', function() {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:false, append:""},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
var spy = sinon.stub(child_process, 'exec',
function(arg1,arg2,arg3,arg4){
function(arg1,arg2,arg3,arg4) {
//console.log(arg1);
// arg3(error,stdout,stderr);
arg3(null,arg1,arg1.toUpperCase());
@ -84,7 +84,7 @@ describe('exec node', function() {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
var spy = sinon.stub(child_process, 'exec',
function(arg1,arg2,arg3,arg4){
function(arg1,arg2,arg3,arg4) {
//console.log(arg1);
// arg3(error,stdout,stderr);
arg3(null,arg1,arg1.toUpperCase());
@ -117,10 +117,10 @@ describe('exec node', function() {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more"},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
var spy = sinon.stub(child_process, 'exec',
function(arg1,arg2,arg3,arg4){
function(arg1,arg2,arg3,arg4) {
//console.log(arg1);
// arg3(error,stdout,stderr);
arg3("error",new Buffer([0x01,0x02,0x03]),"");
arg3("error",new Buffer([0x01,0x02,0x03,0x88]));
});
helper.load(execNode, flow, function() {
@ -129,46 +129,24 @@ describe('exec node', function() {
var n3 = helper.getNode("n3");
var n4 = helper.getNode("n4");
n2.on("input", function(msg) {
//console.log(msg);
//console.log("n2",msg);
msg.should.have.property("payload");
msg.payload.should.be.a.Buffer;
msg.payload.length.should.equal(3);
});
n4.on("input", function(msg) {
//console.log(msg);
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal("error");
msg.payload.length.should.equal(4);
done();
child_process.exec.restore();
});
n1.receive({payload:"and"});
n1.receive({});
});
});
});
describe.skip('calling spawn', function() {
describe('calling spawn', function() {
it('should spawn a simple command', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true
, append:"", useSpawn:true},
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:true},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
var events = require('events');
//var spy = sinon.stub(child_process, 'spawn',
//function(arg1,arg2) {
//console.log(arg1,arg2);
//var blob = new events.EventEmitter;
//blob.stdout = function() { blob.emit("data","A"); }
//blob.stderr = function() { blob.emit("data","B"); }
//console.log("blob",blob);
//setTimeout( function() {
//blob.emit("close","CLOSE");
//},150);
//return blob;
//});
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
@ -181,7 +159,6 @@ describe('exec node', function() {
msg.payload.should.be.a.String;
msg.payload.should.equal("hello world\n");
done();
//child_process.spawn.restore();
});
n1.receive({payload:"hello world"});
});
@ -190,13 +167,6 @@ describe('exec node', function() {
it('should spawn a simple command with a non string payload parameter', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:" deg C", useSpawn:true},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
//var spy = sinon.stub(child_process, 'spawn',
//function(arg1,arg2) {
//console.log(arg1,arg2);
////console.log(this);
//// arg3(error,stdout,stderr);
////arg3(null,arg1,arg1.toUpperCase());
//});
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
@ -214,27 +184,88 @@ describe('exec node', function() {
});
});
it('should error if passed multiple words to spawn command', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this wont work", addpay:false, append:"", useSpawn:true},
it('should spawn a simple command and return binary buffer', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"", useSpawn:true},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n3 = helper.getNode("n3");
var n4 = helper.getNode("n4");
n2.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.Buffer;
msg.payload.length.should.equal(7);
done();
});
n1.receive({payload:new Buffer([0x01,0x02,0x03,0x88])});
});
});
it('should now work if passed multiple words to spawn command', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo this now works", addpay:false, append:"", useSpawn:true},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n3 = helper.getNode("n3");
var n4 = helper.getNode("n4");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exec";
n2.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal("this now works\n");
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Spawn command");
n4.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal(0);
done();
},150);me
});
n1.receive({payload:null});
});
});
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"}];
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n3 = helper.getNode("n3");
var n4 = helper.getNode("n4");
n4.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal(-2);
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},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];
helper.load(execNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n3 = helper.getNode("n3");
var n4 = helper.getNode("n4");
n3.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal("mkdir: /foo/bar/doo: No such file or directory\n");
});
n4.on("input", function(msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String;
msg.payload.should.equal(1);
done();
});
n1.receive({payload:null});
});
});
});
});

View File

@ -30,7 +30,7 @@ describe('template node', function() {
it('should modify payload', function(done) {
var flow = [{id:"n1", type:"template", field: "payload", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
var flow = [{id:"n1", type:"template", field:"payload", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
@ -43,6 +43,50 @@ describe('template node', function() {
});
});
it('should modify payload in plain text mode', function(done) {
var flow = [{id:"n1", type:"template", field:"payload", syntax:"plain", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
msg.should.have.property('topic', 'bar');
msg.should.have.property('payload', 'payload={{payload}}');
done();
});
n1.receive({payload:"foo",topic: "bar"});
});
});
xit('should modify flow context', function(done) {
var flow = [{id:"n1", type:"template", field:"payload", fieldType:"flow", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
setTimeout( function() {
console.log(n2);
console.log(n2.context().global.get("payload"));
//c.should.equal(1); // should only have had one output.
done();
},50);
n1.receive({payload:"foo",topic: "bar"});
});
});
xit('should modify global context', function(done) {
var flow = [{id:"n1", type:"template", field:"payload", fieldType:"global", template:"payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
setTimeout( function() {
console.log(n2);
console.log(n2.context().global.get("payload"));
//c.should.equal(1); // should only have had one output.
done();
},50);
n1.receive({payload:"foo",topic: "bar"});
});
});
it('should handle if the field isn\'t set', function(done) {
var flow = [{id:"n1", type:"template", template: "payload={{payload}}",wires:[["n2"]]},{id:"n2",type:"helper"}];
helper.load(templateNode, flow, function() {