From 3a7775fa73b37cb216553b600505ead67fd849d7 Mon Sep 17 00:00:00 2001 From: Alexey Kuzmin Date: Thu, 28 May 2020 00:21:02 +0200 Subject: [PATCH] test: refactor how spec files are collected (#23774) --- spec-main/index.js | 80 ++++++++++++++++++---------------------- spec/static/get-files.js | 15 ++++++++ spec/static/index.html | 74 ++++++++++++++++++------------------- 3 files changed, 87 insertions(+), 82 deletions(-) create mode 100644 spec/static/get-files.js diff --git a/spec-main/index.js b/spec-main/index.js index ef6d0bbebaba..6753378270a9 100644 --- a/spec-main/index.js +++ b/spec-main/index.js @@ -37,7 +37,7 @@ protocol.registerSchemesAsPrivileged([ { scheme: 'bar', privileges: { standard: true } } ]); -app.whenReady().then(() => { +app.whenReady().then(async () => { require('ts-node/register'); const argv = require('yargs') @@ -68,53 +68,45 @@ app.whenReady().then(() => { if (argv.grep) mocha.grep(argv.grep); if (argv.invert) mocha.invert(); - // Read all test files. - const walker = require('walkdir').walk(__dirname, { - no_recurse: true - }); - - // This allows you to run specific modules only: - // npm run test -match=menu - const moduleMatch = process.env.npm_config_match - ? new RegExp(process.env.npm_config_match, 'g') - : null; - - const testFiles = []; - walker.on('file', (file) => { - if (/-spec\.[tj]s$/.test(file) && - (!moduleMatch || moduleMatch.test(file))) { - testFiles.push(file); + const filter = (file) => { + if (!/-spec\.[tj]s$/.test(file)) { + return false; } + + // This allows you to run specific modules only: + // npm run test -match=menu + const moduleMatch = process.env.npm_config_match + ? new RegExp(process.env.npm_config_match, 'g') + : null; + if (moduleMatch && !moduleMatch.test(file)) { + return false; + } + + const baseElectronDir = path.resolve(__dirname, '..'); + if (argv.files && !argv.files.includes(path.relative(baseElectronDir, file))) { + return false; + } + + return true; + }; + + const getFiles = require('../spec/static/get-files'); + const testFiles = await getFiles(__dirname, { filter }); + testFiles.sort().forEach((file) => { + mocha.addFile(file); }); - const baseElectronDir = path.resolve(__dirname, '..'); - - walker.on('end', () => { - testFiles.sort(); - testFiles.forEach((file) => { - if (!argv.files || argv.files.includes(path.relative(baseElectronDir, file))) { - mocha.addFile(file); - } + const cb = () => { + // Ensure the callback is called after runner is defined + process.nextTick(() => { + process.exit(runner.failures); }); - const cb = () => { - // Ensure the callback is called after runner is defined - process.nextTick(() => { - process.exit(runner.failures); - }); - }; + }; - // Set up chai in the correct order - const chai = require('chai'); - chai.use(require('chai-as-promised')); - chai.use(require('dirty-chai')); + // Set up chai in the correct order + const chai = require('chai'); + chai.use(require('chai-as-promised')); + chai.use(require('dirty-chai')); - const runner = mocha.run(cb); - }); + const runner = mocha.run(cb); }); - -function partition (xs, f) { - const trues = []; - const falses = []; - xs.forEach(x => (f(x) ? trues : falses).push(x)); - return [trues, falses]; -} diff --git a/spec/static/get-files.js b/spec/static/get-files.js new file mode 100644 index 000000000000..9857d9742e89 --- /dev/null +++ b/spec/static/get-files.js @@ -0,0 +1,15 @@ +async function getFiles (directoryPath, { filter = null } = {}) { + const files = []; + const walker = require('walkdir').walk(directoryPath, { + no_recurse: true + }); + walker.on('file', (file) => { + if (!filter || filter(file)) { + files.push(file); + } + }); + await new Promise((resolve) => walker.on('end', resolve)); + return files; +} + +module.exports = getFiles; diff --git a/spec/static/index.html b/spec/static/index.html index 3f45dd957b7d..f50197be966e 100644 --- a/spec/static/index.html +++ b/spec/static/index.html @@ -1,7 +1,7 @@