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

Increase registry test coverage

This commit is contained in:
Nick O'Leary 2015-03-22 22:54:52 +00:00
parent e19b8d35a9
commit 20bdea7ae0
2 changed files with 112 additions and 23 deletions

View File

@ -66,6 +66,7 @@ var registry = (function() {
var moduleList = {}; var moduleList = {};
for (var module in moduleConfigs) { for (var module in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(module)) { if (moduleConfigs.hasOwnProperty(module)) {
if (Object.keys(moduleConfigs[module].nodes).length > 0) { if (Object.keys(moduleConfigs[module].nodes).length > 0) {
if (!moduleList[module]) { if (!moduleList[module]) {
@ -77,6 +78,7 @@ var registry = (function() {
} }
var nodes = moduleConfigs[module].nodes; var nodes = moduleConfigs[module].nodes;
for(var node in nodes) { for(var node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) { if (nodes.hasOwnProperty(node)) {
var config = nodes[node]; var config = nodes[node];
var n = filterNodeInfo(config); var n = filterNodeInfo(config);
@ -109,6 +111,7 @@ var registry = (function() {
// Migrate from the 0.9.1 format of settings // Migrate from the 0.9.1 format of settings
var newConfigs = {}; var newConfigs = {};
for (var id in configs) { for (var id in configs) {
/* istanbul ignore else */
if (configs.hasOwnProperty(id)) { if (configs.hasOwnProperty(id)) {
var nodeConfig = configs[id]; var nodeConfig = configs[id];
var moduleName; var moduleName;
@ -220,6 +223,8 @@ var registry = (function() {
if (nodeTypeToId[typeOrId]) { if (nodeTypeToId[typeOrId]) {
id = nodeTypeToId[typeOrId]; id = nodeTypeToId[typeOrId];
} }
/* istanbul ignore else */
if (id) { if (id) {
var module = moduleConfigs[getModule(id)]; var module = moduleConfigs[getModule(id)];
if (module) { if (module) {
@ -234,9 +239,11 @@ var registry = (function() {
getNodeList: function() { getNodeList: function() {
var list = []; var list = [];
for (var module in moduleConfigs) { for (var module in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(module)) { if (moduleConfigs.hasOwnProperty(module)) {
var nodes = moduleConfigs[module].nodes; var nodes = moduleConfigs[module].nodes;
for (var node in nodes) { for (var node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) { if (nodes.hasOwnProperty(node)) {
list.push(filterNodeInfo(nodes[node])); list.push(filterNodeInfo(nodes[node]));
} }
@ -248,6 +255,7 @@ var registry = (function() {
getModuleList: function() { getModuleList: function() {
var list = []; var list = [];
for (var module in moduleNodes) { for (var module in moduleNodes) {
/* istanbul ignore else */
if (moduleNodes.hasOwnProperty(module)) { if (moduleNodes.hasOwnProperty(module)) {
var nodes = moduleNodes[module]; var nodes = moduleNodes[module];
var m = { var m = {
@ -307,14 +315,14 @@ var registry = (function() {
var config = moduleConfigs[getModule(id)].nodes[getNode(id)]; var config = moduleConfigs[getModule(id)].nodes[getNode(id)];
if (config.enabled && !config.err) { if (config.enabled && !config.err) {
result += config.config; result += config.config;
script += config.script; //script += config.script;
} }
} }
if (script.length > 0) { //if (script.length > 0) {
result += '<script type="text/javascript">'; // result += '<script type="text/javascript">';
result += UglifyJS.minify(script, {fromString: true}).code; // result += UglifyJS.minify(script, {fromString: true}).code;
result += '</script>'; // result += '</script>';
} //}
nodeConfigCache = result; nodeConfigCache = result;
} }
return nodeConfigCache; return nodeConfigCache;
@ -328,9 +336,9 @@ var registry = (function() {
config = config.nodes[getNode(id)]; config = config.nodes[getNode(id)];
if (config) { if (config) {
var result = config.config; var result = config.config;
if (config.script) { //if (config.script) {
result += '<script type="text/javascript">'+config.script+'</script>'; // result += '<script type="text/javascript">'+config.script+'</script>';
} //}
return result; return result;
} else { } else {
return null; return null;
@ -422,12 +430,14 @@ var registry = (function() {
cleanModuleList: function() { cleanModuleList: function() {
var removed = false; var removed = false;
for (var mod in moduleConfigs) { for (var mod in moduleConfigs) {
/* istanbul ignore else */
if (moduleConfigs.hasOwnProperty(mod)) { if (moduleConfigs.hasOwnProperty(mod)) {
var nodes = moduleConfigs[mod].nodes; var nodes = moduleConfigs[mod].nodes;
var node; var node;
if (mod == "node-red") { if (mod == "node-red") {
// For core nodes, look for nodes that are enabled, !loaded and !errored // For core nodes, look for nodes that are enabled, !loaded and !errored
for (node in nodes) { for (node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) { if (nodes.hasOwnProperty(node)) {
var n = nodes[node]; var n = nodes[node];
if (n.enabled && !n.err && !n.loaded) { if (n.enabled && !n.err && !n.loaded) {
@ -439,6 +449,7 @@ var registry = (function() {
} else if (moduleConfigs[mod] && !moduleNodes[mod]) { } else if (moduleConfigs[mod] && !moduleNodes[mod]) {
// For node modules, look for missing ones // For node modules, look for missing ones
for (node in nodes) { for (node in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(node)) { if (nodes.hasOwnProperty(node)) {
registry.removeNode(mod+"/"+node); registry.removeNode(mod+"/"+node);
removed = true; removed = true;
@ -577,6 +588,7 @@ function loadNodesFromModule(moduleDir,pkg) {
var results = []; var results = [];
var iconDirs = []; var iconDirs = [];
for (var n in nodes) { for (var n in nodes) {
/* istanbul ignore else */
if (nodes.hasOwnProperty(n)) { if (nodes.hasOwnProperty(n)) {
var file = path.join(moduleDir,nodes[n]); var file = path.join(moduleDir,nodes[n]);
try { try {
@ -649,8 +661,7 @@ function loadNodeConfig(file,module,name,version) {
node.config = content; node.config = content;
// TODO: parse out the javascript portion of the template // TODO: parse out the javascript portion of the template
node.script = ""; //node.script = "";
for (var i=0;i<node.types.length;i++) { for (var i=0;i<node.types.length;i++) {
if (registry.getTypeId(node.types[i])) { if (registry.getTypeId(node.types[i])) {
node.err = node.types[i]+" already registered"; node.err = node.types[i]+" already registered";

View File

@ -28,18 +28,27 @@ afterEach(function() {
typeRegistry.clear(); typeRegistry.clear();
}); });
describe('NodeRegistry', function() { describe('red/nodes/registry', function() {
var resourcesDir = __dirname+ path.sep + "resources" + path.sep; var resourcesDir = __dirname+ path.sep + "resources" + path.sep;
function stubSettings(s,available) { function stubSettings(s,available,initialConfig) {
s.available = function() {return available;}; s.available = function() {return available;};
s.set = function(s,v) { return when.resolve();}; s.set = function(s,v) { return when.resolve();};
s.get = function(s) { return null;}; s.get = function(s) { return initialConfig;};
return s; return s;
} }
var settings = stubSettings({},false); var settings = stubSettings({},false,null);
var settingsWithStorage = stubSettings({},true); var settingsWithStorage = stubSettings({},true,null);
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion"}});
it('loads initial config', function(done) {
typeRegistry.init(settingsWithStorageAndInitialConfig);
var version = typeRegistry.getModuleVersion("node-red");
should.exist(version);
version.should.eql("testVersion");
done();
});
it('handles nodes that export a function', function(done) { it('handles nodes that export a function', function(done) {
typeRegistry.init(settings); typeRegistry.init(settings);
@ -178,8 +187,7 @@ describe('NodeRegistry', function() {
}); });
}); });
it('rejects a duplicate node type registration', function(done) { it('rejects a duplicate node type registration during load', function(done) {
typeRegistry.init(stubSettings({ typeRegistry.init(stubSettings({
nodesDir:[resourcesDir + "TestNode1",resourcesDir + "DuplicateTestNode"] nodesDir:[resourcesDir + "TestNode1",resourcesDir + "DuplicateTestNode"]
},false)); },false));
@ -199,6 +207,27 @@ describe('NodeRegistry', function() {
}); });
}); });
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);
/*jshint immed: false */
(function(){
typeRegistry.registerType("test-node-1",{});
}).should.throw();
done();
}).catch(function(e) {
done(e);
});
});
it('handles nodesDir as a string', function(done) { it('handles nodesDir as a string', function(done) {
typeRegistry.init(stubSettings({ typeRegistry.init(stubSettings({
@ -374,9 +403,6 @@ describe('NodeRegistry', function() {
var list = typeRegistry.getNodeList(); var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.have.lengthOf(1); list.should.be.an.Array.and.have.lengthOf(1);
var id = list[0].id;
var type = list[0].types[0];
list[0].should.have.property("id","node-red/TestNode1"); list[0].should.have.property("id","node-red/TestNode1");
list[0].should.have.property("name","TestNode1"); list[0].should.have.property("name","TestNode1");
list[0].should.have.property("module","node-red"); list[0].should.have.property("module","node-red");
@ -384,6 +410,10 @@ describe('NodeRegistry', function() {
list[0].should.have.property("enabled",true); list[0].should.have.property("enabled",true);
list[0].should.not.have.property("err"); list[0].should.not.have.property("err");
var id = list[0].id;
var type = list[0].types[0];
var info = typeRegistry.getNodeInfo(id); var info = typeRegistry.getNodeInfo(id);
list[0].should.eql(info); list[0].should.eql(info);
@ -397,6 +427,21 @@ describe('NodeRegistry', function() {
}); });
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);
});
});
it('returns modules list', function(done) { it('returns modules list', function(done) {
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
@ -487,6 +532,9 @@ describe('NodeRegistry', function() {
var module = typeRegistry.getModuleInfo(list[0].name); var module = typeRegistry.getModuleInfo(list[0].name);
module.should.have.property("name", list[0].name); module.should.have.property("name", list[0].name);
module.should.have.property("nodes", nodes); module.should.have.property("nodes", nodes);
should.not.exist(typeRegistry.getModuleInfo("does-not-exist"));
done(); done();
}).catch(function(e) { }).catch(function(e) {
done(e); done(e);
@ -746,6 +794,9 @@ describe('NodeRegistry', function() {
modules[0].should.have.property("name","TestNodeModule"); modules[0].should.have.property("name","TestNodeModule");
modules[0].should.have.property("version","0.0.1"); modules[0].should.have.property("version","0.0.1");
var version = typeRegistry.getModuleVersion("TestNodeModule");
version.should.eql("0.0.1");
done(); done();
}).catch(function(e) { }).catch(function(e) {
done(e); done(e);
@ -879,7 +930,7 @@ describe('NodeRegistry', function() {
}); });
it('fails to remove non-existent module name', function(done) { it('fails to remove non-existent module name', function(done) {
typeRegistry.init(settings); typeRegistry.init(settingsWithStorage);
typeRegistry.load("wontexist",true).then(function(){ typeRegistry.load("wontexist",true).then(function(){
var list = typeRegistry.getNodeList(); var list = typeRegistry.getNodeList();
list.should.be.an.Array.and.be.empty; list.should.be.an.Array.and.be.empty;
@ -887,7 +938,7 @@ describe('NodeRegistry', function() {
/*jshint immed: false */ /*jshint immed: false */
(function() { (function() {
typeRegistry.removeModule("DoesNotExistModule"); typeRegistry.removeModule("DoesNotExistModule");
}).should.throw(); }).should.throw("Unrecognised module: DoesNotExistModule");
done(); done();
@ -1002,4 +1053,31 @@ describe('NodeRegistry', function() {
done(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");
/*jshint immed: false */
(function() {
typeRegistry.addNode("123");
}).should.throw("Settings unavailable");
done();
});
}); });