navy
Version:
Quick and powerful development environments using Docker and Docker Compose
130 lines (128 loc) • 5.98 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _path = _interopRequireDefault(require("path"));
var _fs = _interopRequireWildcard(require("fs"));
var _config = require("../config");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
/* eslint-env mocha */
async function clearConfigCache(sandbox) {
const mkdirRestore = sandbox.stub(_fs.promises, 'mkdir').resolves();
const writeRestore = sandbox.stub(_fs.promises, 'writeFile').resolves();
await (0, _config.setConfig)(null);
mkdirRestore.restore();
writeRestore.restore();
}
describe('config', function () {
let sandbox;
let originalHome;
beforeEach(async function () {
sandbox = _sinon.default.createSandbox();
originalHome = process.env.HOME;
process.env.HOME = '/home/test-user';
await clearConfigCache(sandbox);
});
afterEach(function () {
sandbox.restore();
if (originalHome === undefined) {
delete process.env.HOME;
} else {
process.env.HOME = originalHome;
}
});
describe('getConfigDir', function () {
it('should join $HOME with .navy', function () {
(0, _chai.expect)((0, _config.getConfigDir)()).to.equal(_path.default.join('/home/test-user', '.navy'));
});
it('should throw when HOME is not set', function () {
delete process.env.HOME;
(0, _chai.expect)(() => (0, _config.getConfigDir)()).to.throw(/NO_HOME_DIRECTORY/);
});
});
describe('getConfigPath', function () {
it('should return <configDir>/config.json', function () {
(0, _chai.expect)((0, _config.getConfigPath)()).to.equal(_path.default.join('/home/test-user', '.navy', 'config.json'));
});
});
describe('DEFAULT_TLS_ROOT_CA_DIR', function () {
it('should be exported as a string referencing tls-root-ca', function () {
(0, _chai.expect)(_config.DEFAULT_TLS_ROOT_CA_DIR).to.be.a('string');
(0, _chai.expect)(_config.DEFAULT_TLS_ROOT_CA_DIR).to.contain('tls-root-ca');
});
});
describe('getConfig', function () {
it('should read the config file from disk and parse it as JSON', function () {
const onDisk = {
defaultNavy: 'mine',
externalIP: '10.0.0.1',
tlsRootCaDir: '/tmp/ca'
};
sandbox.stub(_fs.default, 'readFileSync').returns(JSON.stringify(onDisk));
(0, _chai.expect)((0, _config.getConfig)()).to.eql(onDisk);
});
it('should return the cached config on subsequent calls', function () {
const onDisk = {
defaultNavy: 'mine'
};
const readStub = sandbox.stub(_fs.default, 'readFileSync').returns(JSON.stringify(onDisk));
(0, _config.getConfig)();
(0, _config.getConfig)();
(0, _config.getConfig)();
(0, _chai.expect)(readStub.callCount).to.equal(1);
});
it('should fall back to default config when reading the file fails', function () {
sandbox.stub(_fs.default, 'readFileSync').throws(new Error('ENOENT'));
const cfg = (0, _config.getConfig)();
(0, _chai.expect)(cfg).to.have.property('defaultNavy', 'dev');
(0, _chai.expect)(cfg).to.have.property('externalIP', null);
(0, _chai.expect)(cfg).to.have.property('tlsRootCaDir').that.is.a('string');
});
});
describe('setConfig', function () {
it('should write the provided config to the config file', async function () {
const mkdirStub = sandbox.stub(_fs.promises, 'mkdir').resolves();
const writeStub = sandbox.stub(_fs.promises, 'writeFile').resolves();
await (0, _config.setConfig)({
defaultNavy: 'env-1'
});
(0, _chai.expect)(mkdirStub.calledOnce).to.equal(true);
(0, _chai.expect)(mkdirStub.firstCall.args[0]).to.equal(_path.default.join('/home/test-user', '.navy'));
(0, _chai.expect)(mkdirStub.firstCall.args[1]).to.eql({
recursive: true
});
(0, _chai.expect)(writeStub.calledOnce).to.equal(true);
(0, _chai.expect)(writeStub.firstCall.args[0]).to.equal(_path.default.join('/home/test-user', '.navy', 'config.json'));
(0, _chai.expect)(JSON.parse(writeStub.firstCall.args[1])).to.eql({
defaultNavy: 'env-1'
});
});
it('should write the default config when called with null', async function () {
sandbox.stub(_fs.promises, 'mkdir').resolves();
const writeStub = sandbox.stub(_fs.promises, 'writeFile').resolves();
await (0, _config.setConfig)(null);
const written = JSON.parse(writeStub.firstCall.args[1]);
(0, _chai.expect)(written).to.have.property('defaultNavy', 'dev');
});
it('should invalidate the cached config so the next getConfig re-reads', async function () {
const readStub = sandbox.stub(_fs.default, 'readFileSync').returns(JSON.stringify({
defaultNavy: 'one'
}));
(0, _config.getConfig)();
(0, _chai.expect)(readStub.callCount).to.equal(1);
sandbox.stub(_fs.promises, 'mkdir').resolves();
sandbox.stub(_fs.promises, 'writeFile').resolves();
await (0, _config.setConfig)({
defaultNavy: 'two'
});
readStub.returns(JSON.stringify({
defaultNavy: 'two'
}));
const cfg = (0, _config.getConfig)();
(0, _chai.expect)(readStub.callCount).to.equal(2);
(0, _chai.expect)(cfg).to.eql({
defaultNavy: 'two'
});
});
});
});