test: refactor how spec files are collected (#23774)

This commit is contained in:
Alexey Kuzmin 2020-05-28 00:21:02 +02:00 committed by GitHub
parent 5d88d0ee74
commit 3a7775fa73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 82 deletions

View file

@ -37,7 +37,7 @@ protocol.registerSchemesAsPrivileged([
{ scheme: 'bar', privileges: { standard: true } } { scheme: 'bar', privileges: { standard: true } }
]); ]);
app.whenReady().then(() => { app.whenReady().then(async () => {
require('ts-node/register'); require('ts-node/register');
const argv = require('yargs') const argv = require('yargs')
@ -68,34 +68,34 @@ app.whenReady().then(() => {
if (argv.grep) mocha.grep(argv.grep); if (argv.grep) mocha.grep(argv.grep);
if (argv.invert) mocha.invert(); if (argv.invert) mocha.invert();
// Read all test files. const filter = (file) => {
const walker = require('walkdir').walk(__dirname, { if (!/-spec\.[tj]s$/.test(file)) {
no_recurse: true return false;
}); }
// This allows you to run specific modules only: // This allows you to run specific modules only:
// npm run test -match=menu // npm run test -match=menu
const moduleMatch = process.env.npm_config_match const moduleMatch = process.env.npm_config_match
? new RegExp(process.env.npm_config_match, 'g') ? new RegExp(process.env.npm_config_match, 'g')
: null; : null;
if (moduleMatch && !moduleMatch.test(file)) {
const testFiles = []; return false;
walker.on('file', (file) => {
if (/-spec\.[tj]s$/.test(file) &&
(!moduleMatch || moduleMatch.test(file))) {
testFiles.push(file);
} }
});
const baseElectronDir = path.resolve(__dirname, '..'); const baseElectronDir = path.resolve(__dirname, '..');
if (argv.files && !argv.files.includes(path.relative(baseElectronDir, file))) {
walker.on('end', () => { return false;
testFiles.sort();
testFiles.forEach((file) => {
if (!argv.files || argv.files.includes(path.relative(baseElectronDir, file))) {
mocha.addFile(file);
} }
return true;
};
const getFiles = require('../spec/static/get-files');
const testFiles = await getFiles(__dirname, { filter });
testFiles.sort().forEach((file) => {
mocha.addFile(file);
}); });
const cb = () => { const cb = () => {
// Ensure the callback is called after runner is defined // Ensure the callback is called after runner is defined
process.nextTick(() => { process.nextTick(() => {
@ -110,11 +110,3 @@ app.whenReady().then(() => {
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];
}

15
spec/static/get-files.js Normal file
View file

@ -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;

View file

@ -1,7 +1,7 @@
<body> <body>
<script src="jquery-2.0.3.min.js"></script> <script src="jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
(function() { (async function() {
// Deprecated APIs are still supported and should be tested. // Deprecated APIs are still supported and should be tested.
process.throwDeprecation = false process.throwDeprecation = false
@ -49,34 +49,33 @@
if (query.grep) mocha.grep(query.grep) if (query.grep) mocha.grep(query.grep)
if (query.invert) mocha.invert() if (query.invert) mocha.invert()
const files = query.files ? query.files.split(',') : undefined const filter = (file) => {
if (!/-spec\.js$/.test(file)) {
// Read all test files. return false
const walker = require('walkdir').walk(path.dirname(__dirname), { }
no_recurse: true
})
// This allows you to run specific modules only: // This allows you to run specific modules only:
// npm run test -match=menu // npm run test -match=menu
const moduleMatch = process.env.npm_config_match const moduleMatch = process.env.npm_config_match
? new RegExp(process.env.npm_config_match, 'g') ? new RegExp(process.env.npm_config_match, 'g')
: null : null
if (moduleMatch && !moduleMatch.test(file)) {
const testFiles = [] return false
walker.on('file', (file) => {
if (/-spec\.js$/.test(file) && (!moduleMatch || moduleMatch.test(file))) {
testFiles.push(file)
} }
})
const files = query.files ? query.files.split(',') : undefined
const baseElectronDir = path.resolve(__dirname, '..', '..') const baseElectronDir = path.resolve(__dirname, '..', '..')
if (files && !files.includes(path.relative(baseElectronDir, file))) {
walker.on('end', () => { return false
testFiles.sort()
testFiles.forEach((file) => {
if (!files || files.includes(path.relative(baseElectronDir, file))) {
mocha.addFile(file)
} }
return true
}
const getFiles = require('./get-files')
const testFiles = await getFiles(path.dirname(__dirname), { filter })
testFiles.sort().forEach((file) => {
mocha.addFile(file)
}) })
// Set up chai in the correct order // Set up chai in the correct order
@ -90,7 +89,6 @@
ipcRenderer.send('process.exit', runner.failures) ipcRenderer.send('process.exit', runner.failures)
}, 0) }, 0)
}) })
})
})() })()
</script> </script>
</body> </body>