2012-11-17 16:28:54 +01:00
|
|
|
var path = require("path")
|
|
|
|
, npm = require("npm")
|
2013-02-04 01:00:39 +01:00
|
|
|
, fs = require("fs")
|
2019-01-23 17:21:40 +01:00
|
|
|
, util = require("util");
|
2012-10-02 01:35:43 +02:00
|
|
|
|
|
|
|
exports.expressCreateServer = function (hook_name, args, cb) {
|
2019-01-23 17:21:40 +01:00
|
|
|
args.app.get('/tests/frontend/specs_list.js', async function(req, res) {
|
|
|
|
let [coreTests, pluginTests] = await Promise.all([
|
|
|
|
exports.getCoreTests(),
|
|
|
|
exports.getPluginTests()
|
|
|
|
]);
|
|
|
|
|
|
|
|
// merge the two sets of results
|
|
|
|
let files = [].concat(coreTests, pluginTests).sort();
|
2020-03-22 18:41:57 +01:00
|
|
|
|
|
|
|
// Remove swap files from tests
|
|
|
|
files = files.filter(el => !/\.swp$/.test(el))
|
|
|
|
|
2019-01-23 17:21:40 +01:00
|
|
|
console.debug("Sent browser the following test specs:", files);
|
|
|
|
res.send("var specs_list = " + JSON.stringify(files) + ";\n");
|
2012-10-27 18:05:26 +02:00
|
|
|
});
|
|
|
|
|
2015-04-11 03:19:26 +02:00
|
|
|
// path.join seems to normalize by default, but we'll just be explicit
|
|
|
|
var rootTestFolder = path.normalize(path.join(npm.root, "../tests/frontend/"));
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
var url2FilePath = function(url) {
|
2012-10-27 18:29:17 +02:00
|
|
|
var subPath = url.substr("/tests/frontend".length);
|
2019-02-08 23:20:57 +01:00
|
|
|
if (subPath == "") {
|
2012-10-02 01:35:43 +02:00
|
|
|
subPath = "index.html"
|
|
|
|
}
|
2012-10-08 00:34:29 +02:00
|
|
|
subPath = subPath.split("?")[0];
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2015-04-11 02:25:52 +02:00
|
|
|
var filePath = path.normalize(path.join(rootTestFolder, subPath));
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2015-04-11 02:25:52 +02:00
|
|
|
// make sure we jail the paths to the test folder, otherwise serve index
|
|
|
|
if (filePath.indexOf(rootTestFolder) !== 0) {
|
2015-04-11 03:19:26 +02:00
|
|
|
filePath = path.join(rootTestFolder, "index.html");
|
2015-04-11 02:25:52 +02:00
|
|
|
}
|
2012-10-27 18:29:17 +02:00
|
|
|
return filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.app.get('/tests/frontend/specs/*', function (req, res) {
|
|
|
|
var specFilePath = url2FilePath(req.url);
|
|
|
|
var specFileName = path.basename(specFilePath);
|
|
|
|
|
2019-02-08 23:20:57 +01:00
|
|
|
fs.readFile(specFilePath, function(err, content) {
|
|
|
|
if (err) { return res.send(500); }
|
|
|
|
|
2012-10-27 18:29:17 +02:00
|
|
|
content = "describe(" + JSON.stringify(specFileName) + ", function(){ " + content + " });";
|
2012-10-02 01:35:43 +02:00
|
|
|
|
2012-10-27 18:29:17 +02:00
|
|
|
res.send(content);
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2012-10-27 18:29:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
args.app.get('/tests/frontend/*', function (req, res) {
|
|
|
|
var filePath = url2FilePath(req.url);
|
2015-04-10 12:52:58 +02:00
|
|
|
res.sendFile(filePath);
|
2012-10-02 01:35:43 +02:00
|
|
|
});
|
2012-10-27 17:41:17 +02:00
|
|
|
|
|
|
|
args.app.get('/tests/frontend', function (req, res) {
|
|
|
|
res.redirect('/tests/frontend/');
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2013-02-03 01:14:17 +01:00
|
|
|
}
|
2013-02-04 01:00:39 +01:00
|
|
|
|
2019-01-23 17:21:40 +01:00
|
|
|
const readdir = util.promisify(fs.readdir);
|
|
|
|
|
|
|
|
exports.getPluginTests = async function(callback) {
|
|
|
|
const moduleDir = "node_modules/";
|
|
|
|
const specPath = "/static/tests/frontend/specs/";
|
|
|
|
const staticDir = "/static/plugins/";
|
|
|
|
|
|
|
|
let pluginSpecs = [];
|
|
|
|
|
|
|
|
let plugins = await readdir(moduleDir);
|
|
|
|
let promises = plugins
|
|
|
|
.map(plugin => [ plugin, moduleDir + plugin + specPath] )
|
|
|
|
.filter(([plugin, specDir]) => fs.existsSync(specDir)) // check plugin exists
|
|
|
|
.map(([plugin, specDir]) => {
|
|
|
|
return readdir(specDir)
|
|
|
|
.then(specFiles => specFiles.map(spec => {
|
|
|
|
pluginSpecs.push(staticDir + plugin + specPath + spec);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(promises).then(() => pluginSpecs);
|
2013-02-04 01:00:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:21:40 +01:00
|
|
|
exports.getCoreTests = function() {
|
2019-02-08 23:20:57 +01:00
|
|
|
// get the core test specs
|
2019-01-23 17:21:40 +01:00
|
|
|
return readdir('tests/frontend/specs');
|
2013-02-04 01:00:39 +01:00
|
|
|
}
|