Skip to content

Commit

Permalink
Add plugin loader test
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Dec 14, 2014
1 parent 83fe2dd commit 262fc82
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var should = require('chai').should();

describe('Hexo', function(){
//
});
1 change: 1 addition & 0 deletions test/scripts/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ describe('Core', function(){
require('./hexo');
require('./load_config');
require('./load_database');
require('./load_plugins');
require('./post');
require('./render');
require('./scaffold');
Expand Down
99 changes: 99 additions & 0 deletions test/scripts/hexo/load_plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var should = require('chai').should();
var fs = require('hexo-fs');
var pathFn = require('path');

describe('Load plugins', function(){
var Hexo = require('../../../lib/hexo');
var hexo = new Hexo(pathFn.join(__dirname, 'plugin_test'), {silent: true});
var loadPlugins = require('../../../lib/hexo/load_plugins');

var script = [
'hexo._script_test = {',
' filename: __filename,',
' dirname: __dirname,',
' module: module,',
' require: require',
'}'
].join('\n');

function validate(path){
var result = hexo._script_test;

result.filename = path;
result.dirname = pathFn.dirname(path);
result.module.id = path;
result.module.filename = path;

delete hexo._script_test;
}

hexo.env.init = true;
hexo.theme_script_dir = pathFn.join(hexo.base_dir, 'themes', 'test', 'scripts');

before(function(){
return fs.mkdir(hexo.base_dir);
});

after(function(){
return fs.rmdir(hexo.base_dir);
});

it('load plugins', function(){
var path = pathFn.join(hexo.plugin_dir, 'hexo-plugin-test', 'index.js');

return fs.writeFile(path, script).then(function(){
return loadPlugins(hexo);
}).then(function(){
validate(path);
return fs.unlink(path);
});
});

it('ignore plugins whose name is not started with "hexo-"', function(){
var script = 'hexo._script_test = true';
var path = pathFn.join(hexo.plugin_dir, 'another-plugin', 'index.js');

return fs.writeFile(path, script).then(function(){
return loadPlugins(hexo);
}).then(function(){
should.not.exist(hexo._script_test);
return fs.unlink(path);
});
});

it('load scripts', function(){
var path = pathFn.join(hexo.script_dir, 'test.js');

return fs.writeFile(path, script).then(function(){
return loadPlugins(hexo);
}).then(function(){
validate(path);
return fs.unlink(path);
});
});

it('load theme scripts', function(){
var path = pathFn.join(hexo.theme_script_dir, 'test.js');

return fs.writeFile(path, script).then(function(){
return loadPlugins(hexo);
}).then(function(){
validate(path);
return fs.unlink(path);
});
});

it('don\'t load plugins in safe mode', function(){
var script = 'hexo._script_test = true';
var path = pathFn.join(hexo.script_dir, 'test.js');

return fs.writeFile(path, script).then(function(){
hexo.env.safe = true;
return loadPlugins(hexo);
}).then(function(){
hexo.env.safe = false;
should.not.exist(hexo._script_test);
return fs.unlink(path);
});
});
});

0 comments on commit 262fc82

Please sign in to comment.