Move exec and events components to util module

The exec and events components are common components that
are used by both runtime and registry. It makes sense to
move them into the util package.

This also adds some docs to the registry module
This commit is contained in:
Nick O'Leary
2020-12-02 09:25:10 +00:00
parent a1f565f756
commit 6fb96fa3c1
33 changed files with 491 additions and 319 deletions

View File

@@ -25,6 +25,7 @@ var NR_TEST_UTILS = require("nr-test-utils");
var installer = NR_TEST_UTILS.require("@node-red/registry/lib/installer");
var registry = NR_TEST_UTILS.require("@node-red/registry/lib/index");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
const { events, exec, log } = NR_TEST_UTILS.require("@node-red/util");
describe('nodes/registry/installer', function() {
@@ -38,21 +39,15 @@ describe('nodes/registry/installer', function() {
_: function(msg) { return msg }
}
var execResponse;
beforeEach(function() {
installer.init({log:mockLog, settings:{}, events: new EventEmitter(), exec: {
run: function() {
return Promise.resolve("");
}
}});
sinon.stub(exec,"run", () => execResponse || Promise.resolve(""))
installer.init({})
});
function initInstaller(execResult) {
installer.init({log:mockLog, settings:{}, events: new EventEmitter(), exec: {
run: function() {
return execResult;
}
}});
}
afterEach(function() {
execResponse = null;
if (registry.addModule.restore) {
registry.addModule.restore();
}
@@ -72,7 +67,7 @@ describe('nodes/registry/installer', function() {
if (fs.statSync.restore) {
fs.statSync.restore();
}
exec.run.restore();
});
describe("installs module", function() {
@@ -108,7 +103,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.reject(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
installer.installModule("this_wont_exist").catch(function(err) {
err.should.have.property("code",404);
done();
@@ -122,7 +117,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.reject(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
sinon.stub(typeRegistry,"getModuleInfo", function() {
return {
version: "0.1.1"
@@ -163,7 +158,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.reject(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
installer.installModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
}).catch(err => {
@@ -181,7 +176,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.resolve(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
var addModule = sinon.stub(registry,"addModule",function(md) {
return Promise.resolve(nodeInfo);
@@ -226,7 +221,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.resolve(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
installer.installModule(resourcesDir).then(function(info) {
info.should.eql(nodeInfo);
done();
@@ -242,7 +237,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.resolve(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
var addModule = sinon.stub(registry,"addModule",function(md) {
return Promise.resolve(nodeInfo);
@@ -280,7 +275,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.reject(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
installer.uninstallModule("this_wont_exist").then(function() {
done(new Error("Unexpected success"));
@@ -304,7 +299,7 @@ describe('nodes/registry/installer', function() {
}
var p = Promise.resolve(res);
p.catch((err)=>{});
initInstaller(p)
execResponse = p;
sinon.stub(fs,"statSync", function(fn) { return {}; });

View File

@@ -53,7 +53,7 @@ describe("red/nodes/registry/localfilesystem",function() {
}
describe("#getNodeFiles",function() {
it("Finds all the node files in the resources tree",function(done) {
localfilesystem.init({settings:{coreNodesDir:resourcesDir}});
localfilesystem.init({coreNodesDir:resourcesDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -68,7 +68,7 @@ describe("red/nodes/registry/localfilesystem",function() {
done();
});
it("Includes node files from settings",function(done) {
localfilesystem.init({settings:{nodesIncludes:['TestNode1.js'],coreNodesDir:resourcesDir}});
localfilesystem.init({nodesIncludes:['TestNode1.js'],coreNodesDir:resourcesDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -78,7 +78,7 @@ describe("red/nodes/registry/localfilesystem",function() {
done();
});
it("Excludes node files from settings",function(done) {
localfilesystem.init({settings:{nodesExcludes:['TestNode1.js'],coreNodesDir:resourcesDir}});
localfilesystem.init({nodesExcludes:['TestNode1.js'],coreNodesDir:resourcesDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -88,7 +88,7 @@ describe("red/nodes/registry/localfilesystem",function() {
done();
});
it("Finds nodes in userDir/nodes",function(done) {
localfilesystem.init({settings:{userDir:userDir}});
localfilesystem.init({userDir:userDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -99,7 +99,7 @@ describe("red/nodes/registry/localfilesystem",function() {
});
it("Finds nodes in settings.nodesDir (string)",function(done) {
localfilesystem.init({settings:{nodesDir:userDir}});
localfilesystem.init({nodesDir:userDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -110,7 +110,7 @@ describe("red/nodes/registry/localfilesystem",function() {
});
it("Finds nodes in settings.nodesDir (string,relative path)",function(done) {
var relativeUserDir = path.join("test","unit","@node-red","registry","lib","resources","userDir");
localfilesystem.init({settings:{nodesDir:relativeUserDir}});
localfilesystem.init({nodesDir:relativeUserDir});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -120,7 +120,7 @@ describe("red/nodes/registry/localfilesystem",function() {
done();
});
it("Finds nodes in settings.nodesDir (array)",function(done) {
localfilesystem.init({settings:{nodesDir:[userDir]}});
localfilesystem.init({nodesDir:[userDir]});
var nodeList = localfilesystem.getNodeFiles(true);
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -139,7 +139,7 @@ describe("red/nodes/registry/localfilesystem",function() {
}
return _join.apply(null,arguments);
}));
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
localfilesystem.init({coreNodesDir:moduleDir});
var nodeList = localfilesystem.getNodeFiles();
nodeList.should.have.a.property("node-red");
var nm = nodeList['node-red'];
@@ -175,18 +175,7 @@ describe("red/nodes/registry/localfilesystem",function() {
it("scans icon files in the resources tree",function(done) {
var count = 0;
localfilesystem.init({
// events:{emit:function(eventName,dir){
// if (count === 0) {
// eventName.should.equal("node-icon-dir");
// dir.name.should.equal("node-red");
// dir.icons.should.be.an.Array();
// count = 1;
// } else if (count === 1) {
// done();
// }
// }},
settings:{coreNodesDir:resourcesDir}
coreNodesDir: resourcesDir
});
var list = localfilesystem.getNodeFiles(true);
list.should.have.property("node-red");
@@ -201,22 +190,7 @@ describe("red/nodes/registry/localfilesystem",function() {
it("scans icons dir in library",function(done) {
var count = 0;
localfilesystem.init({
//
// events:{emit:function(eventName,dir){
// eventName.should.equal("node-icon-dir");
// if (count === 0) {
// dir.name.should.equal("node-red");
// dir.icons.should.be.an.Array();
// count = 1;
// } else if (count === 1) {
// dir.name.should.equal("Library");
// dir.icons.should.be.an.Array();
// dir.icons.length.should.equal(1);
// dir.icons[0].should.be.equal("test_icon.png");
// done();
// }
// }},
settings:{userDir:userDir}
userDir: userDir
});
var list = localfilesystem.getNodeFiles(true);
list.should.have.property("node-red");
@@ -240,7 +214,7 @@ describe("red/nodes/registry/localfilesystem",function() {
}
return _join.apply(null,arguments);
}));
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
localfilesystem.init({coreNodesDir:moduleDir});
var nodeModule = localfilesystem.getModuleFiles('TestNodeModule');
nodeModule.should.have.a.property('TestNodeModule');
nodeModule['TestNodeModule'].should.have.a.property('name','TestNodeModule');
@@ -266,7 +240,7 @@ describe("red/nodes/registry/localfilesystem",function() {
}
return _join.apply(null,arguments);
}));
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
localfilesystem.init({coreNodesDir:moduleDir});
/*jshint immed: false */
(function(){
localfilesystem.getModuleFiles('WontExistModule');
@@ -286,14 +260,7 @@ describe("red/nodes/registry/localfilesystem",function() {
return _join.apply(null,arguments);
}));
localfilesystem.init({
// events:{emit:function(eventName,dir){
// eventName.should.equal("node-icon-dir");
// dir.name.should.equal("TestNodeModule");
// dir.icons.should.be.an.Array();
// done();
// }},
settings:{coreNodesDir:moduleDir}
coreNodesDir: moduleDir
});
var nodeModule = localfilesystem.getModuleFiles('TestNodeModule');
nodeModule.should.have.property("TestNodeModule");

View File

@@ -21,9 +21,7 @@ var path = require("path");
var NR_TEST_UTILS = require("nr-test-utils");
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
var EventEmitter = require('events');
var events = new EventEmitter();
const { events } = NR_TEST_UTILS.require("@node-red/util");
describe("red/nodes/registry/registry",function() {
@@ -84,7 +82,7 @@ describe("red/nodes/registry/registry",function() {
describe('#init/load', function() {
it('loads initial config', function(done) {
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
typeRegistry.init(settingsWithStorageAndInitialConfig,null);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.load();
typeRegistry.getNodeList().should.have.lengthOf(1);
@@ -121,7 +119,7 @@ describe("red/nodes/registry/registry",function() {
}}
};
var expected = JSON.parse('{"node-red":{"name":"node-red","nodes":{"sentiment":{"name":"sentiment","types":["sentiment"],"enabled":true,"module":"node-red"},"inject":{"name":"inject","types":["inject"],"enabled":true,"module":"node-red"}}},"testModule":{"name":"testModule","nodes":{"a-module.js":{"name":"a-module.js","types":["example"],"enabled":true,"module":"testModule"}}}}');
typeRegistry.init(legacySettings,null,events);
typeRegistry.init(legacySettings,null);
typeRegistry.load();
legacySettings.set.calledOnce.should.be.true();
legacySettings.set.args[0][1].should.eql(expected);
@@ -133,7 +131,7 @@ describe("red/nodes/registry/registry",function() {
describe.skip('#addNodeSet', function() {
it('adds a node set for an unknown module', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
@@ -162,7 +160,7 @@ describe("red/nodes/registry/registry",function() {
it('adds a node set to an existing module', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
@@ -191,7 +189,7 @@ describe("red/nodes/registry/registry",function() {
});
it('doesnt add node set types if node set has an error', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
@@ -207,7 +205,7 @@ describe("red/nodes/registry/registry",function() {
});
it('doesnt add node set if type already exists', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
@@ -241,7 +239,7 @@ describe("red/nodes/registry/registry",function() {
describe("#enableNodeSet", function() {
it('throws error if settings unavailable', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
/*jshint immed: false */
(function(){
typeRegistry.enableNodeSet("test-module/test-name");
@@ -249,7 +247,7 @@ describe("red/nodes/registry/registry",function() {
});
it('throws error if module unknown', function() {
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
typeRegistry.init(settingsWithStorageAndInitialConfig,null);
/*jshint immed: false */
(function(){
typeRegistry.enableNodeSet("test-module/unknown");
@@ -260,7 +258,7 @@ describe("red/nodes/registry/registry",function() {
});
describe("#disableNodeSet", function() {
it('throws error if settings unavailable', function() {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
/*jshint immed: false */
(function(){
typeRegistry.disableNodeSet("test-module/test-name");
@@ -268,7 +266,7 @@ describe("red/nodes/registry/registry",function() {
});
it('throws error if module unknown', function() {
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
typeRegistry.init(settingsWithStorageAndInitialConfig,null);
/*jshint immed: false */
(function(){
typeRegistry.disableNodeSet("test-module/unknown");
@@ -279,7 +277,7 @@ describe("red/nodes/registry/registry",function() {
describe('#getNodeConfig', function() {
it('returns nothing for an unregistered type config', function(done) {
typeRegistry.init(settings,null,events);
typeRegistry.init(settings,null);
var config = typeRegistry.getNodeConfig("imaginary-shark");
(config === null).should.be.true();
done();
@@ -288,7 +286,7 @@ describe("red/nodes/registry/registry",function() {
describe('#saveNodeList',function() {
it('rejects when settings unavailable',function(done) {
typeRegistry.init(stubSettings({},false,{}),null,events);
typeRegistry.init(stubSettings({},false,{}),null);
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {"test-name":{module:"test-module",name:"test-name",types:[]}}});
typeRegistry.saveNodeList().catch(function(err) {
done();
@@ -296,7 +294,7 @@ describe("red/nodes/registry/registry",function() {
});
it('saves the list',function(done) {
var s = stubSettings({},true,{});
typeRegistry.init(s,null,events);
typeRegistry.init(s,null);
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":testNodeSet1,
@@ -325,7 +323,7 @@ describe("red/nodes/registry/registry",function() {
describe('#removeModule',function() {
it('throws error for unknown module', function() {
var s = stubSettings({},true,{});
typeRegistry.init(s,null,events);
typeRegistry.init(s,null);
/*jshint immed: false */
(function(){
typeRegistry.removeModule("test-module/unknown");
@@ -333,7 +331,7 @@ describe("red/nodes/registry/registry",function() {
});
it('throws error for unavaiable settings', function() {
var s = stubSettings({},false,{});
typeRegistry.init(s,null,events);
typeRegistry.init(s,null);
/*jshint immed: false */
(function(){
typeRegistry.removeModule("test-module/unknown");
@@ -341,7 +339,7 @@ describe("red/nodes/registry/registry",function() {
});
it('removes a known module', function() {
var s = stubSettings({},true,{});
typeRegistry.init(s,null,events);
typeRegistry.init(s,null);
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":testNodeSet1
}});
@@ -360,7 +358,7 @@ describe("red/nodes/registry/registry",function() {
it('returns node config', function() {
typeRegistry.init(settings,{
getNodeHelp: function(config) { return "HE"+config.name+"LP" }
},events);
});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
@@ -389,7 +387,7 @@ describe("red/nodes/registry/registry",function() {
});
describe('#getModuleInfo', function() {
it('returns module info', function() {
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",
@@ -413,7 +411,7 @@ describe("red/nodes/registry/registry",function() {
});
describe('#getNodeInfo', function() {
it('returns node info', function() {
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",
@@ -434,7 +432,7 @@ describe("red/nodes/registry/registry",function() {
});
describe('#getFullNodeInfo', function() {
it('returns node info', function() {
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",
@@ -459,7 +457,7 @@ describe("red/nodes/registry/registry",function() {
});
describe('#getNodeList', function() {
it("returns a filtered list", function() {
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",
@@ -526,7 +524,7 @@ describe("red/nodes/registry/registry",function() {
it('returns a registered icon' , function() {
var testIcon = path.resolve(__dirname+'/resources/userDir/lib/icons/');
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",
@@ -558,7 +556,7 @@ describe("red/nodes/registry/registry",function() {
it('returns an icon list of registered node module', function() {
var testIcon = path.resolve(__dirname+'/resources/userDir/lib/icons/');
typeRegistry.init(settings,{},events);
typeRegistry.init(settings,{});
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
"test-name":{
id: "test-module/test-name",