etherpad-lite/src/node/hooks/express/tests.js

95 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-01-21 22:06:52 +01:00
'use strict';
2020-11-23 19:24:19 +01:00
const path = require('path');
const npm = require('npm');
const fs = require('fs');
const util = require('util');
2021-01-21 22:06:52 +01:00
exports.expressCreateServer = (hookName, args, cb) => {
2020-11-23 19:24:19 +01:00
args.app.get('/tests/frontend/specs_list.js', async (req, res) => {
const [coreTests, pluginTests] = await Promise.all([
exports.getCoreTests(),
2020-11-23 19:24:19 +01:00
exports.getPluginTests(),
]);
// merge the two sets of results
let files = [].concat(coreTests, pluginTests).sort();
// Keep only *.js files
files = files.filter((f) => f.endsWith('.js'));
2020-11-23 19:24:19 +01:00
console.debug('Sent browser the following test specs:', files);
res.setHeader('content-type', 'application/javascript');
2020-11-23 19:24:19 +01:00
res.end(`var specs_list = ${JSON.stringify(files)};\n`);
});
// path.join seems to normalize by default, but we'll just be explicit
2020-11-23 19:24:19 +01:00
const rootTestFolder = path.normalize(path.join(npm.root, '../tests/frontend/'));
2021-01-21 22:06:52 +01:00
const url2FilePath = (url) => {
2020-11-23 19:24:19 +01:00
let subPath = url.substr('/tests/frontend'.length);
2021-01-21 22:06:52 +01:00
if (subPath === '') {
2020-11-23 19:24:19 +01:00
subPath = 'index.html';
}
2020-11-23 19:24:19 +01:00
subPath = subPath.split('?')[0];
2020-11-23 19:24:19 +01:00
let filePath = path.normalize(path.join(rootTestFolder, subPath));
// make sure we jail the paths to the test folder, otherwise serve index
if (filePath.indexOf(rootTestFolder) !== 0) {
2020-11-23 19:24:19 +01:00
filePath = path.join(rootTestFolder, 'index.html');
}
2012-10-27 18:29:17 +02:00
return filePath;
2020-11-23 19:24:19 +01:00
};
2012-10-27 18:29:17 +02:00
2020-11-23 19:24:19 +01:00
args.app.get('/tests/frontend/specs/*', (req, res) => {
const specFilePath = url2FilePath(req.url);
const specFileName = path.basename(specFilePath);
2012-10-27 18:29:17 +02:00
2020-11-23 19:24:19 +01:00
fs.readFile(specFilePath, (err, content) => {
if (err) { return res.send(500); }
2020-11-23 19:24:19 +01:00
content = `describe(${JSON.stringify(specFileName)}, function(){ ${content} });`;
2021-01-21 22:06:52 +01:00
if (!specFilePath.endsWith('index.html')) {
res.setHeader('content-type', 'application/javascript');
}
2012-10-27 18:29:17 +02:00
res.send(content);
});
2012-10-27 18:29:17 +02:00
});
2020-11-23 19:24:19 +01:00
args.app.get('/tests/frontend/*', (req, res) => {
const filePath = url2FilePath(req.url);
res.sendFile(filePath);
});
2012-10-27 17:41:17 +02:00
2020-11-23 19:24:19 +01:00
args.app.get('/tests/frontend', (req, res) => {
res.redirect('/tests/frontend/index.html');
});
return cb();
2020-11-23 19:24:19 +01:00
};
2013-02-04 01:00:39 +01:00
const readdir = util.promisify(fs.readdir);
2021-01-21 22:06:52 +01:00
exports.getPluginTests = async (callback) => {
2020-11-23 19:24:19 +01:00
const moduleDir = 'node_modules/';
const specPath = '/static/tests/frontend/specs/';
const staticDir = '/static/plugins/';
const pluginSpecs = [];
const plugins = await readdir(moduleDir);
const promises = plugins
.map((plugin) => [plugin, moduleDir + plugin + specPath])
.filter(([plugin, specDir]) => fs.existsSync(specDir)) // check plugin exists
.map(([plugin, specDir]) => readdir(specDir)
.then((specFiles) => specFiles.map((spec) => {
pluginSpecs.push(staticDir + plugin + specPath + spec);
})));
return Promise.all(promises).then(() => pluginSpecs);
2020-11-23 19:24:19 +01:00
};
2013-02-04 01:00:39 +01:00
exports.getCoreTests = () => readdir('src/tests/frontend/specs');