Skip to content

Commit

Permalink
Merge config instead of extend
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Dec 6, 2015
1 parent 8cca1f9 commit 61941e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Hexo(base, args) {
tag: new extend.Tag()
};

this.config = _.clone(defaultConfig);
this.config = _.cloneDeep(defaultConfig);

this.log = createLogger(this.env);

Expand Down
2 changes: 1 addition & 1 deletion lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function(ctx) {

ctx.log.debug('Config loaded: %s', chalk.magenta(tildify(configPath)));

config = _.extend(ctx.config, config);
config = _.merge(ctx.config, config);
ctx.config_path = configPath;

config.root = config.root.replace(/\/*$/, '/');
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/filters/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Backtick code block', function() {

beforeEach(function() {
// Reset config
hexo.config.highlight = _.clone(defaultConfig.highlight);
hexo.config.highlight = _.cloneDeep(defaultConfig.highlight);
});

it('disabled', function() {
Expand Down
49 changes: 28 additions & 21 deletions test/scripts/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ describe('Load config', function() {

hexo.env.init = true;

function reset() {
hexo.config = _.clone(defaultConfig);
}

before(function() {
return fs.mkdirs(hexo.base_dir).then(function() {
return hexo.init();
Expand All @@ -27,6 +23,10 @@ describe('Load config', function() {
return fs.rmdir(hexo.base_dir);
});

beforeEach(function() {
hexo.config = _.cloneDeep(defaultConfig);
});

it('config file does not exist', function() {
return loadConfig(hexo).then(function() {
hexo.config.should.eql(defaultConfig);
Expand All @@ -40,8 +40,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.config.foo.should.eql(1);

reset();
}).finally(function() {
return fs.unlink(configPath);
});
});
Expand All @@ -53,8 +52,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.config.baz.should.eql(3);

reset();
}).finally(function() {
return fs.unlink(configPath);
});
});
Expand All @@ -66,8 +64,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.config.should.eql(defaultConfig);

reset();
}).finally(function() {
return fs.unlink(configPath);
});
});
Expand All @@ -79,8 +76,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.config.foo.should.eql(1);

reset();
}).finally(function() {
hexo.config_path = pathFn.join(hexo.base_dir, '_config.yml');
return fs.unlink(configPath);
});
Expand All @@ -95,8 +91,7 @@ describe('Load config', function() {
}).then(function() {
hexo.config.foo.should.eql(2);
hexo.config_path.should.eql(realPath);

reset();
}).finally(function() {
hexo.config_path = pathFn.join(hexo.base_dir, '_config.yml');
return fs.unlink(realPath);
});
Expand All @@ -113,8 +108,7 @@ describe('Load config', function() {
}).then(function() {
hexo.config.root.should.eql('foo/');
hexo.config.url.should.eql('http://hexo.io');

reset();
}).finally(function() {
return fs.unlink(hexo.config_path);
});
});
Expand All @@ -124,8 +118,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.public_dir.should.eql(pathFn.resolve(hexo.base_dir, 'foo') + pathFn.sep);

reset();
}).finally(function() {
return fs.unlink(hexo.config_path);
});
});
Expand All @@ -135,8 +128,7 @@ describe('Load config', function() {
return loadConfig(hexo);
}).then(function() {
hexo.source_dir.should.eql(pathFn.resolve(hexo.base_dir, 'bar') + pathFn.sep);

reset();
}).finally(function() {
return fs.unlink(hexo.config_path);
});
});
Expand All @@ -149,8 +141,23 @@ describe('Load config', function() {
hexo.theme_dir.should.eql(pathFn.join(hexo.base_dir, 'themes', 'test') + pathFn.sep);
hexo.theme_script_dir.should.eql(pathFn.join(hexo.theme_dir, 'scripts') + pathFn.sep);
hexo.theme.base.should.eql(hexo.theme_dir);
}).finally(function() {
return fs.unlink(hexo.config_path);
});
});

it('merge config', function() {
var content = [
'highlight:',
' tab_replace: yoooo'
].join('\n');

reset();
return fs.writeFile(hexo.config_path, content).then(function() {
return loadConfig(hexo);
}).then(function() {
hexo.config.highlight.enable.should.be.true;
hexo.config.highlight.tab_replace.should.eql('yoooo');
}).finally(function() {
return fs.unlink(hexo.config_path);
});
});
Expand Down

0 comments on commit 61941e7

Please sign in to comment.