navy
Version:
Quick and powerful development environments using Docker and Docker Compose
123 lines (121 loc) • 5.48 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _proxyquire = _interopRequireDefault(require("proxyquire"));
var _errors = require("../../../errors");
/* eslint-env mocha */
describe('cli/util/import', function () {
let sandbox;
let startDriverLoggingStub;
let stopDriverLoggingStub;
let consoleLogStub;
let navyStub;
let importNavy;
beforeEach(function () {
sandbox = _sinon.default.createSandbox();
navyStub = {
name: 'env-1',
isInitialised: sandbox.stub().resolves(false),
initialise: sandbox.stub().resolves(),
ensurePluginsLoaded: sandbox.stub().resolves(),
emitAsync: sandbox.stub().resolves(),
relaunch: sandbox.stub().resolves()
};
startDriverLoggingStub = sandbox.stub();
stopDriverLoggingStub = sandbox.stub();
consoleLogStub = sandbox.stub(console, 'log');
importNavy = _proxyquire.default.noCallThru()('../import', {
'../../driver-logging': {
startDriverLogging: startDriverLoggingStub,
stopDriverLogging: stopDriverLoggingStub
},
'../../errors': {
NavyError: _errors.NavyError
}
}).importNavy;
});
afterEach(function () {
sandbox.restore();
});
describe('importNavy', function () {
it('should throw a NavyError when the navy has already been initialised', async function () {
navyStub.isInitialised.resolves(true);
let caught;
try {
await importNavy(navyStub, {});
} catch (err) {
caught = err;
}
(0, _chai.expect)(caught).to.be.instanceof(_errors.NavyError);
(0, _chai.expect)(caught.message).to.contain('env-1');
(0, _chai.expect)(caught.message).to.contain('already been imported');
(0, _chai.expect)(navyStub.initialise.called).to.equal(false);
});
it('should initialise the navy with the provided opts', async function () {
const opts = {
configProvider: 'filesystem',
path: '/cwd'
};
await importNavy(navyStub, opts);
(0, _chai.expect)(navyStub.initialise.calledOnce).to.equal(true);
(0, _chai.expect)(navyStub.initialise.firstCall.args[0]).to.equal(opts);
});
it('should ensure plugins are loaded and emit cli.import in order before relaunch', async function () {
await importNavy(navyStub, {});
(0, _chai.expect)(navyStub.ensurePluginsLoaded.calledOnce).to.equal(true);
(0, _chai.expect)(navyStub.emitAsync.calledOnce).to.equal(true);
(0, _chai.expect)(navyStub.emitAsync.firstCall.args[0]).to.equal('cli.import');
(0, _chai.expect)(navyStub.relaunch.calledOnce).to.equal(true);
(0, _chai.expect)(navyStub.initialise.calledBefore(navyStub.ensurePluginsLoaded)).to.equal(true);
(0, _chai.expect)(navyStub.ensurePluginsLoaded.calledBefore(navyStub.emitAsync)).to.equal(true);
(0, _chai.expect)(navyStub.emitAsync.calledBefore(navyStub.relaunch)).to.equal(true);
});
it('should bracket relaunch with start/stopDriverLogging', async function () {
await importNavy(navyStub, {});
(0, _chai.expect)(startDriverLoggingStub.calledOnce).to.equal(true);
(0, _chai.expect)(stopDriverLoggingStub.calledOnce).to.equal(true);
(0, _chai.expect)(startDriverLoggingStub.calledBefore(navyStub.relaunch)).to.equal(true);
(0, _chai.expect)(navyStub.relaunch.calledBefore(stopDriverLoggingStub)).to.equal(true);
});
it('should log a success message containing the navy name', async function () {
await importNavy(navyStub, {});
const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join(' ');
(0, _chai.expect)(printed).to.contain('env-1');
(0, _chai.expect)(printed).to.contain('imported and initialised');
});
it('should print a labelled line for each known option key', async function () {
const opts = {
configProvider: 'filesystem',
path: '/cwd',
npmPackage: 'some-pkg'
};
await importNavy(navyStub, opts);
const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join('\n');
(0, _chai.expect)(printed).to.contain('Provider');
(0, _chai.expect)(printed).to.contain('filesystem');
(0, _chai.expect)(printed).to.contain('Directory');
(0, _chai.expect)(printed).to.contain('/cwd');
(0, _chai.expect)(printed).to.contain('NPM Package');
(0, _chai.expect)(printed).to.contain('some-pkg');
});
it('should ignore unknown option keys when printing labels', async function () {
const opts = {
configProvider: 'filesystem',
unrecognised: 'value'
};
await importNavy(navyStub, opts);
const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join('\n');
(0, _chai.expect)(printed).to.contain('Provider');
(0, _chai.expect)(printed).to.not.contain('unrecognised');
(0, _chai.expect)(printed).to.not.contain('value');
});
it('should not print any option label lines when no known options are supplied', async function () {
await importNavy(navyStub, {});
const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join('\n');
(0, _chai.expect)(printed).to.not.contain('Provider');
(0, _chai.expect)(printed).to.not.contain('Directory');
(0, _chai.expect)(printed).to.not.contain('NPM Package');
});
});
});