2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const path = require('path');
|
|
|
|
const rewire = require('rewire');
|
|
|
|
const _ = require('lodash');
|
2020-06-02 00:06:50 +03:00
|
|
|
const configUtils = require('../../../utils/configUtils');
|
2017-02-02 15:46:30 +03:00
|
|
|
|
2021-06-16 16:42:06 +03:00
|
|
|
describe('Config Loader', function () {
|
2016-04-13 19:10:40 +03:00
|
|
|
before(function () {
|
|
|
|
configUtils.restore();
|
|
|
|
});
|
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
afterEach(function () {
|
|
|
|
configUtils.restore();
|
2014-12-14 14:31:00 +03:00
|
|
|
});
|
|
|
|
|
2017-02-02 15:46:30 +03:00
|
|
|
describe('hierarchy of config channels', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let originalEnv;
|
|
|
|
let originalArgv;
|
|
|
|
let customConfig;
|
2021-06-16 16:42:06 +03:00
|
|
|
let loader;
|
2017-02-02 15:46:30 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
originalEnv = _.clone(process.env);
|
|
|
|
originalArgv = _.clone(process.argv);
|
2021-06-16 16:42:06 +03:00
|
|
|
loader = rewire('../../../../core/shared/config/loader');
|
2017-06-07 12:31:01 +03:00
|
|
|
|
|
|
|
// we manually call `loadConf` in the tests and we need to ensure that the minimum
|
|
|
|
// required config properties are available
|
2019-08-19 14:41:09 +03:00
|
|
|
process.env.paths__contentPath = 'content/';
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
process.env = originalEnv;
|
|
|
|
process.argv = originalArgv;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('env parameter is stronger than file', function () {
|
2019-08-19 14:41:09 +03:00
|
|
|
process.env.database__client = 'test';
|
2017-02-02 15:46:30 +03:00
|
|
|
|
2021-06-16 16:42:06 +03:00
|
|
|
customConfig = loader.loadNconf({
|
2020-06-02 00:06:50 +03:00
|
|
|
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
|
|
|
|
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config')
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
customConfig.get('database:client').should.eql('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('argv is stronger than env parameter', function () {
|
2019-08-19 14:41:09 +03:00
|
|
|
process.env.database__client = 'test';
|
2017-02-02 15:46:30 +03:00
|
|
|
process.argv[2] = '--database:client=stronger';
|
|
|
|
|
2021-06-16 16:42:06 +03:00
|
|
|
customConfig = loader.loadNconf({
|
2020-06-02 00:06:50 +03:00
|
|
|
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
|
|
|
|
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config')
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
customConfig.get('database:client').should.eql('stronger');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('argv or env is NOT stronger than overrides', function () {
|
2019-08-19 14:41:09 +03:00
|
|
|
process.env.paths__corePath = 'try-to-override';
|
2017-02-02 15:46:30 +03:00
|
|
|
process.argv[2] = '--paths:corePath=try-to-override';
|
|
|
|
|
2021-06-16 16:42:06 +03:00
|
|
|
customConfig = loader.loadNconf({
|
2020-06-02 00:06:50 +03:00
|
|
|
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
|
|
|
|
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config')
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
customConfig.get('paths:corePath').should.not.containEql('try-to-override');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('overrides is stronger than every other config file', function () {
|
2021-06-16 16:42:06 +03:00
|
|
|
customConfig = loader.loadNconf({
|
2020-06-02 00:06:50 +03:00
|
|
|
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
|
|
|
|
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config')
|
2017-02-02 15:46:30 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
customConfig.get('paths:corePath').should.not.containEql('try-to-override');
|
|
|
|
customConfig.get('database:client').should.eql('sqlite3');
|
|
|
|
customConfig.get('database:connection:filename').should.eql('/hehe.db');
|
|
|
|
customConfig.get('database:debug').should.eql(true);
|
|
|
|
customConfig.get('url').should.eql('http://localhost:2368');
|
|
|
|
customConfig.get('logging:level').should.eql('error');
|
|
|
|
customConfig.get('logging:transports').should.eql(['stdout']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-01-05 10:40:53 +04:00
|
|
|
describe('Index', function () {
|
2013-12-28 20:01:08 +04:00
|
|
|
it('should have exactly the right keys', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const pathConfig = configUtils.config.get('paths');
|
2013-12-28 20:01:08 +04:00
|
|
|
|
|
|
|
// This will fail if there are any extra keys
|
2021-03-25 03:12:43 +03:00
|
|
|
// NOTE: using `Object.keys` here instead of `should.have.keys` assertion
|
|
|
|
// because when `have.keys` fails there's no useful diff
|
|
|
|
// and it doesn't make sure to check for "extra" keys
|
|
|
|
Object.keys(pathConfig).should.eql([
|
2013-12-28 20:01:08 +04:00
|
|
|
'contentPath',
|
2021-11-16 16:03:20 +03:00
|
|
|
'fixtures',
|
2021-11-19 12:43:48 +03:00
|
|
|
'assetSrc',
|
2021-11-17 19:05:17 +03:00
|
|
|
'urlCache',
|
2021-03-25 03:12:43 +03:00
|
|
|
'appRoot',
|
2013-12-28 20:01:08 +04:00
|
|
|
'corePath',
|
2021-03-25 03:12:43 +03:00
|
|
|
'clientAssets',
|
2013-12-28 20:01:08 +04:00
|
|
|
'helperTemplates',
|
2021-03-25 03:12:43 +03:00
|
|
|
'adminViews',
|
|
|
|
'defaultViews',
|
|
|
|
'defaultSettings',
|
|
|
|
'internalAppPath',
|
|
|
|
'internalAdaptersPath',
|
|
|
|
'migrationPath',
|
|
|
|
'publicFilePath'
|
|
|
|
]);
|
2013-12-28 20:01:08 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have the correct values for each key', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const pathConfig = configUtils.config.get('paths');
|
2020-06-02 00:06:50 +03:00
|
|
|
const appRoot = path.resolve(__dirname, '../../../../');
|
2013-12-28 20:01:08 +04:00
|
|
|
|
|
|
|
pathConfig.should.have.property('appRoot', appRoot);
|
2016-01-11 14:37:59 +03:00
|
|
|
});
|
|
|
|
|
2014-02-08 19:41:15 +04:00
|
|
|
it('should allow specific properties to be user defined', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const contentPath = path.join(configUtils.config.get('paths').appRoot, 'otherContent', '/');
|
2014-01-05 10:40:53 +04:00
|
|
|
|
2016-09-13 23:24:57 +03:00
|
|
|
configUtils.set('paths:contentPath', contentPath);
|
2017-02-02 15:46:30 +03:00
|
|
|
configUtils.config.get('paths').should.have.property('contentPath', contentPath);
|
|
|
|
configUtils.config.getContentPath('images').should.eql(contentPath + 'images/');
|
2013-12-28 20:01:08 +04:00
|
|
|
});
|
|
|
|
});
|
2014-09-10 08:06:24 +04:00
|
|
|
});
|