Fixed bug in npmInstall so that it returns on error.

This commit is contained in:
Jim Unger 2015-07-09 20:28:53 -05:00
parent 49e67dd444
commit 6a5978145d
5 changed files with 284 additions and 286 deletions

View file

@ -13,7 +13,7 @@ module.exports = function (dest, logger) {
if (e.code !== 'ENOENT')
throw e;
reject(new Error('Plugin does not contain package.json file'));
return reject(new Error('Plugin does not contain package.json file'));
}
var cmd = (process.env.NPM) ? process.env.NPM : 'npm';

View file

@ -1,11 +1,11 @@
var root = require('requirefrom')('');
var expect = require('expect.js');
var sinon = require('sinon');
var nock = require('nock');
var glob = require('glob');
var rimraf = require('rimraf');
var fs = require('fs');
var join = require('path').join;
var sinon = require('sinon');
var pluginLogger = root('src/server/bin/plugin/pluginLogger');
var npmInstall = root('src/server/bin/plugin/npmInstall');
@ -15,55 +15,52 @@ describe('kibana cli', function () {
describe('npmInstall', function () {
//var logger;
//var testWorkingPath;
//beforeEach(function () {
// logger = pluginLogger(false);
// testWorkingPath = join(__dirname, '.test.data');
// rimraf.sync(testWorkingPath);
// sinon.stub(logger, 'log', function (data, sameLine) {
// data.pipe(process.stdout);
// });
// sinon.stub(logger, 'error', function (data) {
// data.pipe(process.stderr);
// });
//});
var logger;
var testWorkingPath = join(__dirname, '.test.data');
var statSyncStub;
//afterEach(function () {
// logger.log.restore();
// logger.error.restore();
// rimraf.sync(testWorkingPath);
//});
beforeEach(function () {
statSyncStub = undefined;
logger = pluginLogger(false);
rimraf.sync(testWorkingPath);
sinon.stub(logger, 'log');
sinon.stub(logger, 'error');
});
//it('should throw an error if there is no package.json file in the archive', function () {
// fs.mkdirSync(testWorkingPath);
afterEach(function () {
logger.log.restore();
logger.error.restore();
rimraf.sync(testWorkingPath);
if (statSyncStub) statSyncStub.restore();
});
// var errorStub = sinon.stub();
// return npmInstall(testWorkingPath, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(true);
// expect(errorStub.lastCall.args[0].message).to.match(/package.json/);
// });
//});
it('should throw an error if there is no package.json file in the archive', function () {
fs.mkdirSync(testWorkingPath);
//it('should rethrow any errors other than "ENOENT" from fs.statSync', function () {
// fs.mkdirSync(testWorkingPath);
var errorStub = sinon.stub();
return npmInstall(testWorkingPath, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(true);
expect(errorStub.lastCall.args[0].message).to.match(/package.json/);
});
});
// sinon.stub(fs, 'statSync', function () {
// throw new Error('This is unexpected.');
// });
it('should rethrow any errors other than "ENOENT" from fs.statSync', function () {
fs.mkdirSync(testWorkingPath);
// var errorStub = sinon.stub();
// return npmInstall(testWorkingPath, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(true);
// expect(errorStub.lastCall.args[0].message).to.match(/This is unexpected./);
statSyncStub = sinon.stub(fs, 'statSync', function () {
throw new Error('This is unexpected.');
});
// fs.statSync.restore();
// });
//});
var errorStub = sinon.stub();
return npmInstall(testWorkingPath, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(true);
expect(errorStub.lastCall.args[0].message).to.match(/This is unexpected./);
});
});
});

View file

