Skip to content

Commit

Permalink
Add tests for config console
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Feb 13, 2016
1 parent f4acfb8 commit 65031b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/plugins/console/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var yaml = require('js-yaml');
var fs = require('hexo-fs');
var pathFn = require('path');
var Promise = require('bluebird');

function configConsole(args) {
var key = args._[0];
Expand All @@ -11,13 +12,13 @@ function configConsole(args) {

if (!key) {
console.log(this.config);
return;
return Promise.resolve();
}

if (!value) {
value = getProperty(this.config, key);
if (value) console.log(value);
return;
return Promise.resolve();
}

var configPath = this.config_path;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"jscs": "^2.9.0",
"jscs-preset-hexo": "^1.0.1",
"mocha": "^2.4.5",
"rewire": "^2.5.1",
"sinon": "^1.17.3"
}
}
51 changes: 49 additions & 2 deletions test/scripts/console/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ var fs = require('hexo-fs');
var pathFn = require('path');
var yaml = require('js-yaml');
var _ = require('lodash');
var rewire = require('rewire');
var sinon = require('sinon');

describe('config', function() {
var Hexo = require('../../../lib/hexo');
var hexo = new Hexo(pathFn.join(__dirname, 'config_test'), {silent: true});
var config = require('../../../lib/plugins/console/config').bind(hexo);
var configModule = rewire('../../../lib/plugins/console/config');

before(function() {
return fs.mkdirs(hexo.base_dir).then(function() {
Expand All @@ -25,9 +28,53 @@ describe('config', function() {
return fs.rmdir(hexo.base_dir);
});

it('read all config');
it('read all config', function() {
var spy = sinon.spy();

it('read config');
return configModule.__with__({
console: {
log: spy
}
})(function() {
return configModule.call(hexo, {_: []});
}).then(function() {
spy.args[0][0].should.eql(hexo.config);
});
});

it('read config', function() {
var spy = sinon.spy();

return configModule.__with__({
console: {
log: spy
}
})(function() {
return configModule.call(hexo, {_: ['title']});
}).then(function() {
spy.args[0][0].should.eql(hexo.config.title);
});
});

it('read nested config', function() {
var spy = sinon.spy();

hexo.config.server = {
port: 12345
};

return configModule.__with__({
console: {
log: spy
}
})(function() {
return configModule.call(hexo, {_: ['server.port']});
}).then(function() {
spy.args[0][0].should.eql(hexo.config.server.port);
}).finally(function() {
delete hexo.config.server;
});
});

function writeConfig() {
var args = _.toArray(arguments);
Expand Down

0 comments on commit 65031b7

Please sign in to comment.