navy
Version:
Quick and powerful development environments using Docker and Docker Compose
176 lines (174 loc) • 6.7 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 = _interopRequireDefault(require("../../../util/fs"));
var execAsyncModule = _interopRequireWildcard(require("../../../util/exec-async"));
var _index = _interopRequireDefault(require("../index"));
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 */
function makeNavy({
name = 'env',
state = null
} = {}) {
return {
name,
getState: _sinon.default.stub().resolves(state)
};
}
describe('filesystem config provider', function () {
let sandbox;
beforeEach(function () {
sandbox = _sinon.default.createSandbox();
sandbox.stub(process, 'cwd').returns('/work/dir');
});
afterEach(function () {
sandbox.restore();
});
describe('getNavyPath', function () {
it('should return the path from state when the path exists on disk', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/some/path'
}
}));
sandbox.stub(_fs.default, 'statAsync').resolves({});
(0, _chai.expect)(await provider.getNavyPath()).to.equal('/some/path');
});
it('should throw STATE_NONEXISTANT when state is missing', async function () {
const provider = (0, _index.default)(makeNavy({
state: null
}));
let caught;
try {
await provider.getNavyPath();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/STATE_NONEXISTANT/);
});
it('should throw FILESYSTEM_PROVIDER_REQUIRES_PATH when state has no path', async function () {
const provider = (0, _index.default)(makeNavy({
state: {}
}));
let caught;
try {
await provider.getNavyPath();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/FILESYSTEM_PROVIDER_REQUIRES_PATH/);
});
it('should throw FILESYSTEM_PROVIDER_INVALID_PATH when stat of the path fails', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/missing'
}
}));
sandbox.stub(_fs.default, 'statAsync').rejects(new Error('ENOENT'));
let caught;
try {
await provider.getNavyPath();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/FILESYSTEM_PROVIDER_INVALID_PATH/);
});
});
describe('getNavyFilePath', function () {
it('should return <navyPath>/Navyfile.js', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/some/path'
}
}));
sandbox.stub(_fs.default, 'statAsync').resolves({});
(0, _chai.expect)(await provider.getNavyFilePath()).to.equal(_path.default.join('/some/path', 'Navyfile.js'));
});
});
describe('refreshConfig', function () {
it('should be a no-op that returns false', async function () {
const provider = (0, _index.default)(makeNavy());
(0, _chai.expect)(await provider.refreshConfig()).to.equal(false);
});
});
describe('getLocationDisplayName', function () {
it('should return the path from state', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/x'
}
}));
(0, _chai.expect)(await provider.getLocationDisplayName()).to.equal('/x');
});
it('should throw STATE_NONEXISTANT when state is missing', async function () {
const provider = (0, _index.default)(makeNavy({
state: null
}));
let caught;
try {
await provider.getLocationDisplayName();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/STATE_NONEXISTANT/);
});
});
describe('isDangling', function () {
it('should return true when state is null', async function () {
const provider = (0, _index.default)(makeNavy({
state: null
}));
(0, _chai.expect)(await provider.isDangling()).to.equal(true);
});
it('should return true when state has no path', async function () {
const provider = (0, _index.default)(makeNavy({
state: {}
}));
(0, _chai.expect)(await provider.isDangling()).to.equal(true);
});
it('should return true when stat of the path fails', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/x'
}
}));
sandbox.stub(_fs.default, 'statAsync').rejects(new Error('ENOENT'));
(0, _chai.expect)(await provider.isDangling()).to.equal(true);
});
it('should return false when state has a valid path', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
path: '/x'
}
}));
sandbox.stub(_fs.default, 'statAsync').resolves({});
(0, _chai.expect)(await provider.isDangling()).to.equal(false);
});
});
describe('importCliOptions', function () {
it('should expose an empty CLI options array', function () {
(0, _chai.expect)(_index.default.importCliOptions).to.eql([]);
});
});
describe('getImportOptionsForCLI', function () {
it('should return { configProvider: "filesystem", path: cwd } when docker-compose config is valid', async function () {
sandbox.stub(execAsyncModule, 'execAsync').resolves('');
(0, _chai.expect)(await _index.default.getImportOptionsForCLI({})).to.eql({
configProvider: 'filesystem',
path: '/work/dir'
});
});
it('should throw NO_DOCKER_COMPOSE_FILE when docker-compose config fails', async function () {
sandbox.stub(execAsyncModule, 'execAsync').rejects(new Error('boom'));
let caught;
try {
await _index.default.getImportOptionsForCLI({});
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/NO_DOCKER_COMPOSE_FILE/);
});
});
});