navy
Version:
Quick and powerful development environments using Docker and Docker Compose
226 lines (224 loc) • 7.72 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _chai = require("chai");
var _sinon = _interopRequireDefault(require("sinon"));
var _fs = require("fs");
var _child_process = _interopRequireDefault(require("child_process"));
var _fs2 = _interopRequireDefault(require("../../../util/fs"));
var _index = _interopRequireDefault(require("../index"));
/* eslint-env mocha */
function makeNavy({
name = 'env',
state = null
} = {}) {
return {
name,
getState: _sinon.default.stub().resolves(state)
};
}
describe('npm config provider', 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('getNavyPath', function () {
it('should return the resolved module path when the package is installed', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'my-config'
}
}));
sandbox.stub(_fs2.default, 'accessSync');
const result = await provider.getNavyPath();
(0, _chai.expect)(result).to.contain('node_modules');
(0, _chai.expect)(result).to.contain('my-config');
});
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 NPM_PROVIDER_REQUIRES_PACKAGE when state has no npmPackage', 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(/NPM_PROVIDER_REQUIRES_PACKAGE/);
});
it('should throw when accessSync fails', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'missing'
}
}));
sandbox.stub(_fs2.default, 'accessSync').throws(new Error('ENOENT'));
let caught;
try {
await provider.getNavyPath();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught).to.be.an('error');
});
});
describe('getNavyFilePath', function () {
it('should return <navyPath>/Navyfile.js', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'my-config'
}
}));
sandbox.stub(_fs2.default, 'accessSync');
const filePath = await provider.getNavyFilePath();
(0, _chai.expect)(filePath).to.match(/my-config\/Navyfile\.js$/);
});
});
describe('refreshConfig', function () {
it('should reinstall the package via npm and return true', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'pkg'
}
}));
sandbox.stub(_fs.promises, 'mkdir').resolves();
const execSync = sandbox.stub(_child_process.default, 'execSync').returns('');
(0, _chai.expect)(await provider.refreshConfig()).to.equal(true);
const calls = execSync.getCalls().map(c => c.args[0]);
(0, _chai.expect)(calls[0]).to.match(/npm info pkg name/);
(0, _chai.expect)(calls[1]).to.match(/npm i pkg/);
});
it('should throw STATE_NONEXISTANT when state is missing', async function () {
const provider = (0, _index.default)(makeNavy({
state: null
}));
let caught;
try {
await provider.refreshConfig();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/STATE_NONEXISTANT/);
});
it('should throw NPM_PROVIDER_REQUIRES_PACKAGE when no npmPackage in state', async function () {
const provider = (0, _index.default)(makeNavy({
state: {}
}));
let caught;
try {
await provider.refreshConfig();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.match(/NPM_PROVIDER_REQUIRES_PACKAGE/);
});
it('should throw a friendly error when npm info fails', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'unknown-pkg'
}
}));
sandbox.stub(_fs.promises, 'mkdir').resolves();
sandbox.stub(_child_process.default, 'execSync').callsFake(cmd => {
if (cmd.startsWith('npm info')) throw new Error('not found');
return '';
});
let caught;
try {
await provider.refreshConfig();
} catch (e) {
caught = e;
}
(0, _chai.expect)(caught.message).to.contain('Package "unknown-pkg" not found or unreachable');
});
});
describe('getLocationDisplayName', function () {
it('should return the npmPackage from state', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'pkg'
}
}));
(0, _chai.expect)(await provider.getLocationDisplayName()).to.equal('pkg');
});
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 npmPackage', async function () {
const provider = (0, _index.default)(makeNavy({
state: {}
}));
(0, _chai.expect)(await provider.isDangling()).to.equal(true);
});
it('should return false when state has an npmPackage', async function () {
const provider = (0, _index.default)(makeNavy({
state: {
npmPackage: 'pkg'
}
}));
(0, _chai.expect)(await provider.isDangling()).to.equal(false);
});
});
describe('importCliOptions', function () {
it('should advertise the --npm-package CLI option', function () {
(0, _chai.expect)(_index.default.importCliOptions).to.have.lengthOf(1);
(0, _chai.expect)(_index.default.importCliOptions[0][0]).to.match(/--npm-package/);
});
});
describe('getImportOptionsForCLI', function () {
it('should install the package and return npm provider config when --npm-package is provided', async function () {
sandbox.stub(_fs.promises, 'mkdir').resolves();
sandbox.stub(_child_process.default, 'execSync').returns('');
const result = await _index.default.getImportOptionsForCLI({
npmPackage: 'my-pkg'
});
(0, _chai.expect)(result).to.eql({
configProvider: 'npm',
npmPackage: 'my-pkg'
});
});
it('should return undefined when --npm-package is not provided', async function () {
const result = await _index.default.getImportOptionsForCLI({});
(0, _chai.expect)(result).to.equal(undefined);
});
});
});