diff --git a/core/server/config/index.js b/core/server/config/index.js index 1f29f6fae4..7b1195e4da 100644 --- a/core/server/config/index.js +++ b/core/server/config/index.js @@ -30,8 +30,10 @@ nconf.file('ghost4', __dirname + '/defaults.json'); /** * transform all relative paths to absolute paths + * transform sqlite filename path for Ghost-CLI */ -localUtils.makePathsAbsolute.bind(nconf)(); +localUtils.makePathsAbsolute.bind(nconf)(nconf.get('paths'), 'paths'); +localUtils.makePathsAbsolute.bind(nconf)(nconf.get('database:connection'), 'database:connection'); /** * values we have to set manual diff --git a/core/server/config/utils.js b/core/server/config/utils.js index 0d332ca051..76f749aeed 100644 --- a/core/server/config/utils.js +++ b/core/server/config/utils.js @@ -1,5 +1,6 @@ var path = require('path'), - _ = require('lodash'); + _ = require('lodash'), + errors = require('../errors'); exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) { if (!this.get('privacy')) { @@ -16,20 +17,36 @@ exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) { /** * transform all relative paths to absolute paths * @TODO: imagesRelPath is a dirty little attribute (especially when looking at the usages) + * @TODO: re-write this function a little bit so we don't have to add the parent path - that is hard to understand + * + * Path must be string. + * Path must match minimum one / or \ + * Path can be a "." to re-present current folder */ -exports.makePathsAbsolute = function makePathsAbsolute(paths, parent) { +exports.makePathsAbsolute = function makePathsAbsolute(obj, parent) { var self = this; - if (!paths && !parent) { - paths = this.get('paths'); - parent = 'paths'; + if (!obj) { + throw new errors.IncorrectUsageError({ + message: 'makePathsAbsolute: Object is missing.' + }); } - _.each(paths, function (configValue, pathsKey) { + if (!parent) { + throw new errors.IncorrectUsageError({ + message: 'makePathsAbsolute: Parent is missing.' + }); + } + + _.each(obj, function (configValue, pathsKey) { if (_.isObject(configValue)) { makePathsAbsolute.bind(self)(configValue, parent + ':' + pathsKey); } else { - if (configValue[0] !== '/' && pathsKey !== 'imagesRelPath') { + if (_.isString(configValue) && + (configValue.match(/\/+|\\+/) || configValue === '.') && + (configValue[0] !== '/' && configValue[0] !== '\\') && + pathsKey !== 'imagesRelPath' + ) { self.set(parent + ':' + pathsKey, path.join(__dirname + '/../../../', configValue)); } } diff --git a/core/test/unit/config_spec.js b/core/test/unit/config/index_spec.js similarity index 93% rename from core/test/unit/config_spec.js rename to core/test/unit/config/index_spec.js index 153523fb31..bbfc74897c 100644 --- a/core/test/unit/config_spec.js +++ b/core/test/unit/config/index_spec.js @@ -6,14 +6,14 @@ var should = require('should'), fs = require('fs'), _ = require('lodash'), - testUtils = require('../utils'), - i18n = require('../../server/i18n'), - utils = require('../../server/utils'), + testUtils = require('../../utils'), + i18n = require('../../../server/i18n'), + utils = require('../../../server/utils'), /*jshint unused:false*/ - db = require('../../server/data/db/connection'), + db = require('../../../server/data/db/connection'), // Thing we are testing - configUtils = require('../utils/configUtils'), + configUtils = require('../../utils/configUtils'), config = configUtils.config; i18n.init(); @@ -106,9 +106,10 @@ describe('Config', function () { it('should have the correct values for each key', function () { var pathConfig = config.get('paths'), - appRoot = path.resolve(__dirname, '../../../'); + appRoot = path.resolve(__dirname, '../../../../'); pathConfig.should.have.property('appRoot', appRoot); + pathConfig.should.have.property('imagesRelPath', 'content/images'); }); it('should allow specific properties to be user defined', function () { diff --git a/core/test/unit/config/utils_spec.js b/core/test/unit/config/utils_spec.js new file mode 100644 index 0000000000..b07f3a96bf --- /dev/null +++ b/core/test/unit/config/utils_spec.js @@ -0,0 +1,83 @@ +var configUtils = require('../../../server/config/utils'), + should = require('should'); + +should.equal(true, true); + +describe('UNIT: Config utils', function () { + describe('makePathsAbsolute', function () { + it('ensure we change paths only', function () { + var changedKey = [], + obj = { + database: { + client: 'mysql', + connection: { + filename: 'content/data/ghost.db' + } + } + }; + + this.set = function (key, value) { + changedKey.push([key, value]); + }; + + configUtils.makePathsAbsolute.bind(this)(obj.database, 'database'); + + changedKey.length.should.eql(1); + changedKey[0][0].should.eql('database:connection:filename'); + changedKey[0][1].should.not.eql('content/data/ghost.db'); + }); + + it('ensure it skips non strings', function () { + var changedKey = [], + obj = { + database: { + test: 10 + } + }; + + this.set = function (key, value) { + changedKey.push([key, value]); + }; + + configUtils.makePathsAbsolute.bind(this)(obj.database, 'database'); + changedKey.length.should.eql(0); + }); + + it('ensure we don\' change absolute paths', function () { + var changedKey = [], + obj = { + database: { + client: 'mysql', + connection: { + filename: '/content/data/ghost.db' + } + } + }; + + this.set = function (key, value) { + changedKey.push([key, value]); + }; + + configUtils.makePathsAbsolute.bind(this)(obj.database, 'database'); + changedKey.length.should.eql(0); + }); + + it('match paths on windows', function () { + var changedKey = [], + obj = { + database: { + filename: 'content\\data\\ghost.db' + } + }; + + this.set = function (key, value) { + changedKey.push([key, value]); + }; + + configUtils.makePathsAbsolute.bind(this)(obj.database, 'database'); + changedKey.length.should.eql(1); + changedKey[0][0].should.eql('database:filename'); + changedKey[0][1].should.not.eql('content\\data\\ghost.db'); + }); + }); +});