tests.js: remove use of async.js

Use real `async` instead of async.js where applicable.
The `getPluginTests()` function was never truly async anyway because it only
contains calls to synchronous `fs` modules.
pull/3559/head
Ray Bellis 2019-01-23 16:21:40 +00:00
parent 0c2d662541
commit 23a3a079a6
1 changed files with 34 additions and 39 deletions

View File

@ -1,24 +1,19 @@
var path = require("path") var path = require("path")
, npm = require("npm") , npm = require("npm")
, fs = require("fs") , fs = require("fs")
, async = require("async"); , util = require("util");
exports.expressCreateServer = function (hook_name, args, cb) { exports.expressCreateServer = function (hook_name, args, cb) {
args.app.get('/tests/frontend/specs_list.js', function(req, res) { args.app.get('/tests/frontend/specs_list.js', async function(req, res) {
async.parallel({ let [coreTests, pluginTests] = await Promise.all([
coreSpecs: function(callback) { exports.getCoreTests(),
exports.getCoreTests(callback); exports.getPluginTests()
}, ]);
pluginSpecs: function(callback) {
exports.getPluginTests(callback); // merge the two sets of results
} let files = [].concat(coreTests, pluginTests).sort();
}, console.debug("Sent browser the following test specs:", files);
function(err, results) { res.send("var specs_list = " + JSON.stringify(files) + ";\n");
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");
});
}); });
// path.join seems to normalize by default, but we'll just be explicit // path.join seems to normalize by default, but we'll just be explicit
@ -63,30 +58,30 @@ exports.expressCreateServer = function (hook_name, args, cb) {
}); });
} }
exports.getPluginTests = function(callback) { const readdir = util.promisify(fs.readdir);
var pluginSpecs = [];
var plugins = fs.readdirSync('node_modules'); exports.getPluginTests = async function(callback) {
plugins.forEach(function(plugin) { const moduleDir = "node_modules/";
if (fs.existsSync("node_modules/" + plugin + "/static/tests/frontend/specs")) { const specPath = "/static/tests/frontend/specs/";
// if plugins exists const staticDir = "/static/plugins/";
var specFiles = fs.readdirSync("node_modules/" + plugin + "/static/tests/frontend/specs/");
async.forEach(specFiles, function(spec) { let pluginSpecs = [];
// for each specFile push it to pluginSpecs
pluginSpecs.push("/static/plugins/" + plugin + "/static/tests/frontend/specs/" + spec); let plugins = await readdir(moduleDir);
}, let promises = plugins
function(err) { .map(plugin => [ plugin, moduleDir + plugin + specPath] )
// blow up if something bad happens! .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);
callback(null, pluginSpecs);
} }
exports.getCoreTests = function(callback) { exports.getCoreTests = function() {
// get the core test specs // get the core test specs
fs.readdir('tests/frontend/specs', function(err, coreSpecs) { return readdir('tests/frontend/specs');
if (err) { return res.send(500); }
callback(null, coreSpecs);
});
} }