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/ensure-settings_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

84 lines
4 KiB
JavaScript

const sinon = require('sinon');
const should = require('should');
const fs = require('fs-extra');
const yaml = require('js-yaml');
const path = require('path');
const configUtils = require('../../../utils/configUtils');
const common = require('../../../../core/server/lib/common');
const ensureSettings = require('../../../../core/frontend/services/settings/ensure-settings');
describe('UNIT > Settings Service ensure settings:', function () {
beforeEach(function () {
configUtils.set('paths:contentPath', path.join(__dirname, '../../../utils/fixtures/'));
sinon.stub(fs, 'readFile');
sinon.stub(fs, 'copy');
});
afterEach(function () {
sinon.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');
return ensureSettings(['goodroutes', 'badroutes']).then(() => {
fs.readFile.callCount.should.be.eql(2);
fs.copy.called.should.be.false();
});
});
it('copies default settings file if not found but does not overwrite existing files', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../core/frontend/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();
return ensureSettings(['routes', 'globals'])
.then(() => {
fs.readFile.calledTwice.should.be.true();
fs.copy.calledOnce.should.be.true();
});
});
it('copies default settings file if no file found', function () {
const expectedDefaultSettingsPath = path.join(__dirname, '../../../../core/frontend/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();
return ensureSettings(['routes']).then(() => {
fs.readFile.calledOnce.should.be.true();
fs.copy.calledOnce.should.be.true();
});
});
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();
});
});
});
});