Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): validate config after load_config #4381

Merged
merged 5 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class Hexo extends EventEmitter {
return Promise.each([
'update_package', // Update package.json
'load_config', // Load config
'validate_config', // Validate config
'load_theme_config', // Load alternate theme config
'load_plugins' // Load external plugins & scripts
], name => require(`./${name}`)(this)).then(() => this.execFilter('after_init', null, {context: this})).then(() => {
Expand Down
33 changes: 33 additions & 0 deletions lib/hexo/validate_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = ctx => {
const { config, log } = ctx;

log.info('Validating config');

// Validation for config.url && config.root
if (typeof config.url !== 'string') {
throw new TypeError('_config.yml validation failed, "url" should be string!');
}
if (config.url.trim().length <= 0) {
throw new TypeError('_config.yml validation failed, "url" should not be empty!');
}

if (typeof config.root !== 'string') {
throw new TypeError('_config.yml validation failed, "root" should be string!');
}
if (config.root.trim().length <= 0) {
throw new TypeError('_config.yml validation failed, "root" should not be empty!');
}

// Soft deprecate use_date_for_updated
if (typeof config.use_date_for_updated === 'boolean') {
log.warn('_config.yml validation: "use_date_for_updated" is deprecated, please use "updated_option" instead. See https://hexo.io/docs/configuration for more details.');
}

// Soft deprecate external_link Boolean
if (typeof config.external_link === 'boolean') {
log.warn('_config.yml validation: "external_link" with a Boolean value is deprecated. See https://hexo.io/docs/configuration for more details.');
}
};

1 change: 1 addition & 0 deletions test/scripts/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe('Core', () => {
require('./hexo');
require('./load_config');
require('./validate_config');
require('./load_database');
require('./load_plugins');
require('./load_theme_config');
Expand Down
102 changes: 102 additions & 0 deletions test/scripts/hexo/validate_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
'use strict';

const { spy } = require('sinon');

describe('Validate config', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const validateConfig = require('../../../lib/hexo/validate_config');
const defaultConfig = require('../../../lib/hexo/default_config');
let logSpy;

beforeEach(() => {
logSpy = spy();
hexo.config = JSON.parse(JSON.stringify(defaultConfig));
hexo.log.warn = logSpy;
hexo.log.info = spy();
});

it('config.url - undefined', () => {
delete hexo.config.url;

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "url" should be string!');
}
});

it('config.url - wrong type', () => {
hexo.config.url = true;

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "url" should be string!');
}
});

it('config.url - empty', () => {
hexo.config.url = ' ';

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "url" should not be empty!');
}
});

it('config.root - undefined', () => {
delete hexo.config.root;

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "root" should be string!');
}
});

it('config.root - wrong type', () => {
hexo.config.root = true;

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "root" should be string!');
}
});

it('config.root - empty', () => {
hexo.config.root = ' ';

try {
validateConfig(hexo);
} catch (e) {
e.name.should.eql('TypeError');
e.message.should.eql('_config.yml validation failed, "root" should not be empty!');
}
});

it('config.use_date_for_updated - depreacte', () => {
hexo.config.use_date_for_updated = true;

validateConfig(hexo);

logSpy.calledOnce.should.be.true;
logSpy.calledWith('_config.yml validation: "use_date_for_updated" is deprecated, please use "updated_option" instead. See https://hexo.io/docs/configuration for more details.').should.be.true;
});

it('config.external_link - depreacte Boolean value', () => {
hexo.config.external_link = false;

validateConfig(hexo);

logSpy.calledOnce.should.be.true;
logSpy.calledWith('_config.yml validation: "external_link" with a Boolean value is deprecated. See https://hexo.io/docs/configuration for more details.').should.be.true;
});
});