2015-11-09 12:29:48 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2014, 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 sinon = require("sinon");
|
|
|
|
var when = require("when");
|
2015-11-09 17:52:14 +01:00
|
|
|
var path = require("path");
|
2015-11-14 21:27:04 +01:00
|
|
|
var fs = require('fs');
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
var child_process = require('child_process');
|
2015-11-12 08:56:23 +01:00
|
|
|
var installer = require("../../../../../red/runtime/nodes/registry/installer");
|
|
|
|
var registry = require("../../../../../red/runtime/nodes/registry/index");
|
|
|
|
var typeRegistry = require("../../../../../red/runtime/nodes/registry/registry");
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
describe('nodes/registry/installer', function() {
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
installer.init({});
|
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
afterEach(function() {
|
|
|
|
if (child_process.execFile.restore) {
|
|
|
|
child_process.execFile.restore();
|
|
|
|
}
|
2015-11-14 21:27:04 +01:00
|
|
|
if (registry.addModule.restore) {
|
|
|
|
registry.addModule.restore();
|
|
|
|
}
|
|
|
|
if (registry.removeModule.restore) {
|
|
|
|
registry.removeModule.restore();
|
|
|
|
}
|
|
|
|
if (typeRegistry.removeModule.restore) {
|
|
|
|
typeRegistry.removeModule.restore();
|
|
|
|
}
|
|
|
|
if (registry.getModuleInfo.restore) {
|
|
|
|
registry.getModuleInfo.restore();
|
|
|
|
}
|
|
|
|
|
2015-11-16 12:31:55 +01:00
|
|
|
if (require('fs').statSync.restore) {
|
|
|
|
require('fs').statSync.restore();
|
2015-11-14 21:27:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
describe("installs module", function() {
|
|
|
|
it("rejects when npm returns a 404", function(done) {
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-09 12:29:48 +01:00
|
|
|
cb(new Error(),""," 404 this_wont_exist");
|
|
|
|
});
|
|
|
|
|
|
|
|
installer.installModule("this_wont_exist").otherwise(function(err) {
|
|
|
|
err.code.should.be.eql(404);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("rejects with generic error", function(done) {
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-09 12:29:48 +01:00
|
|
|
cb(new Error("test_error"),"","");
|
|
|
|
});
|
|
|
|
|
|
|
|
installer.installModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-09 12:29:48 +01:00
|
|
|
cb(null,"","");
|
|
|
|
});
|
|
|
|
var addModule = sinon.stub(registry,"addModule",function(md) {
|
|
|
|
return when.resolve(nodeInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
installer.installModule("this_wont_exist").then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
// commsMessages.should.have.length(1);
|
|
|
|
// commsMessages[0].topic.should.equal("node/added");
|
|
|
|
// commsMessages[0].msg.should.eql(nodeInfo.nodes);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
it("rejects when non-existant path is provided", function(done) {
|
2015-11-14 00:16:47 +01:00
|
|
|
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","NonExistant"));
|
2015-11-09 17:52:14 +01:00
|
|
|
installer.installModule(resourcesDir).then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
err.code.should.eql(404);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when path is valid node-red module", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
|
|
|
var addModule = sinon.stub(registry,"addModule",function(md) {
|
|
|
|
return when.resolve(nodeInfo);
|
|
|
|
});
|
2015-11-14 00:16:47 +01:00
|
|
|
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","local","TestNodeModule","node_modules","TestNodeModule"));
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
|
|
|
cb(null,"","");
|
|
|
|
});
|
|
|
|
installer.installModule(resourcesDir).then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-11-09 12:29:48 +01:00
|
|
|
});
|
|
|
|
describe("uninstalls module", function() {
|
|
|
|
it("rejects invalid module names", function(done) {
|
|
|
|
var promises = [];
|
|
|
|
promises.push(installer.uninstallModule("this_wont_exist "));
|
|
|
|
promises.push(installer.uninstallModule("this_wont_exist;no_it_really_wont"));
|
|
|
|
when.settle(promises).then(function(results) {
|
|
|
|
results[0].state.should.be.eql("rejected");
|
|
|
|
results[1].state.should.be.eql("rejected");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("rejects with generic error", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
|
|
|
var removeModule = sinon.stub(registry,"removeModule",function(md) {
|
|
|
|
return when.resolve(nodeInfo);
|
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-09 12:29:48 +01:00
|
|
|
cb(new Error("test_error"),"","");
|
|
|
|
});
|
|
|
|
|
|
|
|
installer.uninstallModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
|
|
|
var removeModule = sinon.stub(typeRegistry,"removeModule",function(md) {
|
|
|
|
return nodeInfo;
|
|
|
|
});
|
|
|
|
var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) {
|
|
|
|
return {nodes:[]};
|
|
|
|
});
|
2015-11-09 17:52:14 +01:00
|
|
|
sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-09 12:29:48 +01:00
|
|
|
cb(null,"","");
|
|
|
|
});
|
|
|
|
|
2015-11-16 12:31:55 +01:00
|
|
|
sinon.stub(fs,"statSync", function(fn) { return {}; });
|
2015-11-09 12:29:48 +01:00
|
|
|
|
|
|
|
installer.uninstallModule("this_wont_exist").then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
// commsMessages.should.have.length(1);
|
|
|
|
// commsMessages[0].topic.should.equal("node/removed");
|
|
|
|
// commsMessages[0].msg.should.eql(nodeInfo);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|