Ghost/core/test/unit/services/settings/ensure-settings_spec.js

190 lines
8.2 KiB
JavaScript
Raw Normal View History

YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
const sinon = require('sinon'),
should = require('should'),
fs = require('fs-extra'),
yaml = require('js-yaml'),
path = require('path'),
configUtils = require('../../../utils/configUtils'),
common = require('../../../../server/lib/common'),
ensureSettings = require('../../../../server/services/settings/ensure-settings'),
sandbox = sinon.sandbox.create();
describe('UNIT > Settings Service:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
sandbox.stub(fs, 'readFile');
sandbox.stub(fs, 'copy');
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
});
afterEach(function () {
sandbox.restore();
configUtils.restore();
});
describe('Ensure settings files', function () {
it('returns yaml file from settings folder if it exists', function () {
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/badroutes.yaml'), 'utf8').resolves('content');
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/goodroutes.yaml'), 'utf8').resolves('content');
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
return ensureSettings(['goodroutes', 'badroutes']).then(() => {
fs.readFile.callCount.should.be.eql(2);
fs.copy.called.should.be.false();
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
});
});
it('copies default settings file if not found but does not overwrite existing files', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-globals.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/globals.yaml');
const fsError = new Error('not found');
fsError.code = 'ENOENT';
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').resolves('content');
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/globals.yaml'), 'utf8').rejects(fsError);
fs.copy.withArgs(expectedDefaultSettingsPath, expectedContentPath).resolves();
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
return ensureSettings(['routes', 'globals'])
.then(() => {
fs.readFile.calledTwice.should.be.true();
fs.copy.calledOnce.should.be.true();
});
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
});
it('copies default settings file if no file found', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-routes.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
const fsError = new Error('not found');
fsError.code = 'ENOENT';
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').rejects(fsError);
fs.copy.withArgs(expectedDefaultSettingsPath, expectedContentPath).resolves();
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
return ensureSettings(['routes']).then(() => {
fs.readFile.calledOnce.should.be.true();
fs.copy.calledOnce.should.be.true();
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
});
});
it('rejects, if error is not a not found error', function () {
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/');
const fsError = new Error('no permission');
fsError.code = 'EPERM';
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').rejects(fsError);
return ensureSettings(['routes'])
.then(()=> {
throw new Error('Expected test to fail');
})
.catch((error) => {
should.exist(error);
error.message.should.be.eql(`Error trying to access settings files in ${expectedContentPath}.`);
fs.readFile.calledOnce.should.be.true();
fs.copy.called.should.be.false();
});
});
});
describe('Migrations: home.hbs', function () {
it('routes.yaml has modifications', function () {
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').resolves('' +
'routes:\n' +
'\n' +
'collections:\n' +
' /:\n' +
' permalink: \'{globals.permalinks}\' # special 1.0 compatibility setting. See the docs for details.\n' +
' template:\n' +
' - index\n' +
'\n' +
'taxonomies:\n' +
' tag: /tag/{slug}/\n' +
' author: /author/{slug}/' + '' +
'\n'
);
return ensureSettings(['routes']).then(() => {
fs.readFile.callCount.should.be.eql(1);
fs.copy.called.should.be.false();
});
});
it('routes.yaml is old routes.yaml', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-routes.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').resolves('' +
'routes:\n' +
'\n' +
'collections:\n' +
' /:\n' +
' permalink: \'{globals.permalinks}\' # special 1.0 compatibility setting. See the docs for details.\n' +
' template:\n' +
' - home\n' +
' - index\n' +
'\n' +
'taxonomies:\n' +
' tag: /tag/{slug}/\n' +
' author: /author/{slug}/' +
'\n'
);
fs.copy.withArgs(expectedDefaultSettingsPath, expectedContentPath).resolves();
return ensureSettings(['routes']).then(() => {
fs.readFile.callCount.should.be.eql(1);
fs.copy.called.should.be.true();
});
});
it('routes.yaml is old routes.yaml', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../server/services/settings/default-routes.yaml');
const expectedContentPath = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').resolves('' +
'routes:\n' +
'\n\n' +
'collections:\n' +
' /:\n' +
' permalink: \'{globals.permalinks}\' # special 1.0 compatibility setting. See the docs for details.\n' +
' template:\n' +
' - home\n' +
' - index\n' +
'\n\r' +
'taxonomies: \n' +
' tag: /tag/{slug}/\n' +
' author: /author/{slug}/' +
'\t' +
'\n'
);
fs.copy.withArgs(expectedDefaultSettingsPath, expectedContentPath).resolves();
return ensureSettings(['routes']).then(() => {
fs.readFile.callCount.should.be.eql(1);
fs.copy.called.should.be.true();
});
});
it('routes.yaml has modifications, do not replace', function () {
fs.readFile.withArgs(path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'), 'utf8').resolves('' +
'routes:\n' +
' /about/: about' +
'\n' +
'collections:\n' +
' /:\n' +
' permalink: \'{globals.permalinks}\' # special 1.0 compatibility setting. See the docs for details.\n' +
'\n' +
'taxonomies:\n' +
' tag: /categories/{slug}/\n' +
' author: /author/{slug}/' + '' +
'\n'
);
return ensureSettings(['routes']).then(() => {
fs.readFile.callCount.should.be.eql(1);
fs.copy.called.should.be.false();
YAML settings loader and parser closes #9528 These code changes introduce a YAML parser which will load and parse YAML files from the `/content/settings` directory. There are three major parts involved: 1. `ensure-settings.js`: this fn takes care that on bootstrap, the supported files are present in the `/content/settings` directory. If the files are not present, they get copied back from our default files. The default files to copy from are located in `core/server/services/settings`. 2. `loader.js`: the settings loader reads the requested `yaml` file from the disk and passes it to the yaml parser, which returns a `json` object of the file. The settings loader throws an error, if the file is not accessible, e. g. because of permission errors. 3. `yaml-parser`: gets passed a `yaml` file and returns a `json` object. If the file is not parseable, it returns a clear error that contains the information, what and where the parsing error occurred (e. g. line number and reason). - added a `get()` fn to settings services, that returns the settings object that's asked for. e. g. `settings.get('routes').then(()...` will return the `routes` settings. - added a `getAll()` fn to settings services, that returns all available settings in an object. The object looks like: `{routes: {routes: {}, collections: {}, resources: {}}, globals: {value: {}}`, assuming that we have to supported settings `routes` and `globals`. Further additions: - config `contentPath` for `settings` - config overrides for default `yaml` files location in `/core/server/services/settings` **Important**: These code changes are in preparation for Dynamic Routing and not yet used. The process of copying the supported `yaml` files (in this first step, the `routes.yaml` file) is not yet activated.
2018-04-13 03:34:03 +02:00
});
});
});
});