0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Extracted yarml persed dep out of settings loader

refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- Moving internal dependencies to be injected through constructor DI for better testability. This is first step of few more to follow. Not doing it all at once as there's too many thing failing when doing a bulk refactor
This commit is contained in:
Naz 2021-09-30 14:25:36 +02:00
parent 96d075c47d
commit 1ac0ba07de
3 changed files with 23 additions and 14 deletions

View file

@ -11,8 +11,9 @@ const tpl = require('@tryghost/tpl');
const config = require('../../../shared/config');
const bridge = require('../../../bridge');
const SettingsLoader = require('./settings-loader');
const parseYaml = require('./yaml-parser');
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({parseYaml});
const messages = {
loadError: 'Could not load {filename} file.'

View file

@ -4,7 +4,6 @@ const debug = require('@tryghost/debug')('frontend:services:settings:settings-lo
const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors');
const config = require('../../../shared/config');
const yamlParser = require('./yaml-parser');
const validate = require('./validate');
const messages = {
@ -12,8 +11,12 @@ const messages = {
};
class SettingsLoader {
constructor() {
/**
* @param {Object} options
* @param {Function} options.parseYaml yaml parser
*/
constructor({parseYaml}) {
this.parseYaml = parseYaml;
}
/**
@ -48,7 +51,7 @@ class SettingsLoader {
const file = await fs.readFile(filePath, 'utf8');
debug('settings file found for', setting);
const object = yamlParser(file, fileName);
const object = this.parseYaml(file, fileName);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {
@ -64,7 +67,7 @@ class SettingsLoader {
err: err
});
}
};
}
/**
* Reads the routes.yaml settings file and passes the
@ -80,7 +83,7 @@ class SettingsLoader {
const file = fs.readFileSync(filePath, 'utf8');
debug('settings file found for', setting);
const object = yamlParser(file, fileName);
const object = this.parseYaml(file, fileName);
return validate(object);
} catch (err) {
if (errors.utils.isIgnitionError(err)) {

View file

@ -38,7 +38,9 @@ describe('UNIT > SettingsLoader:', function () {
});
it('reads a settings object for routes.yaml file', function () {
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub
});
const settingsStubFile = {
routes: null,
collections: {
@ -67,10 +69,11 @@ describe('UNIT > SettingsLoader:', function () {
yamlParserStub.returns(yamlStubFile);
validateStub.returns({routes: {}, collections: {}, taxonomies: {}});
SettingsLoader.__set__('yamlParser', yamlParserStub);
SettingsLoader.__set__('validate', validateStub);
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub
});
const setting = settingsLoader.loadSettingsSync();
should.exist(setting);
setting.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies');
@ -81,13 +84,14 @@ describe('UNIT > SettingsLoader:', function () {
});
it('can handle errors from YAML parser', function (done) {
SettingsLoader.__set__('yamlParser', yamlParserStub);
yamlParserStub.throws(new errors.GhostError({
message: 'could not parse yaml file',
context: 'bad indentation of a mapping entry at line 5, column 10'
}));
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub
});
try {
settingsLoader.loadSettingsSync();
done(new Error('Loader should fail'));
@ -114,10 +118,11 @@ describe('UNIT > SettingsLoader:', function () {
return originalFn(filePath, options);
});
SettingsLoader.__set__('yamlParser', yamlParserStub);
yamlParserStub = sinon.spy();
const settingsLoader = new SettingsLoader();
const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub
});
try {
settingsLoader.loadSettingsSync();