mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
🎨 make sqlite filename absolute (#7585)
no issue
- add tests for makePathsAbsolute
- add support for windows paths
When Ghost-CLI inits the database of the current GhostVersion (in /current), then it uses knex-migrator to do that.
Knex migrator is reading the .knex-migrator file of the current Ghost version. This returns a relative path to the database location.
The problem: knex-migrator will init the database in the root folder of Ghost-CLI /content/data instead of /current/content . And when you start Ghost (ghost start), it always complains that
that database is not initialised, because it expects the database in /current/content...
* 🎨 move config_spec to config/index_spec
- add one more test case
This commit is contained in:
parent
af4534c74a
commit
fd0a08ae8c
4 changed files with 117 additions and 14 deletions
|
@ -30,8 +30,10 @@ nconf.file('ghost4', __dirname + '/defaults.json');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* transform all relative paths to absolute paths
|
* 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
|
* values we have to set manual
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
var path = require('path'),
|
var path = require('path'),
|
||||||
_ = require('lodash');
|
_ = require('lodash'),
|
||||||
|
errors = require('../errors');
|
||||||
|
|
||||||
exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
|
exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
|
||||||
if (!this.get('privacy')) {
|
if (!this.get('privacy')) {
|
||||||
|
@ -16,20 +17,36 @@ exports.isPrivacyDisabled = function isPrivacyDisabled(privacyFlag) {
|
||||||
/**
|
/**
|
||||||
* transform all relative paths to absolute paths
|
* transform all relative paths to absolute paths
|
||||||
* @TODO: imagesRelPath is a dirty little attribute (especially when looking at the usages)
|
* @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;
|
var self = this;
|
||||||
|
|
||||||
if (!paths && !parent) {
|
if (!obj) {
|
||||||
paths = this.get('paths');
|
throw new errors.IncorrectUsageError({
|
||||||
parent = 'paths';
|
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)) {
|
if (_.isObject(configValue)) {
|
||||||
makePathsAbsolute.bind(self)(configValue, parent + ':' + pathsKey);
|
makePathsAbsolute.bind(self)(configValue, parent + ':' + pathsKey);
|
||||||
} else {
|
} 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));
|
self.set(parent + ':' + pathsKey, path.join(__dirname + '/../../../', configValue));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ var should = require('should'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
|
|
||||||
testUtils = require('../utils'),
|
testUtils = require('../../utils'),
|
||||||
i18n = require('../../server/i18n'),
|
i18n = require('../../../server/i18n'),
|
||||||
utils = require('../../server/utils'),
|
utils = require('../../../server/utils'),
|
||||||
/*jshint unused:false*/
|
/*jshint unused:false*/
|
||||||
db = require('../../server/data/db/connection'),
|
db = require('../../../server/data/db/connection'),
|
||||||
|
|
||||||
// Thing we are testing
|
// Thing we are testing
|
||||||
configUtils = require('../utils/configUtils'),
|
configUtils = require('../../utils/configUtils'),
|
||||||
config = configUtils.config;
|
config = configUtils.config;
|
||||||
|
|
||||||
i18n.init();
|
i18n.init();
|
||||||
|
@ -106,9 +106,10 @@ describe('Config', function () {
|
||||||
|
|
||||||
it('should have the correct values for each key', function () {
|
it('should have the correct values for each key', function () {
|
||||||
var pathConfig = config.get('paths'),
|
var pathConfig = config.get('paths'),
|
||||||
appRoot = path.resolve(__dirname, '../../../');
|
appRoot = path.resolve(__dirname, '../../../../');
|
||||||
|
|
||||||
pathConfig.should.have.property('appRoot', appRoot);
|
pathConfig.should.have.property('appRoot', appRoot);
|
||||||
|
pathConfig.should.have.property('imagesRelPath', 'content/images');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should allow specific properties to be user defined', function () {
|
it('should allow specific properties to be user defined', function () {
|
83
core/test/unit/config/utils_spec.js
Normal file
83
core/test/unit/config/utils_spec.js
Normal file
|
@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue