2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2021-05-09 16:33:09 -04:00
|
|
|
const fsp = fs.promises;
|
2021-05-09 17:36:18 -04:00
|
|
|
const sanitizePathname = require('../../utils/sanitizePathname');
|
2021-02-07 11:32:57 +00:00
|
|
|
const settings = require('../../utils/Settings');
|
2012-10-02 00:35:43 +01:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
exports.expressCreateServer = (hookName, args, cb) => {
|
2021-04-22 13:18:54 -04:00
|
|
|
args.app.get('/tests/frontend/frontendTestSpecs.js', async (req, res) => {
|
2021-05-07 17:55:00 -04:00
|
|
|
const [coreTests, pluginTests] = await Promise.all([getCoreTests(), getPluginTests()]);
|
2019-01-23 16:21:40 +00:00
|
|
|
|
|
|
|
// merge the two sets of results
|
|
|
|
let files = [].concat(coreTests, pluginTests).sort();
|
2020-03-22 17:41:57 +00:00
|
|
|
|
2020-10-11 19:56:01 -04:00
|
|
|
// Keep only *.js files
|
|
|
|
files = files.filter((f) => f.endsWith('.js'));
|
2020-03-22 17:41:57 +00:00
|
|
|
|
2021-02-07 11:32:57 +00:00
|
|
|
// remove admin tests if the setting to enable them isn't in settings.json
|
|
|
|
if (!settings.enableAdminUITests) {
|
|
|
|
files = files.filter((file) => file.indexOf('admin') !== 0);
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
console.debug('Sent browser the following test specs:', files);
|
2020-12-27 23:32:09 +01:00
|
|
|
res.setHeader('content-type', 'application/javascript');
|
2021-04-22 13:22:42 -04:00
|
|
|
res.end(`window.frontendTestSpecs = ${JSON.stringify(files, null, 2)};\n`);
|
2012-10-27 17:05:26 +01:00
|
|
|
});
|
|
|
|
|
2021-02-10 01:21:29 -05:00
|
|
|
const rootTestFolder = path.join(settings.root, 'src/tests/frontend/');
|
2015-04-10 20:19:26 -05:00
|
|
|
|
2021-05-08 18:33:36 -04:00
|
|
|
// The regexp /[\d\D]{0,}/ is equivalent to the regexp /.*/. The Express route path used here
|
|
|
|
// uses the more verbose /[\d\D]{0,}/ pattern instead of /.*/ because path-to-regexp v0.1.7 (the
|
|
|
|
// version used with Express v4.x) interprets '.' and '*' differently than regexp.
|
2021-05-09 17:34:49 -04:00
|
|
|
args.app.get('/tests/frontend/:file([\\d\\D]{0,})', (req, res, next) => {
|
2021-05-09 16:36:47 -04:00
|
|
|
(async () => {
|
2021-05-09 17:36:18 -04:00
|
|
|
let relFile = sanitizePathname(req.params.file);
|
|
|
|
if (['', '.', './'].includes(relFile)) relFile = 'index.html';
|
|
|
|
const file = path.join(rootTestFolder, relFile);
|
|
|
|
if (relFile.startsWith('specs/') && file.endsWith('.js')) {
|
2021-05-09 17:23:02 -04:00
|
|
|
const content = await fsp.readFile(file);
|
2021-01-21 21:06:52 +00:00
|
|
|
res.setHeader('content-type', 'application/javascript');
|
2021-05-09 17:23:02 -04:00
|
|
|
res.send(`describe(${JSON.stringify(path.basename(file))}, function () {\n${content}\n});`);
|
|
|
|
} else {
|
|
|
|
res.sendFile(file);
|
2021-01-21 21:06:52 +00:00
|
|
|
}
|
2021-05-09 16:36:47 -04:00
|
|
|
})().catch((err) => next(err || new Error(err)));
|
2012-10-27 17:29:17 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
args.app.get('/tests/frontend', (req, res) => {
|
2020-04-06 14:15:59 -04:00
|
|
|
res.redirect('/tests/frontend/index.html');
|
2019-02-08 23:20:57 +01:00
|
|
|
});
|
2020-10-10 22:51:26 -04:00
|
|
|
|
|
|
|
return cb();
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2013-02-04 00:00:39 +00:00
|
|
|
|
2021-05-07 17:55:00 -04:00
|
|
|
const getPluginTests = async (callback) => {
|
2020-11-23 13:24:19 -05:00
|
|
|
const moduleDir = 'node_modules/';
|
|
|
|
const specPath = '/static/tests/frontend/specs/';
|
2021-05-09 16:33:09 -04:00
|
|
|
const plugins = await fsp.readdir(moduleDir);
|
2021-05-09 16:50:59 -04:00
|
|
|
const specLists = await Promise.all(plugins.map(async (plugin) => {
|
2021-05-09 16:41:59 -04:00
|
|
|
const specDir = moduleDir + plugin + specPath;
|
2021-05-09 16:50:59 -04:00
|
|
|
if (!fs.existsSync(specDir)) return [];
|
2021-05-09 16:41:59 -04:00
|
|
|
const specFiles = await fsp.readdir(specDir);
|
2021-05-09 16:53:16 -04:00
|
|
|
return specFiles.map((spec) => `/static/plugins/${plugin}${specPath}${spec}`);
|
2021-05-09 16:41:59 -04:00
|
|
|
}));
|
2021-05-09 16:50:59 -04:00
|
|
|
return [].concat(...specLists);
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2013-02-04 00:00:39 +00:00
|
|
|
|
2021-05-07 17:55:00 -04:00
|
|
|
const getCoreTests = async () => await fsp.readdir('src/tests/frontend/specs');
|