Merge pull request #1446 from ether/allow-plugins-to-specify-specs

Allow plugins to specify specs
This commit is contained in:
John McLear 2013-02-06 15:43:39 -08:00
commit 488ebc20a4
2 changed files with 48 additions and 7 deletions

View file

@ -1,14 +1,26 @@
var path = require("path")
, npm = require("npm")
, fs = require("fs");
, fs = require("fs")
, async = require("async");
exports.expressCreateServer = function (hook_name, args, cb) {
args.app.get('/tests/frontend/specs_list.js', function(req, res){
fs.readdir('tests/frontend/specs', function(err, files){
if(err){ return res.send(500); }
res.send("var specs_list = " + JSON.stringify(files.sort()) + ";\n");
async.parallel({
coreSpecs: function(callback){
exports.getCoreTests(callback);
},
pluginSpecs: function(callback){
exports.getPluginTests(callback);
}
},
function(err, results){
var files = results.coreSpecs; // push the core specs to a file object
files = files.concat(results.pluginSpecs); // add the plugin Specs to the core specs
console.debug("Sent browser the following test specs:", files.sort());
res.send("var specs_list = " + JSON.stringify(files.sort()) + ";\n");
});
});
var url2FilePath = function(url){
@ -44,4 +56,29 @@ exports.expressCreateServer = function (hook_name, args, cb) {
args.app.get('/tests/frontend', function (req, res) {
res.redirect('/tests/frontend/');
});
}
}
exports.getPluginTests = function(callback){
var pluginSpecs = [];
var plugins = fs.readdirSync('node_modules');
plugins.forEach(function(plugin){
if(fs.existsSync("node_modules/"+plugin+"/static/tests/frontend/specs")){ // if plugins exists
var specFiles = fs.readdirSync("node_modules/"+plugin+"/static/tests/frontend/specs/");
async.forEach(specFiles, function(spec){ // for each specFile push it to pluginSpecs
pluginSpecs.push("/static/plugins/"+plugin+"/static/tests/frontend/specs/" + spec);
},
function(err){
// blow up if something bad happens!
});
}
});
callback(null, pluginSpecs);
}
exports.getCoreTests = function(callback){
fs.readdir('tests/frontend/specs', function(err, coreSpecs){ // get the core test specs
if(err){ return res.send(500); }
callback(null, coreSpecs);
});
}

View file

@ -179,7 +179,11 @@ $(function(){
//inject spec scripts into the dom
var $body = $('body');
$.each(specs, function(i, spec){
$body.append('<script src="specs/' + spec + '"></script>')
if(spec[0] != "/"){ // if the spec isn't a plugin spec which means the spec file might be in a different subfolder
$body.append('<script src="specs/' + spec + '"></script>')
}else{
$body.append('<script src="' + spec + '"></script>')
}
});
//initalize the test helper
@ -196,4 +200,4 @@ $(function(){
mocha.run();
});
});
});