@ -17,11 +17,13 @@ describe('kibana cli', function () {
};
describe('cleanPrevious', function () {
var cleaner;
var errorStub;
var logger;
var progress;
var request;
beforeEach(function () {
errorStub = sinon.stub();
logger = pluginLogger(false);

View file

@ -12,243 +12,237 @@ describe('kibana cli', function () {
describe('plugin downloader', function () {
//var testWorkingPath;
//var logger;
//var downloader;
var testWorkingPath = join(__dirname, '.test.data');
var logger;
var downloader;
//describe('_downloadSingle', function () {
beforeEach(function () {
logger = pluginLogger(false);
sinon.stub(logger, 'log');
sinon.stub(logger, 'error');
rimraf.sync(testWorkingPath);
});
// beforeEach(function () {
// logger = pluginLogger(false);
// downloader = pluginDownloader({}, logger);
// testWorkingPath = join(__dirname, '.test.data');
// sinon.stub(logger, 'log');
// sinon.stub(logger, 'error');
// rimraf.sync(testWorkingPath);
// });
afterEach(function () {
logger.log.restore();
logger.error.restore();
rimraf.sync(testWorkingPath);
});
// afterEach(function () {
// logger.log.restore();
// logger.error.restore();
// rimraf.sync(testWorkingPath);
// });
describe('_downloadSingle', function () {
// it('should throw an ENOTFOUND error for a 404 error', function () {
// var couchdb = nock('http://www.files.com')
// .get('/plugin.tar.gz')
// .reply(404);
beforeEach(function () {
downloader = pluginDownloader({}, logger);
});
// var source = 'http://www.files.com/plugin.tar.gz';
afterEach(function () {
});
// var errorStub = sinon.stub();
// return downloader._downloadSingle(source, testWorkingPath, 0, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(true);
// expect(errorStub.lastCall.args[0].message).to.match(/ENOTFOUND/);
it('should throw an ENOTFOUND error for a 404 error', function () {
var couchdb = nock('http://www.files.com')
.get('/plugin.tar.gz')
.reply(404);
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// expect(files).to.eql([]);
// });
// });
var source = 'http://www.files.com/plugin.tar.gz';
// it('should download and extract a valid plugin', function () {
// var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
// var couchdb = nock('http://www.files.com')
// .defaultReplyHeaders({
// 'content-length': '10'
// })
// .get('/plugin.tar.gz')
// .replyWithFile(200, filename);
var errorStub = sinon.stub();
return downloader._downloadSingle(source, testWorkingPath, 0, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(true);
expect(errorStub.lastCall.args[0].message).to.match(/ENOTFOUND/);
// var source = 'http://www.files.com/plugin.tar.gz';
var files = glob.sync('**/*', { cwd: testWorkingPath });
expect(files).to.eql([]);
});
});
// return downloader._downloadSingle(source, testWorkingPath, 0, logger)
// .then(function (data) {
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// var expected = [
// 'README.md',
// 'index.js',
// 'package.json',
// 'public',
// 'public/app.js'
// ];
// expect(files.sort()).to.eql(expected.sort());
// });
// });
it('should download and extract a valid plugin', function () {
var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
var couchdb = nock('http://www.files.com')
.defaultReplyHeaders({
'content-length': '10'
})
.get('/plugin.tar.gz')
.replyWithFile(200, filename);
// it('should abort the download and extraction for a corrupt archive.', function () {
// var filename = join(__dirname, 'replies/corrupt.tar.gz');
// var couchdb = nock('http://www.files.com')
// .get('/plugin.tar.gz')
// .replyWithFile(200, filename);
var source = 'http://www.files.com/plugin.tar.gz';
// var source = 'http://www.files.com/plugin.tar.gz';
return downloader._downloadSingle(source, testWorkingPath, 0, logger)
.then(function (data) {
var files = glob.sync('**/*', { cwd: testWorkingPath });
var expected = [
'README.md',
'index.js',
'package.json',
'public',
'public/app.js'
];
expect(files.sort()).to.eql(expected.sort());
});
});
// var errorStub = sinon.stub();
// return downloader._downloadSingle(source, testWorkingPath, 0, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(true);
it('should abort the download and extraction for a corrupt archive.', function () {
var filename = join(__dirname, 'replies/corrupt.tar.gz');
var couchdb = nock('http://www.files.com')
.get('/plugin.tar.gz')
.replyWithFile(200, filename);
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// expect(files).to.eql([]);
// });
// });
var source = 'http://www.files.com/plugin.tar.gz';
//});
var errorStub = sinon.stub();
return downloader._downloadSingle(source, testWorkingPath, 0, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(true);
//describe('download', function () {
var files = glob.sync('**/*', { cwd: testWorkingPath });
expect(files).to.eql([]);
});
});
// var downloader;
// beforeEach(function () {
// logger = pluginLogger(false);
// sinon.stub(logger, 'log');
// sinon.stub(logger, 'error');
// testWorkingPath = join(__dirname, '.test.data');
// rimraf.sync(testWorkingPath);
// });
});
// afterEach(function () {
// logger.log.restore();
// logger.error.restore();
// rimraf.sync(testWorkingPath);
// });
describe('download', function () {
// it('should loop through bad urls until it finds a good one.', function () {
// var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
// var settings = {
// urls: [
// 'http://www.files.com/badfile1.tar.gz',
// 'http://www.files.com/badfile2.tar.gz',
// 'I am a bad uri',
// 'http://www.files.com/goodfile.tar.gz'
// ],
// workingPath: testWorkingPath,
// timeout: 0
// };
// downloader = pluginDownloader(settings, logger);
beforeEach(function () {});
// var couchdb = nock('http://www.files.com')
// .defaultReplyHeaders({
// 'content-length': '10'
// })
// .get('/badfile1.tar.gz')
// .reply(404)
// .get('/badfile2.tar.gz')
// .reply(404)
// .get('/goodfile.tar.gz')
// .replyWithFile(200, filename);
afterEach(function () {});
// var errorStub = sinon.stub();
// return downloader.download(settings, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(false);
it('should loop through bad urls until it finds a good one.', function () {
var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
var settings = {
urls: [
'http://www.files.com/badfile1.tar.gz',
'http://www.files.com/badfile2.tar.gz',
'I am a bad uri',
'http://www.files.com/goodfile.tar.gz'
],
workingPath: testWorkingPath,
timeout: 0
};
downloader = pluginDownloader(settings, logger);
// expect(logger.log.getCall(0).args[0]).to.match(/badfile1.tar.gz/);
// expect(logger.log.getCall(1).args[0]).to.match(/badfile2.tar.gz/);
// expect(logger.log.getCall(2).args[0]).to.match(/I am a bad uri/);
// expect(logger.log.getCall(3).args[0]).to.match(/goodfile.tar.gz/);
// expect(logger.log.lastCall.args[0]).to.match(/complete/i);
var couchdb = nock('http://www.files.com')
.defaultReplyHeaders({
'content-length': '10'
})
.get('/badfile1.tar.gz')
.reply(404)
.get('/badfile2.tar.gz')
.reply(404)
.get('/goodfile.tar.gz')
.replyWithFile(200, filename);
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// var expected = [
// 'README.md',
// 'index.js',
// 'package.json',
// 'public',
// 'public/app.js'
// ];
// expect(files.sort()).to.eql(expected.sort());
// });
// });
var errorStub = sinon.stub();
return downloader.download(settings, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(false);
// it('should stop looping through urls when it finds a good one.', function () {
// var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
// var settings = {
// urls: [
// 'http://www.files.com/badfile1.tar.gz',
// 'http://www.files.com/badfile2.tar.gz',
// 'http://www.files.com/goodfile.tar.gz',
// 'http://www.files.com/badfile3.tar.gz'
// ],
// workingPath: testWorkingPath,
// timeout: 0
// };
// downloader = pluginDownloader(settings, logger);
expect(logger.log.getCall(0).args[0]).to.match(/badfile1.tar.gz/);
expect(logger.log.getCall(1).args[0]).to.match(/badfile2.tar.gz/);
expect(logger.log.getCall(2).args[0]).to.match(/I am a bad uri/);
expect(logger.log.getCall(3).args[0]).to.match(/goodfile.tar.gz/);
expect(logger.log.lastCall.args[0]).to.match(/complete/i);
// var couchdb = nock('http://www.files.com')
// .defaultReplyHeaders({
// 'content-length': '10'
// })
// .get('/badfile1.tar.gz')
// .reply(404)
// .get('/badfile2.tar.gz')
// .reply(404)
// .get('/goodfile.tar.gz')
// .replyWithFile(200, filename)
// .get('/badfile3.tar.gz')
// .reply(404);
var files = glob.sync('**/*', { cwd: testWorkingPath });
var expected = [
'README.md',
'index.js',
'package.json',
'public',
'public/app.js'
];
expect(files.sort()).to.eql(expected.sort());
});
});
// var errorStub = sinon.stub();
// return downloader.download(settings, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(false);
it('should stop looping through urls when it finds a good one.', function () {
var filename = join(__dirname, 'replies/test-plugin-master.tar.gz');
var settings = {
urls: [
'http://www.files.com/badfile1.tar.gz',
'http://www.files.com/badfile2.tar.gz',
'http://www.files.com/goodfile.tar.gz',
'http://www.files.com/badfile3.tar.gz'
],
workingPath: testWorkingPath,
timeout: 0
};
downloader = pluginDownloader(settings, logger);
// for (var i = 0; i < logger.log.callCount; i++) {
// expect(logger.log.getCall(i).args[0]).to.not.match(/badfile3.tar.gz/);
// }
var couchdb = nock('http://www.files.com')
.defaultReplyHeaders({
'content-length': '10'
})
.get('/badfile1.tar.gz')
.reply(404)
.get('/badfile2.tar.gz')
.reply(404)
.get('/goodfile.tar.gz')
.replyWithFile(200, filename)
.get('/badfile3.tar.gz')
.reply(404);
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// var expected = [
// 'README.md',
// 'index.js',
// 'package.json',
// 'public',
// 'public/app.js'
// ];
// expect(files.sort()).to.eql(expected.sort());
// });
// });
var errorStub = sinon.stub();
return downloader.download(settings, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(false);
// it('should throw an error when it doesn\'t find a good url.', function () {
// var settings = {
// urls: [
// 'http://www.files.com/badfile1.tar.gz',
// 'http://www.files.com/badfile2.tar.gz',
// 'http://www.files.com/badfile3.tar.gz'
// ],
// workingPath: testWorkingPath,
// timeout: 0
// };
// downloader = pluginDownloader(settings, logger);
for (var i = 0; i < logger.log.callCount; i++) {
expect(logger.log.getCall(i).args[0]).to.not.match(/badfile3.tar.gz/);
}
// var couchdb = nock('http://www.files.com')
// .defaultReplyHeaders({
// 'content-length': '10'
// })
// .get('/badfile1.tar.gz')
// .reply(404)
// .get('/badfile2.tar.gz')
// .reply(404)
// .get('/badfile3.tar.gz')
// .reply(404);
var files = glob.sync('**/*', { cwd: testWorkingPath });
var expected = [
'README.md',
'index.js',
'package.json',
'public',
'public/app.js'
];
expect(files.sort()).to.eql(expected.sort());
});
});
// var errorStub = sinon.stub();
// return downloader.download(settings, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(errorStub.called).to.be(true);
// expect(errorStub.lastCall.args[0].message).to.match(/not a valid/i);
it('should throw an error when it doesn\'t find a good url.', function () {
var settings = {
urls: [
'http://www.files.com/badfile1.tar.gz',
'http://www.files.com/badfile2.tar.gz',
'http://www.files.com/badfile3.tar.gz'
],
workingPath: testWorkingPath,
timeout: 0
};
downloader = pluginDownloader(settings, logger);
// var files = glob.sync('**/*', { cwd: testWorkingPath });
// expect(files).to.eql([]);
// });
// });
var couchdb = nock('http://www.files.com')
.defaultReplyHeaders({
'content-length': '10'
})
.get('/badfile1.tar.gz')
.reply(404)
.get('/badfile2.tar.gz')
.reply(404)
.get('/badfile3.tar.gz')
.reply(404);
//});
var errorStub = sinon.stub();
return downloader.download(settings, logger)
.catch(errorStub)
.then(function (data) {
expect(errorStub.called).to.be(true);
expect(errorStub.lastCall.args[0].message).to.match(/not a valid/i);
var files = glob.sync('**/*', { cwd: testWorkingPath });
expect(files).to.eql([]);
});
});
});
});

View file

@ -16,51 +16,56 @@ describe('kibana cli', function () {
describe('pluginInstaller', function () {
//var logger;
//var testWorkingPath;
//beforeEach(function () {
// logger = pluginLogger(false);
// testWorkingPath = join(__dirname, '.test.data');
// rimraf.sync(testWorkingPath);
// sinon.stub(logger, 'log');
// sinon.stub(logger, 'error');
//});
var logger;
var testWorkingPath;
var processExitStub;
var statSyncStub;
beforeEach(function () {
processExitStub = undefined;
statSyncStub = undefined;
logger = pluginLogger(false);
testWorkingPath = join(__dirname, '.test.data');
rimraf.sync(testWorkingPath);
sinon.stub(logger, 'log');
sinon.stub(logger, 'error');
});
//afterEach(function () {
// logger.log.restore();
// logger.error.restore();
// rimraf.sync(testWorkingPath);
//});
afterEach(function () {
if (processExitStub) processExitStub.restore();
if (statSyncStub) statSyncStub.restore();
logger.log.restore();
logger.error.restore();
rimraf.sync(testWorkingPath);
});
//it('should throw an error if the workingPath already exists.', function () {
// sinon.stub(process, 'exit');
// fs.mkdirSync(testWorkingPath);
it('should throw an error if the workingPath already exists.', function () {
processExitStub = sinon.stub(process, 'exit');
fs.mkdirSync(testWorkingPath);
// var settings = {
// pluginPath: testWorkingPath
// };
var settings = {
pluginPath: testWorkingPath
};
// var errorStub = sinon.stub();
// return pluginInstaller.install(settings, logger)
// .catch(errorStub)
// .then(function (data) {
// expect(logger.error.firstCall.args[0]).to.match(/already exists/);
// expect(process.exit.called).to.be(true);
// process.exit.restore();
// });
//});
var errorStub = sinon.stub();
return pluginInstaller.install(settings, logger)
.catch(errorStub)
.then(function (data) {
expect(logger.error.firstCall.args[0]).to.match(/already exists/);
expect(process.exit.called).to.be(true);
});
});
//it('should rethrow any non "ENOENT" error from fs.', function () {
// sinon.stub(fs, 'statSync', function () {
// throw new Error('This is unexpected.');
// });
it('should rethrow any non "ENOENT" error from fs.', function () {
statSyncStub = sinon.stub(fs, 'statSync', function () {
throw new Error('This is unexpected.');
});
// var settings = {
// pluginPath: testWorkingPath
// };
var settings = {
pluginPath: testWorkingPath
};
// expect(pluginInstaller.install).withArgs(settings, logger).to.throwException(/this is unexpected/i);
//});
expect(pluginInstaller.install).withArgs(settings, logger).to.throwException(/this is unexpected/i);
});
});