0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/test/unit/services/settings/loader_spec.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

107 lines
4.1 KiB
JavaScript

const sinon = require('sinon');
const should = require('should');
const rewire = require('rewire');
const fs = require('fs-extra');
const path = require('path');
const configUtils = require('../../../utils/configUtils');
const common = require('../../../../core/server/lib/common');
const loadSettings = rewire('../../../../core/frontend/services/settings/loader');
describe('UNIT > Settings Service loader:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
});
afterEach(function () {
sinon.restore();
configUtils.restore();
});
describe('Settings Loader', function () {
const yamlStubFile = {
routes: null,
collections: {
'/': {
permalink: '/{slug}/',
template: ['home', 'index']
}
},
taxonomies: {tag: '/tag/{slug}/', author: '/author/{slug}/'}
};
let yamlParserStub;
let validateStub;
beforeEach(function () {
yamlParserStub = sinon.stub();
validateStub = sinon.stub();
});
it('can find yaml settings file and returns a settings object', function () {
const fsReadFileSpy = sinon.spy(fs, 'readFileSync');
const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/goodroutes.yaml');
yamlParserStub.returns(yamlStubFile);
validateStub.returns({routes: {}, collections: {}, taxonomies: {}});
loadSettings.__set__('yamlParser', yamlParserStub);
loadSettings.__set__('validate', validateStub);
const setting = loadSettings('goodroutes');
should.exist(setting);
setting.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies');
// There are 4 files in the fixtures folder, but only 1 supported and valid yaml files
fsReadFileSpy.calledOnce.should.be.true();
fsReadFileSpy.calledWith(expectedSettingsFile).should.be.true();
yamlParserStub.callCount.should.be.eql(1);
});
it('can handle errors from YAML parser', function (done) {
yamlParserStub.throws(new common.errors.GhostError({
message: 'could not parse yaml file',
context: 'bad indentation of a mapping entry at line 5, column 10'
}));
loadSettings.__set__('yamlParser', yamlParserStub);
try {
loadSettings('goodroutes');
done(new Error('Loader should fail'));
} catch (err) {
should.exist(err);
err.message.should.be.eql('could not parse yaml file');
err.context.should.be.eql('bad indentation of a mapping entry at line 5, column 10');
yamlParserStub.calledOnce.should.be.true();
done();
}
});
it('throws error if file can\'t be accessed', function (done) {
const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml');
const fsError = new Error('no permission');
fsError.code = 'EPERM';
const originalFn = fs.readFileSync;
const fsReadFileStub = sinon.stub(fs, 'readFileSync').callsFake(function (filePath, options) {
if (filePath.match(/routes\.yaml/)) {
throw fsError;
}
return originalFn(filePath, options);
});
yamlParserStub = sinon.spy();
loadSettings.__set__('yamlParser', yamlParserStub);
try {
loadSettings('routes');
done(new Error('Loader should fail'));
} catch (err) {
err.message.should.match(/Error trying to load YAML setting for routes from/);
fsReadFileStub.calledWith(expectedSettingsFile).should.be.true();
yamlParserStub.calledOnce.should.be.false();
done();
}
});
});
});