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

Improve node registry test coverage

This commit is contained in:
Nick O'Leary 2015-11-13 23:16:47 +00:00
parent 93f5da325b
commit 45f67191ba
28 changed files with 1065 additions and 784 deletions

View File

@ -31,12 +31,6 @@ function init(_settings) {
loader.init(settings);
registry.init(settings,loader);
}
//TODO: defaultNodesDir/disableNodePathScan are to make testing easier.
// When the tests are componentized to match the new registry structure,
// these flags belong on localfilesystem.load, not here.
function load(defaultNodesDir,disableNodePathScan) {
return loader.load(defaultNodesDir,disableNodePathScan);
}
function addModule(module) {
return loader.addModule(module).then(function() {
@ -58,7 +52,7 @@ function enableNodeSet(typeOrId) {
module.exports = {
init:init,
load:load,
load:loader.load,
clear: registry.clear,
registerType: registry.registerNodeConstructor,

View File

@ -133,7 +133,7 @@ function scanDirForNodesModules(dir,moduleName) {
* @return a list of node modules: {dir,package}
*/
function scanTreeForNodesModules(moduleName) {
var dir = __dirname+"/../../../../nodes";
var dir = defaultNodesDir;
var results = [];
var userDir;

View File

@ -20,762 +20,110 @@ var path = require("path");
var when = require("when");
var fs = require("fs");
var RedNodes = require("../../../../../red/runtime/nodes");
var RedNode = require("../../../../../red/runtime/nodes/Node");
var typeRegistry = require("../../../../../red/runtime/nodes/registry");
var events = require("../../../../../red/runtime/events");
var registry = require("../../../../../red/runtime/nodes/registry");
var installer = require("../../../../../red/runtime/nodes/registry/installer");
var loader = require("../../../../../red/runtime/nodes/registry/loader");
var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry");
describe('red/nodes/registry/index', function() {
var stubs = [];
afterEach(function() {
typeRegistry.clear();
while(stubs.length) {
stubs.pop().restore();
}
})
describe('#init',function() {
it('intialises components', function() {
stubs.push(sinon.stub(installer,"init"));
stubs.push(sinon.stub(loader,"init"));
stubs.push(sinon.stub(typeRegistry,"init"));
registry.init({});
installer.init.called.should.be.true;
loader.init.called.should.be.true;
typeRegistry.init.called.should.be.true;
})
});
var resourcesDir = path.join(__dirname,"..","resources",path.sep);
function stubSettings(s,available,initialConfig) {
s.available = function() {return available;};
s.set = function(s,v) { return when.resolve();};
s.get = function(s) { return initialConfig;};
return s;
}
var settings = stubSettings({},false,null);
var settingsWithStorage = stubSettings({},true,null);
it('handles nodes that export a function', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-1");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
describe('#addModule', function() {
it('loads the module and returns its info', function(done) {
stubs.push(sinon.stub(loader,"addModule",function(module) {
return when.resolve();
}));
stubs.push(sinon.stub(typeRegistry,"getModuleInfo", function(module) {
return "info";
}));
registry.addModule("foo").then(function(info) {
info.should.eql("info");
done();
}).otherwise(function(err) { done(err); });
});
});
it('handles nodes that export a function returning a resolving promise', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode2",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode2");
list[0].should.have.property("name","TestNode2");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-2"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-2");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodes that export a function returning a rejecting promise', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode3",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode3");
list[0].should.have.property("name","TestNode3");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-3"]);
list[0].should.have.property("enabled",true);
list[0].should.have.property("err","fail");
var nodeConstructor = typeRegistry.get("test-node-3");
(nodeConstructor === null).should.be.true;
done();
}).catch(function(e) {
done(e);
});
});
it('handles files containing multiple nodes', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "MultipleNodes1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/MultipleNodes1");
list[0].should.have.property("name","MultipleNodes1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-multiple-1a","test-node-multiple-1b"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var nodeConstructor = typeRegistry.get("test-node-multiple-1a");
nodeConstructor.should.be.type("function");
nodeConstructor = typeRegistry.get("test-node-multiple-1b");
nodeConstructor.should.be.type("function");
done();
}).catch(function(e) {
done(e);
it('rejects if loader rejects', function(done) {
stubs.push(sinon.stub(loader,"addModule",function(module) {
return when.reject("error");
}));
stubs.push(sinon.stub(typeRegistry,"getModuleInfo", function(module) {
return "info";
}));
registry.addModule("foo").then(function(info) {
done(new Error("unexpected resolve"));
}).otherwise(function(err) {
err.should.eql("error");
done();
})
});
});
it('handles nested directories', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "NestedDirectoryNode",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/NestedNode");
list[0].should.have.property("name","NestedNode");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["nested-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
done();
}).catch(function(e) {
done(e);
describe('#enableNode',function() {
it('enables a node set',function(done) {
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
return when.resolve();
}));
stubs.push(sinon.stub(typeRegistry,"getNodeInfo", function() {
return {id:"node-set",loaded:true};
}));
registry.enableNode("node-set").then(function(ns) {
typeRegistry.enableNodeSet.called.should.be.true;
ns.should.have.a.property('id','node-set');
done();
}).otherwise(function(err) { done(err); });
});
});
it('emits type-registered and node-icon-dir events', function(done) {
var eventEmitSpy = sinon.spy(events,"emit");
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "NestedDirectoryNode",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/NestedNode");
list[0].should.have.property("name","NestedNode");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["nested-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
eventEmitSpy.callCount.should.equal(3);
eventEmitSpy.firstCall.args[0].should.be.equal("node-icon-dir");
eventEmitSpy.firstCall.args[1].should.be.equal(resourcesDir + "NestedDirectoryNode" + path.sep + "NestedNode" + path.sep + "icons");
eventEmitSpy.secondCall.args[0].should.be.equal("node-locales-dir");
eventEmitSpy.thirdCall.args[0].should.be.equal("type-registered");
eventEmitSpy.thirdCall.args[1].should.be.equal("nested-node-1");
done();
}).catch(function(e) {
done(e);
}).finally(function() {
eventEmitSpy.restore();
});
});
it('rejects a duplicate node type registration during load', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "DuplicateTestNode"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
done();
}).catch(function(e) {
done(e);
});
});
it('rejects a duplicate node type registration', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
it('rejects if node unknown',function() {
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
throw new Error('failure');
}));
/*jshint immed: false */
(function(){
typeRegistry.registerType("test-node-1",{});
registry.enableNode("node-set")
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodesDir as a string', function(done) {
typeRegistry.init(stubSettings({
nodesDir :resourcesDir + "TestNode1"
},false));
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("types",["test-node-1"]);
done();
}).catch(function(e) {
done("Loading of non-existing nodesDir should succeed");
});
});
it('handles invalid nodesDir',function(done) {
typeRegistry.init(stubSettings({
nodesDir : "wontexist"
},false));
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
done();
}).catch(function(e) {
done("Loading of non-existing nodesDir should succeed");
});
});
it('returns nothing for an unregistered type config', function(done) {
typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function(){
var config = typeRegistry.getNodeConfig("imaginary-shark");
(config === null).should.be.true;
done();
}).catch(function(e) {
done(e);
});
});
it('excludes node files listed in nodesExcludes',function(done) {
typeRegistry.init(stubSettings({
nodesExcludes: [ "TestNode1.js" ],
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("types",["test-node-2"]);
done();
}).catch(function(e) {
done(e);
});
});
it('returns the node configurations', function(done) {
typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2"]
},false));
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
var nodeConfigs = typeRegistry.getNodeConfigs();
// TODO: this is brittle...
nodeConfigs.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script>\n\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-1',{});</script>\n<style></style>\n<p>this should be filtered out</p>\n<script type=\"text/x-red\" data-help-name=\"test-node-1\"></script><script type=\"text/x-red\" data-template-name=\"test-node-2\"></script>\n\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-2',{});</script>\n<style></style>\n<script type=\"text/x-red\" data-help-name=\"test-node-2\"></script>");
var nodeId = list[0].id;
var nodeConfig = typeRegistry.getNodeConfig(nodeId);
nodeConfig.should.equal("<script type=\"text/x-red\" data-template-name=\"test-node-1\"></script>\n\n<script type=\"text/javascript\">RED.nodes.registerType('test-node-1',{});</script>\n<style></style>\n<p>this should be filtered out</p>\n<script type=\"text/x-red\" data-help-name=\"test-node-1\"></script>");
done();
}).catch(function(e) {
done(e);
});
});
it('stores the node list', function(done) {
var settings = {
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "TestNode2",resourcesDir + "TestNode3"],
available: function() { return true; },
set: function(s,v) { return when.resolve(); },
get: function(s) { return null; }
};
var settingsSave = sinon.spy(settings,"set");
typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function() {
var nodeList = typeRegistry.getNodeList();
var moduleList = typeRegistry.getModuleList();
Object.keys(moduleList).should.have.length(1);
moduleList.should.have.a.property("node-red");
Object.keys(moduleList["node-red"].nodes).should.have.length(3);
nodeList.should.be.Array.and.have.length(3);
settingsSave.callCount.should.equal(1);
settingsSave.firstCall.args[0].should.be.equal("nodes");
var savedList = settingsSave.firstCall.args[1];
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("id","node-red/TestNode1");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("name","TestNode1");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("module","node-red");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("file");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("enabled",true);
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("types");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("config");
moduleList['node-red'].nodes['TestNode1'].should.have.a.property("template");
savedList['node-red'].nodes['TestNode1'].should.not.have.a.property("id");
savedList['node-red'].nodes['TestNode1'].should.have.a.property("name",moduleList['node-red'].nodes['TestNode1'].name);
savedList['node-red'].nodes['TestNode1'].should.have.a.property("module",moduleList['node-red'].nodes['TestNode1'].module);
savedList['node-red'].nodes['TestNode1'].should.have.a.property("file",moduleList['node-red'].nodes['TestNode1'].file);
savedList['node-red'].nodes['TestNode1'].should.have.a.property("enabled",moduleList['node-red'].nodes['TestNode1'].enabled);
savedList['node-red'].nodes['TestNode1'].should.have.a.property("types",moduleList['node-red'].nodes['TestNode1'].types);
savedList['node-red'].nodes['TestNode1'].should.not.have.a.property("config");
savedList['node-red'].nodes['TestNode1'].should.not.have.a.property("template");
done();
}).catch(function(e) {
done(e);
}).finally(function() {
settingsSave.restore();
});
});
it('returns node info by type or id', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
var id = "node-red/TestNode1";
var type = "test-node-1";
var info = typeRegistry.getNodeInfo(id);
info.should.have.property("loaded");
delete info.loaded;
list[0].should.eql(info);
var info2 = typeRegistry.getNodeInfo(type);
info2.should.have.property("loaded");
delete info2.loaded;
list[0].should.eql(info2);
done();
}).catch(function(e) {
done(e);
});
});
it('returns null node info for unrecognised id', function(done) {
typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
should.not.exist(typeRegistry.getNodeInfo("does-not-exist"));
done();
}).catch(function(e) {
done(e);
});
});
describe('with mocked filesystem', function() {
beforeEach(function() {
var _join = path.join;
sinon.stub(path,"join",function() {
console.log(arguments);
var result;
if (arguments.length == 3 && arguments[2] == "package.json") {
result = _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1],arguments[2]);
} else if (arguments.length == 2 && arguments[1] == "TestNodeModule") {
result = _join(resourcesDir,"TestNodeModule" + path.sep + "node_modules" + path.sep,arguments[1]);
} else {
result = _join.apply(this,arguments);
}
console.log("==",result);
return result;
});
var originalReaddirSync = fs.readdirSync;
var callCount = 0;
sinon.stub(fs,"readdirSync",function(dir) {
var result = [];
if (callCount == 1) {
result = originalReaddirSync(resourcesDir + "TestNodeModule" + path.sep + "node_modules");
}
callCount++;
console.log("@",callCount,result)
return result;
});
});
afterEach(function() {
if (path.join.restore) {
path.join.restore();
}
if (fs.readdirSync.restore) {
fs.readdirSync.restore();
}
});
it('returns modules list', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule").then(function() {
var list = typeRegistry.getModuleList();
Object.keys(list).should.have.length(1);
list.should.have.a.property("TestNodeModule");
Object.keys(list["TestNodeModule"].nodes).should.have.length(2);
list["TestNodeModule"].nodes["TestNodeMod1"].should.have.property("name", "TestNodeMod1");
list["TestNodeModule"].nodes["TestNodeMod2"].should.have.property("name", "TestNodeMod2");
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('returns module info', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule").then(function(modInfo) {
var info = typeRegistry.getModuleInfo("TestNodeModule");
modInfo.should.eql(info);
should.not.exist(typeRegistry.getModuleInfo("does-not-exist"));
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('scans the node_modules path for node files', function(done) {
var eventEmitSpy = sinon.spy(events,"emit");
typeRegistry.init(settings);
typeRegistry.load("wontexist",false).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
list[0].should.have.property("id","TestNodeModule/TestNodeMod1");
list[0].should.have.property("name","TestNodeMod1");
list[0].should.have.property("module","TestNodeModule");
list[0].should.have.property("types",["test-node-mod-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
list[1].should.have.property("id","TestNodeModule/TestNodeMod2");
list[1].should.have.property("name","TestNodeMod2");
list[1].should.have.property("module","TestNodeModule");
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",true);
list[1].should.have.property("err");
eventEmitSpy.callCount.should.equal(3);
eventEmitSpy.firstCall.args[0].should.be.equal("node-locales-dir");
eventEmitSpy.secondCall.args[0].should.be.equal("node-icon-dir");
eventEmitSpy.secondCall.args[1].should.be.equal(
resourcesDir + "TestNodeModule" + path.sep+ "node_modules" + path.sep + "TestNodeModule" + path.sep + "icons");
eventEmitSpy.thirdCall.args[0].should.be.equal("type-registered");
eventEmitSpy.thirdCall.args[1].should.be.equal("test-node-mod-1");
it('triggers a node load',function(done) {
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
return when.resolve();
}));
var calls = 0;
stubs.push(sinon.stub(typeRegistry,"getNodeInfo", function() {
// loaded=false on first call, true on subsequent
return {id:"node-set",loaded:(calls++>0)};
}));
stubs.push(sinon.stub(loader,"loadNodeSet",function(){return when.resolve();}));
stubs.push(sinon.stub(typeRegistry,"getFullNodeInfo"));
registry.enableNode("node-set").then(function(ns) {
typeRegistry.enableNodeSet.called.should.be.true;
loader.loadNodeSet.called.should.be.true;
ns.should.have.a.property('id','node-set');
ns.should.have.a.property('loaded',true);
done();
}).catch(function(e) {
done(e);
}).finally(function() {
eventEmitSpy.restore();
});
}).otherwise(function(err) { done(err); });
});
it('allows nodes to be added by module name', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addModule("TestNodeModule").then(function(modInfo) {
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
list[0].should.have.property("id","TestNodeModule/TestNodeMod1");
list[0].should.have.property("name","TestNodeMod1");
list[0].should.have.property("module","TestNodeModule");
list[0].should.have.property("types",["test-node-mod-1"]);
list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err");
list[1].should.have.property("id","TestNodeModule/TestNodeMod2");
list[1].should.have.property("name","TestNodeMod2");
list[1].should.have.property("module","TestNodeModule");
list[1].should.have.property("types",["test-node-mod-2"]);
list[1].should.have.property("enabled",true);
list[1].should.have.property("err");
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('adds module with version number', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
typeRegistry.addModule("TestNodeModule","0.0.1").then(function(node) {
var module = typeRegistry.getModuleInfo("TestNodeModule");
module.should.have.property("name","TestNodeModule");
module.should.have.property("version","0.0.1");
var modules = typeRegistry.getModuleList();
modules.should.have.property("TestNodeModule");
modules["TestNodeModule"].should.have.property("version","0.0.1");
done();
}).catch(function(e) {
done(e);
});
}).catch(function(e) {
done(e);
});
});
it('rejects adding duplicate node modules', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load('wontexist',false).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
typeRegistry.addModule("TestNodeModule").then(function(node) {
done(new Error("addModule resolved"));
}).otherwise(function(err) {
done();
});
}).catch(function(e) {
done(e);
});
});
it('fails to add non-existent module name', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
typeRegistry.addModule("DoesNotExistModule").then(function(node) {
done(new Error("ENOENT not thrown"));
}).otherwise(function(e) {
e.code.should.eql("MODULE_NOT_FOUND");
done();
});
}).catch(function(e) {
done(e);
});
});
it('removes nodes from the registry by module', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load('wontexist',false).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(2);
var res = typeRegistry.removeModule("TestNodeModule");
res.should.be.an.Array.and.have.lengthOf(2);
res[0].should.have.a.property("id",list[0].id);
res[1].should.have.a.property("id",list[1].id);
list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
done();
}).catch(function(e) {
done(e);
});
});
});
it('fails to remove non-existent module name', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
/*jshint immed: false */
(function() {
typeRegistry.removeModule("DoesNotExistModule");
}).should.throw("Unrecognised module: DoesNotExistModule");
done();
}).catch(function(e) {
done(e);
});
});
it('allows nodes to be enabled and disabled by id', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir+path.sep+"TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("enabled",true);
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
typeRegistry.disableNode(list[0].id).then(function(info) {
info.should.have.property("id",list[0].id);
info.should.have.property("enabled",false);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
typeRegistry.getNodeConfigs().length.should.equal(0);
typeRegistry.enableNode(list[0].id).then(function(info2) {
info2.should.have.property("id",list[0].id);
info2.should.have.property("enabled",true);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
});
});
}).catch(function(e) {
done(e);
});
});
it('allows nodes to be enabled and disabled by node-type', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load(resourcesDir+path.sep+"TestNode1",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1);
list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red");
list[0].should.have.property("types",["test-node-1"]);
list[0].should.have.property("enabled",true);
var nodeConfig = typeRegistry.getNodeConfigs();
nodeConfig.length.should.be.greaterThan(0);
typeRegistry.disableNode(list[0].types[0]).then(function(info) {;
info.should.have.property("id",list[0].id);
info.should.have.property("types",list[0].types);
info.should.have.property("enabled",false);
var list2 = typeRegistry.getNodeList();
list2.should.be.an.Array.and.have.lengthOf(1);
list2[0].should.have.property("enabled",false);
typeRegistry.getNodeConfigs().length.should.equal(0);
typeRegistry.enableNode(list[0].types[0]).then(function(info2) {
info2.should.have.property("id",list[0].id);
info2.should.have.property("types",list[0].types);
info2.should.have.property("enabled",true);
var list3 = typeRegistry.getNodeList();
list3.should.be.an.Array.and.have.lengthOf(1);
list3[0].should.have.property("enabled",true);
var nodeConfig2 = typeRegistry.getNodeConfigs();
nodeConfig2.should.eql(nodeConfig);
done();
});
});
}).catch(function(e) {
done(e);
});
});
it('fails to enable/disable non-existent nodes', function(done) {
typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function() {
var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty;
/*jshint immed: false */
(function() {
typeRegistry.disableNode("123");
}).should.throw();
/*jshint immed: false */
(function() {
typeRegistry.enableNode("123");
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
it("handles unavailable settings", function(done) {
typeRegistry.init(settings);
/*jshint immed: false */
(function() {
typeRegistry.enableNode("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.disableNode("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.addModule("123");
}).should.throw("Settings unavailable");
/*jshint immed: false */
(function() {
typeRegistry.removeModule("123");
}).should.throw("Settings unavailable");
done();
});
});

View File

@ -79,7 +79,7 @@ describe('nodes/registry/installer', function() {
});
});
it("rejects when non-existant path is provided", function(done) {
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","TestNodeModule","node_modules","NonExistant"));
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","NonExistant"));
installer.installModule(resourcesDir).then(function() {
done(new Error("Unexpected success"));
}).otherwise(function(err) {
@ -92,7 +92,7 @@ describe('nodes/registry/installer', function() {
var addModule = sinon.stub(registry,"addModule",function(md) {
return when.resolve(nodeInfo);
});
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","TestNodeModule","node_modules","TestNodeModule"));
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","TestNodeModule"));
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
cb(null,"","");
});

View File

@ -0,0 +1,518 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var when = require("when");
var sinon = require("sinon");
var path = require("path");
var fs = require("fs");
var loader = require("../../../../../red/runtime/nodes/registry/loader");
var localfilesystem = require("../../../../../red/runtime/nodes/registry/localfilesystem");
var registry = require("../../../../../red/runtime/nodes/registry/registry");
var nodes = require("../../../../../red/runtime/nodes/registry");
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local"));
describe("red/nodes/registry/loader",function() {
var stubs = [];
before(function() {
sinon.stub(localfilesystem,"init");
});
after(function() {
localfilesystem.init.restore();
});
afterEach(function() {
while(stubs.length) {
stubs.pop().restore();
}
})
describe("#init",function() {
it("init",function() {
loader.init({});
localfilesystem.init.called.should.be.true;
});
});
describe("#load",function() {
it("load empty set without settings available", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
loader.init({available:function(){return false;}});
loader.load("foo",true).then(function() {
localfilesystem.getNodeFiles.called.should.be.true;
localfilesystem.getNodeFiles.lastCall.args[0].should.eql('foo');
localfilesystem.getNodeFiles.lastCall.args[1].should.be.true;
registry.saveNodeList.called.should.be.false;
done();
})
});
it("load empty set with settings available triggers registery save", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
loader.init({available:function(){return true;}});
loader.load("foo",true).then(function() {
registry.saveNodeList.called.should.be.true;
done();
}).otherwise(function(err) {
done(err);
})
});
it("load core node files scanned by lfs - single node single file", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"nodes": {
"TestNode1": {
"file": path.join(resourcesDir,"TestNode1","TestNode1.js"),
"module": "node-red",
"name": "TestNode1"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"node-red/TestNode1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"node-red");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',undefined);
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(1);
registry.addNodeSet.lastCall.args[1].types[0].should.eql('test-node-1');
registry.addNodeSet.lastCall.args[1].should.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.have.a.property('namespace','node-red');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
nodes.registerType.calledOnce.should.be.true;
nodes.registerType.lastCall.args[0].should.eql('test-node-1');
done();
}).otherwise(function(err) {
done(err);
});
});
it("load core node files scanned by lfs - multiple nodes single file", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"nodes": {
"MultipleNodes1": {
"file": path.join(resourcesDir,"MultipleNodes1","MultipleNodes1.js"),
"module": "node-red",
"name": "MultipleNodes1"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/MultipleNodes1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"node-red/MultipleNodes1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"node-red");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',undefined);
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(2);
registry.addNodeSet.lastCall.args[1].types[0].should.eql('test-node-multiple-1a');
registry.addNodeSet.lastCall.args[1].types[1].should.eql('test-node-multiple-1b');
registry.addNodeSet.lastCall.args[1].should.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.have.a.property('namespace','node-red');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
nodes.registerType.calledTwice.should.be.true;
nodes.registerType.firstCall.args[0].should.eql('test-node-multiple-1a');
nodes.registerType.secondCall.args[0].should.eql('test-node-multiple-1b');
done();
}).otherwise(function(err) {
done(err);
});
});
it("load core node files scanned by lfs - node with promise", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"nodes": {
"TestNode2": {
"file": path.join(resourcesDir,"TestNode2","TestNode2.js"),
"module": "node-red",
"name": "TestNode2"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode2");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"node-red/TestNode2");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"node-red");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',undefined);
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(1);
registry.addNodeSet.lastCall.args[1].types[0].should.eql('test-node-2');
registry.addNodeSet.lastCall.args[1].should.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.have.a.property('namespace','node-red');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
nodes.registerType.calledOnce.should.be.true;
nodes.registerType.lastCall.args[0].should.eql('test-node-2');
done();
}).otherwise(function(err) {
done(err);
});
});
it("load core node files scanned by lfs - node with rejecting promise", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"nodes": {
"TestNode3": {
"file": path.join(resourcesDir,"TestNode3","TestNode3.js"),
"module": "node-red",
"name": "TestNode3"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/TestNode3");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"node-red/TestNode3");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"node-red");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',false);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',undefined);
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(1);
registry.addNodeSet.lastCall.args[1].types[0].should.eql('test-node-3');
registry.addNodeSet.lastCall.args[1].should.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.have.a.property('namespace','node-red');
registry.addNodeSet.lastCall.args[1].should.have.a.property('err','fail');
nodes.registerType.calledOnce.should.be.false;
done();
}).otherwise(function(err) {
done(err);
});
});
it("load core node files scanned by lfs - missing file", function(done) {
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
var result = {};
result["node-red"] = {
"name": "node-red",
"nodes": {
"DoesNotExist": {
"file": path.join(resourcesDir,"doesnotexist"),
"module": "node-red",
"name": "DoesNotExist"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.load().then(function(result) {
registry.addNodeSet.called.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("node-red/DoesNotExist");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"node-red/DoesNotExist");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"node-red");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',false);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',undefined);
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(0);
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('namespace','node-red');
registry.addNodeSet.lastCall.args[1].should.have.a.property('err');
nodes.registerType.calledOnce.should.be.false;
done();
}).otherwise(function(err) {
done(err);
});
});
});
describe("#addModule",function() {
it("throws error if settings unavailable", function() {
loader.init({available:function(){return false;}});
/*jshint immed: false */
(function(){
loader.addModule("test-module");
}).should.throw("Settings unavailable");
});
it("returns rejected error if module already loaded", function(done) {
stubs.push(sinon.stub(registry,"getModuleInfo",function(){return{}}));
loader.init({available:function(){return true;}});
loader.addModule("test-module").otherwise(function(err) {
err.code.should.eql("module_already_loaded");
done();
});
});
it("returns rejected error if module not found", function(done) {
stubs.push(sinon.stub(registry,"getModuleInfo",function(){return null}));
stubs.push(sinon.stub(localfilesystem,"getModuleFiles",function() {
throw new Error("failure");
}));
loader.init({available:function(){return true;}});
loader.addModule("test-module").otherwise(function(err) {
err.message.should.eql("failure");
done();
});
});
it("loads module by name", function(done) {
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(registry,"getModuleInfo",function(){ return null; }));
stubs.push(sinon.stub(localfilesystem,"getModuleFiles", function(){
var result = {};
result["TestNodeModule"] = {
"name": "TestNodeModule",
"version": "1.2.3",
"nodes": {
"TestNode1": {
"file": path.join(resourcesDir,"TestNodeModule","node_modules","TestNodeModule","TestNodeModule.js"),
"module": "TestNodeModule",
"name": "TestNode1",
"version": "1.2.3"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.addModule("TestNodeModule").then(function(result) {
result.should.eql("a node list");
registry.addNodeSet.calledOnce.should.be.true;
registry.addNodeSet.lastCall.args[0].should.eql("TestNodeModule/TestNode1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('id',"TestNodeModule/TestNode1");
registry.addNodeSet.lastCall.args[1].should.have.a.property('module',"TestNodeModule");
registry.addNodeSet.lastCall.args[1].should.have.a.property('enabled',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('loaded',true);
registry.addNodeSet.lastCall.args[1].should.have.a.property('version',"1.2.3");
registry.addNodeSet.lastCall.args[1].should.have.a.property('types');
registry.addNodeSet.lastCall.args[1].types.should.have.a.length(1);
registry.addNodeSet.lastCall.args[1].types[0].should.eql('test-node-mod-1');
registry.addNodeSet.lastCall.args[1].should.have.a.property('config');
registry.addNodeSet.lastCall.args[1].should.have.a.property('help');
registry.addNodeSet.lastCall.args[1].should.have.a.property('namespace','TestNodeModule');
registry.addNodeSet.lastCall.args[1].should.not.have.a.property('err');
nodes.registerType.calledOnce.should.be.true;
done();
}).otherwise(function(err) {
done(err);
});
});
it("skips module that fails version check", function(done) {
// This module isn't already loaded
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
stubs.push(sinon.stub(registry,"getModuleInfo",function(){ return null; }));
stubs.push(sinon.stub(localfilesystem,"getModuleFiles", function(){
var result = {};
result["TestNodeModule"] = {
"name": "TestNodeModule",
"version": "1.2.3",
"redVersion":"999.0.0",
"nodes": {
"TestNode1": {
"file": path.join(resourcesDir,"TestNodeModule","node_modules","TestNodeModule","TestNodeModule.js"),
"module": "TestNodeModule",
"name": "TestNode1",
"version": "1.2.3"
}
}
};
return result;
}));
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
stubs.push(sinon.stub(registry,"addNodeSet", function(){ return }));
stubs.push(sinon.stub(nodes,"registerType"));
loader.init({available:function(){return true;}});
loader.addModule("TestNodeModule").then(function(result) {
result.should.eql("a node list");
registry.addNodeSet.called.should.be.false;
nodes.registerType.called.should.be.false;
done();
}).otherwise(function(err) {
done(err);
});
});
it.skip('registers a message catalog');
});
describe("#loadNodeSet",function() {
it("no-ops the load if node is not enabled", function(done) {
stubs.push(sinon.stub(nodes,"registerType"));
loader.loadNodeSet({
"file": path.join(resourcesDir,"TestNode1","TestNode1.js"),
"module": "node-red",
"name": "TestNode1",
"enabled": false
}).then(function(node) {
node.enabled.should.be.false;
nodes.registerType.called.should.be.false;
done();
}).otherwise(function(err) {
done(err);
});
});
it("handles node that errors on require", function(done) {
stubs.push(sinon.stub(nodes,"registerType"));
loader.loadNodeSet({
"file": path.join(resourcesDir,"TestNode4","TestNode4.js"),
"module": "node-red",
"name": "TestNode4",
"enabled": true
}).then(function(node) {
node.enabled.should.be.true;
nodes.registerType.called.should.be.false;
node.should.have.property('err');
node.err.message.should.eql("fail to require");
done();
}).otherwise(function(err) {
done(err);
});
});
});
describe("#getNodeHelp",function() {
it("returns preloaded help", function() {
loader.getNodeHelp({
help:{
en:"foo"
}
},"en").should.eql("foo");
});
it("loads help, caching result", function() {
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
return 'bar';
}))
var node = {
template: "/tmp/node/directory/file.html",
help:{
en:"foo"
}
};
loader.getNodeHelp(node,"fr").should.eql("bar");
node.help['fr'].should.eql("bar");
fs.readFileSync.calledOnce.should.be.true;
fs.readFileSync.lastCall.args[0].should.eql("/tmp/node/directory/locales/fr/file.html");
loader.getNodeHelp(node,"fr").should.eql("bar");
fs.readFileSync.calledOnce.should.be.true;
});
it("loads help, defaulting to en-US content", function() {
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
throw new Error("not found");
}))
var node = {
template: "/tmp/node/directory/file.html",
help:{}
};
node.help['en-US'] = 'foo';
loader.getNodeHelp(node,"fr").should.eql("foo");
node.help['fr'].should.eql("foo");
fs.readFileSync.calledOnce.should.be.true;
fs.readFileSync.lastCall.args[0].should.eql("/tmp/node/directory/locales/fr/file.html");
loader.getNodeHelp(node,"fr").should.eql("foo");
fs.readFileSync.calledOnce.should.be.true;
});
});
});

View File

@ -0,0 +1,173 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var should = require("should");
var when = require("when");
var sinon = require("sinon");
var path = require("path");
var localfilesystem = require("../../../../../red/runtime/nodes/registry/localfilesystem");
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local"));
var userDir = path.resolve(path.join(__dirname,"..","resources","userDir"));
var moduleDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule"));
describe("red/nodes/registry/localfilesystem",function() {
var stubs = [];
afterEach(function() {
while(stubs.length) {
stubs.pop().restore();
}
})
function checkNodes(nodes,shouldHaveNodes,shouldNotHaveNodes,module) {
for (var i=0;i<shouldHaveNodes.length;i++) {
nodes.should.have.a.property(shouldHaveNodes[i]);
nodes[shouldHaveNodes[i]].should.have.a.property('file');
nodes[shouldHaveNodes[i]].should.have.a.property('module',module||'node-red');
nodes[shouldHaveNodes[i]].should.have.a.property('name',shouldHaveNodes[i]);
}
for (i=0;i<shouldNotHaveNodes.length;i++) {
nodes.should.not.have.a.property(shouldNotHaveNodes[i]);
}
}
describe("#getNodeFiles",function() {
it("Finds all the node files in the resources tree",function(done) {
localfilesystem.init({});
var nodeList = localfilesystem.getNodeFiles(resourcesDir,true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
var nodes = nm.nodes;
checkNodes(nm.nodes,['TestNode1','MultipleNodes1','NestedNode','TestNode2','TestNode3','TestNode4'],['TestNodeModule']);
done();
});
it("Excludes node files from settings",function(done) {
localfilesystem.init({nodesExcludes:['TestNode1.js']});
var nodeList = localfilesystem.getNodeFiles(resourcesDir,true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['MultipleNodes1','NestedNode','TestNode2','TestNode3','TestNode4'],['TestNode1','TestNodeModule']);
done();
});
it("Finds nodes in userDir/nodes",function(done) {
localfilesystem.init({userDir:userDir});
var nodeList = localfilesystem.getNodeFiles(__dirname,true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
done();
});
it("Finds nodes in settings.nodesDir (string)",function(done) {
localfilesystem.init({nodesDir:userDir});
var nodeList = localfilesystem.getNodeFiles(__dirname,true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
done();
});
it("Finds nodes in settings.nodesDir (array)",function(done) {
localfilesystem.init({nodesDir:[userDir]});
var nodeList = localfilesystem.getNodeFiles(__dirname,true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
done();
});
it("Finds nodes module path",function(done) {
var _join = path.join;
stubs.push(sinon.stub(path,"join",function() {
if (arguments[0] == resourcesDir) {
// This stops the module tree scan from going any higher
// up the tree than resourcesDir.
return arguments[0];
}
return _join.apply(null,arguments);
}));
localfilesystem.init({});
var nodeList = localfilesystem.getNodeFiles(moduleDir,false);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
nm.should.have.a.property('name','node-red');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,[],['TestNode1']);
nm = nodeList['TestNodeModule'];
nm.should.have.a.property('name','TestNodeModule');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['TestNodeMod1','TestNodeMod2'],[],'TestNodeModule');
nm = nodeList['VersionMismatchModule'];
nm.should.have.a.property('name','VersionMismatchModule');
nm.should.have.a.property("nodes");
checkNodes(nm.nodes,['VersionMismatchMod1','VersionMismatchMod2'],[],'VersionMismatchModule');
done();
});
});
describe("#getModuleFiles",function() {
it("gets a nodes module files",function(done) {
var _join = path.join;
stubs.push(sinon.stub(path,"join",function() {
if (arguments[0] == resourcesDir) {
// This stops the module tree scan from going any higher
// up the tree than resourcesDir.
return arguments[0];
}
return _join.apply(null,arguments);
}));
localfilesystem.init({},moduleDir,true);
var nodeModule = localfilesystem.getModuleFiles('TestNodeModule');
nodeModule.should.have.a.property('TestNodeModule');
nodeModule['TestNodeModule'].should.have.a.property('name','TestNodeModule');
nodeModule['TestNodeModule'].should.have.a.property('version','0.0.1');
nodeModule['TestNodeModule'].should.have.a.property('nodes');
checkNodes(nodeModule['TestNodeModule'].nodes,['TestNodeMod1','TestNodeMod2'],[],'TestNodeModule');
nodeModule = localfilesystem.getModuleFiles('VersionMismatchModule');
nodeModule.should.have.a.property('VersionMismatchModule');
nodeModule['VersionMismatchModule'].should.have.a.property('redVersion','100.0.0');
done();
});
it("throws an error if a node isn't found",function(done) {
var _join = path.join;
stubs.push(sinon.stub(path,"join",function() {
if (arguments[0] == resourcesDir) {
// This stops the module tree scan from going any higher
// up the tree than resourcesDir.
return arguments[0];
}
return _join.apply(null,arguments);
}));
localfilesystem.init({},moduleDir,true);
/*jshint immed: false */
(function(){
localfilesystem.getModuleFiles('WontExistModule');
}).should.throw();
done();
});
});
});

View File

@ -20,19 +20,21 @@ var sinon = require("sinon");
var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry");
var events = require("../../../../../red/runtime/events");
describe("red/nodes/registry/registry",function() {
beforeEach(function() {
afterEach(function() {
typeRegistry.clear();
});
function stubSettings(s,available,initialConfig) {
s.available = function() {return available;};
s.set = function(s,v) { return when.resolve();};
s.set = sinon.spy(function(s,v) { return when.resolve();});
s.get = function(s) { return initialConfig;};
return s;
}
var settings = stubSettings({},false,null);
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion",nodes:{"node":{id:"node-red/testName",name:"test",types:["a","b"],enabled:true}}}});
@ -42,15 +44,17 @@ describe("red/nodes/registry/registry",function() {
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"]
};
var testNodeSet2 = {
id: "test-module/test-name-2",
module: "test-module",
name: "test-name-2",
enabled: true,
loaded: false,
config: "configB",
types: [ "test-c","test-d"]
};
var testNodeSet2WithError = {
@ -60,19 +64,20 @@ describe("red/nodes/registry/registry",function() {
enabled: true,
loaded: false,
err: "I have an error",
config: "configC",
types: [ "test-c","test-d"]
};
describe('#init', function() {
it('loads initial config', function(done) {
typeRegistry.init(settingsWithStorageAndInitialConfig);
typeRegistry.getNodeList().should.have.lengthOf(1);
done();
});
it('migrates legacy format', function(done) {
var legacySettings = {
available: function() { return true; },
@ -109,18 +114,18 @@ describe("red/nodes/registry/registry",function() {
done();
});
});
describe('#addNodeSet', function() {
it('adds a node set for an unknown module', function() {
typeRegistry.init(settings);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.getNodeList().should.have.lengthOf(1);
var moduleList = typeRegistry.getModuleList();
moduleList.should.have.a.property("test-module");
@ -128,8 +133,9 @@ describe("red/nodes/registry/registry",function() {
moduleList["test-module"].should.have.a.property("version","0.0.1");
moduleList["test-module"].should.have.a.property("nodes");
moduleList["test-module"].nodes.should.have.a.property("test-name");
moduleList["test-module"].nodes["test-name"].should.eql({
config: 'configA',
id: 'test-module/test-name',
module: 'test-module',
name: 'test-name',
@ -137,17 +143,17 @@ describe("red/nodes/registry/registry",function() {
loaded: false,
types: [ 'test-a', 'test-b' ]
});
});
it('adds a node set to an existing module', function() {
typeRegistry.init(settings);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.getNodeList().should.have.lengthOf(1);
var moduleList = typeRegistry.getModuleList();
Object.keys(moduleList).should.have.a.lengthOf(1);
@ -155,13 +161,13 @@ describe("red/nodes/registry/registry",function() {
moduleList["test-module"].should.have.a.property("name","test-module");
moduleList["test-module"].should.have.a.property("version","0.0.1");
moduleList["test-module"].should.have.a.property("nodes");
Object.keys(moduleList["test-module"].nodes).should.have.a.lengthOf(1);
moduleList["test-module"].nodes.should.have.a.property("test-name");
typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2);
typeRegistry.getNodeList().should.have.lengthOf(2);
moduleList = typeRegistry.getModuleList();
Object.keys(moduleList).should.have.a.lengthOf(1);
@ -169,24 +175,24 @@ describe("red/nodes/registry/registry",function() {
moduleList["test-module"].nodes.should.have.a.property("test-name");
moduleList["test-module"].nodes.should.have.a.property("test-name-2");
});
it('doesnt add node set types if node set has an error', function() {
typeRegistry.init(settings);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.getTypeId("test-a").should.eql("test-module/test-name");
should.not.exist(typeRegistry.getTypeId("test-c"));
typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2WithError, "0.0.1");
should.not.exist(typeRegistry.getTypeId("test-c"));
});
});
});
describe("#enableNodeSet", function() {
it('throws error if settings unavailable', function() {
typeRegistry.init(settings);
@ -195,7 +201,7 @@ describe("red/nodes/registry/registry",function() {
typeRegistry.enableNodeSet("test-module/test-name");
}).should.throw("Settings unavailable");
});
it('throws error if module unknown', function() {
typeRegistry.init(settingsWithStorageAndInitialConfig);
/*jshint immed: false */
@ -203,9 +209,28 @@ describe("red/nodes/registry/registry",function() {
typeRegistry.enableNodeSet("test-module/unknown");
}).should.throw("Unrecognised id: test-module/unknown");
});
it.skip('enables the node',function(){})
});
describe("#disableNodeSet", function() {
it('throws error if settings unavailable', function() {
typeRegistry.init(settings);
/*jshint immed: false */
(function(){
typeRegistry.disableNodeSet("test-module/test-name");
}).should.throw("Settings unavailable");
});
it('throws error if module unknown', function() {
typeRegistry.init(settingsWithStorageAndInitialConfig);
/*jshint immed: false */
(function(){
typeRegistry.disableNodeSet("test-module/unknown");
}).should.throw("Unrecognised id: test-module/unknown");
});
it.skip('disables the node',function(){})
});
describe('#getNodeConfig', function() {
it('returns nothing for an unregistered type config', function(done) {
typeRegistry.init(settings);
@ -214,5 +239,218 @@ describe("red/nodes/registry/registry",function() {
done();
});
});
describe('#saveNodeList',function() {
it('rejects when settings unavailable',function(done) {
typeRegistry.init(stubSettings({},false,{}));
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.saveNodeList().otherwise(function(err) {
done();
});
});
it('saves the list',function(done) {
var s = stubSettings({},true,{});
typeRegistry.init(s);
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2WithError, "0.0.1");
typeRegistry.saveNodeList().then(function() {
s.set.called.should.be.true;
s.set.lastCall.args[0].should.eql('nodes');
var nodes = s.set.lastCall.args[1];
nodes.should.have.property('test-module');
for (var n in nodes['test-module'].nodes) {
var nn = nodes['test-module'].nodes[n];
nn.should.not.have.property('err');
nn.should.not.have.property('id');
}
done();
}).otherwise(function(err) {
done(err);
});
});
});
describe('#removeModule',function() {
it('throws error for unknown module', function() {
var s = stubSettings({},true,{});
typeRegistry.init(s);
/*jshint immed: false */
(function(){
typeRegistry.removeModule("test-module/unknown");
}).should.throw("Unrecognised module: test-module/unknown");
});
it('throws error for unavaiable settings', function() {
var s = stubSettings({},false,{});
typeRegistry.init(s);
/*jshint immed: false */
(function(){
typeRegistry.removeModule("test-module/unknown");
}).should.throw("Settings unavailable");
});
it('removes a known module', function() {
var s = stubSettings({},true,{});
typeRegistry.init(s);
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
var moduleList = typeRegistry.getModuleList();
moduleList.should.have.a.property("test-module");
typeRegistry.getNodeList().should.have.lengthOf(1);
var info = typeRegistry.removeModule('test-module');
moduleList = typeRegistry.getModuleList();
moduleList.should.not.have.a.property("test-module");
typeRegistry.getNodeList().should.have.lengthOf(0);
});
});
describe('#get[All]NodeConfigs', function() {
it('returns node config', function() {
typeRegistry.init(settings,{
getNodeHelp: function(config) { return "HE"+config.name+"LP" }
});
typeRegistry.addNodeSet("test-module/test-name",{
id: "test-module/test-name",
module: "test-module",
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"]
}, "0.0.1");
typeRegistry.getNodeConfig("test-module/test-name").should.eql('configAHEtest-nameLP');
typeRegistry.getAllNodeConfigs().should.eql('configAHEtest-nameLP');
typeRegistry.addNodeSet("test-module/test-name-2",{
id: "test-module/test-name-2",
module: "test-module",
name: "test-name-2",
enabled: true,
loaded: false,
config: "configB",
types: [ "test-a","test-b"]
}, "0.0.1");
typeRegistry.getNodeConfig("test-module/test-name-2").should.eql('configBHEtest-name-2LP');
typeRegistry.getAllNodeConfigs().should.eql('configAHEtest-nameLPconfigBHEtest-name-2LP');
});
});
describe('#getModuleInfo', function() {
it('returns module info', function() {
typeRegistry.init(settings,{});
typeRegistry.addNodeSet("test-module/test-name",{
id: "test-module/test-name",
module: "test-module",
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"],
file: "abc"
}, "0.0.1");
var moduleInfo = typeRegistry.getModuleInfo("test-module");
moduleInfo.should.have.a.property('name','test-module');
moduleInfo.should.have.a.property('version','0.0.1');
moduleInfo.should.have.a.property('nodes');
moduleInfo.nodes.should.have.a.lengthOf(1);
moduleInfo.nodes[0].should.have.a.property('id','test-module/test-name');
moduleInfo.nodes[0].should.not.have.a.property('file');
});
});
describe('#getNodeInfo', function() {
it('returns node info', function() {
typeRegistry.init(settings,{});
typeRegistry.addNodeSet("test-module/test-name",{
id: "test-module/test-name",
module: "test-module",
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"],
file: "abc"
}, "0.0.1");
var nodeSetInfo = typeRegistry.getNodeInfo("test-module/test-name");
nodeSetInfo.should.have.a.property('id',"test-module/test-name");
nodeSetInfo.should.not.have.a.property('config');
nodeSetInfo.should.not.have.a.property('file');
});
});
describe('#getFullNodeInfo', function() {
it('returns node info', function() {
typeRegistry.init(settings,{});
typeRegistry.addNodeSet("test-module/test-name",{
id: "test-module/test-name",
module: "test-module",
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"],
file: "abc"
}, "0.0.1");
var nodeSetInfo = typeRegistry.getFullNodeInfo("test-module/test-name");
nodeSetInfo.should.have.a.property('id',"test-module/test-name");
nodeSetInfo.should.have.a.property('config');
nodeSetInfo.should.have.a.property('file');
});
});
describe('#cleanModuleList', function() {
it.skip("cleans the module list");
});
describe('#getNodeList', function() {
it("returns a filtered list", function() {
typeRegistry.init(settings,{});
typeRegistry.addNodeSet("test-module/test-name",{
id: "test-module/test-name",
module: "test-module",
name: "test-name",
enabled: true,
loaded: false,
config: "configA",
types: [ "test-a","test-b"],
file: "abc"
}, "0.0.1");
typeRegistry.addNodeSet("test-module/test-name-2",{
id: "test-module/test-name-2",
module: "test-module",
name: "test-name-2",
enabled: true,
loaded: false,
config: "configB",
types: [ "test-c","test-d"],
file: "def"
}, "0.0.1");
var filterCallCount = 0;
var filteredList = typeRegistry.getNodeList(function(n) { filterCallCount++; return n.name === 'test-name-2';});
filterCallCount.should.eql(2);
filteredList.should.have.a.lengthOf(1);
filteredList[0].should.have.a.property('id',"test-module/test-name-2");
});
});
describe('#registerNodeConstructor', function() {
beforeEach(function() {
sinon.stub(events,'emit');
});
afterEach(function() {
events.emit.restore();
});
it('registers a node constructor', function() {
typeRegistry.registerNodeConstructor('node-type',{});
events.emit.calledOnce.should.be.true;
events.emit.lastCall.args[0].should.eql('type-registered');
events.emit.lastCall.args[1].should.eql('node-type');
})
it('throws error on duplicate node registration', function() {
typeRegistry.registerNodeConstructor('node-type',{});
events.emit.calledOnce.should.be.true;
events.emit.lastCall.args[0].should.eql('type-registered');
events.emit.lastCall.args[1].should.eql('node-type');
/*jshint immed: false */
(function(){
typeRegistry.registerNodeConstructor('node-type',{});
}).should.throw("node-type already registered");
events.emit.calledOnce.should.be.true;
})
});
});

View File

@ -0,0 +1,3 @@
<script type="text/x-red" data-template-name="test-node-3"></script>
<script type="text/x-red" data-help-name="test-node-3"></script>
<script type="text/javascript">RED.nodes.registerType('test-node-3',{});</script>

View File

@ -0,0 +1 @@
throw new Error("fail to require");

View File

@ -0,0 +1,5 @@
<script type="text/x-red" data-template-name="test-node-5"></script>
<script type="text/x-red" data-help-name="test-node-5"></script>
<script type="text/javascript">RED.nodes.registerType('test-node-5',{});</script>
<style></style>
<p>this should be filtered out</p>

View File

@ -0,0 +1 @@
throw new Error("fail to require");