UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

103 lines (101 loc) 4.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _sinon = _interopRequireDefault(require("sinon")); var _path = _interopRequireDefault(require("path")); var _fs = require("fs"); var _fs2 = _interopRequireDefault(require("../../util/fs")); var _state = require("../state"); /* eslint-env mocha */ describe('navy/state', function () { let sandbox; let originalHome; beforeEach(function () { sandbox = _sinon.default.createSandbox(); originalHome = process.env.HOME; process.env.HOME = '/home/test'; }); afterEach(function () { sandbox.restore(); if (originalHome === undefined) { delete process.env.HOME; } else { process.env.HOME = originalHome; } }); describe('pathToNavyRoot', function () { it('should return $HOME/.navy', function () { (0, _chai.expect)((0, _state.pathToNavyRoot)()).to.equal(_path.default.join('/home/test', '.navy')); }); it('should throw when HOME is not set', function () { delete process.env.HOME; (0, _chai.expect)(() => (0, _state.pathToNavyRoot)()).to.throw(/NO_HOME_DIRECTORY/); }); }); describe('pathToNavys', function () { it('should return $HOME/.navy/navies', function () { (0, _chai.expect)((0, _state.pathToNavys)()).to.equal(_path.default.join('/home/test', '.navy', 'navies')); }); }); describe('pathToNavy', function () { it('should return $HOME/.navy/navies/<env>', function () { (0, _chai.expect)((0, _state.pathToNavy)('myenv')).to.equal(_path.default.join('/home/test', '.navy', 'navies', 'myenv')); }); }); describe('pathToState', function () { it('should return $HOME/.navy/navies/<env>/state.json', function () { (0, _chai.expect)((0, _state.pathToState)('myenv')).to.equal(_path.default.join('/home/test', '.navy', 'navies', 'myenv', 'state.json')); }); }); describe('getState', function () { it('should read and parse the state.json file', async function () { const state = { driver: 'docker-compose', services: { api: {} } }; const buf = Buffer.from(JSON.stringify(state)); sandbox.stub(_fs2.default, 'readFileAsync').resolves(buf); (0, _chai.expect)(await (0, _state.getState)('myenv')).to.eql(state); }); it('should return null when the state file cannot be read', async function () { sandbox.stub(_fs2.default, 'readFileAsync').rejects(new Error('ENOENT')); (0, _chai.expect)(await (0, _state.getState)('myenv')).to.equal(null); }); it('should return null when the state file content is not valid JSON', async function () { sandbox.stub(_fs2.default, 'readFileAsync').resolves(Buffer.from('not json')); (0, _chai.expect)(await (0, _state.getState)('myenv')).to.equal(null); }); }); describe('saveState', function () { it('should mkdir -p the navy directory and write the state JSON file', async function () { const mkdirStub = sandbox.stub(_fs.promises, 'mkdir').resolves(); const writeStub = sandbox.stub(_fs2.default, 'writeFileAsync').resolves(); const state = { driver: 'docker-compose' }; await (0, _state.saveState)('myenv', state); (0, _chai.expect)(mkdirStub.calledOnce).to.equal(true); (0, _chai.expect)(mkdirStub.firstCall.args[0]).to.equal(_path.default.join('/home/test', '.navy', 'navies', 'myenv')); (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', '.navy', 'navies', 'myenv', 'state.json')); (0, _chai.expect)(JSON.parse(writeStub.firstCall.args[1])).to.eql(state); }); }); describe('deleteState', function () { it('should rm -rf the navy directory recursively', async function () { const rmStub = sandbox.stub(_fs.promises, 'rm').resolves(); await (0, _state.deleteState)('myenv'); (0, _chai.expect)(rmStub.calledOnce).to.equal(true); (0, _chai.expect)(rmStub.firstCall.args[0]).to.equal(_path.default.join('/home/test', '.navy', 'navies', 'myenv')); (0, _chai.expect)(rmStub.firstCall.args[1]).to.eql({ recursive: true, force: true }); }); }); });