Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor how spec files are collected #23774

Merged
merged 1 commit into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
78 changes: 35 additions & 43 deletions spec-main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
});
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;

const testFiles = [];
walker.on('file', (file) => {
if (/-spec\.[tj]s$/.test(file) &&
(!moduleMatch || moduleMatch.test(file))) {
testFiles.push(file);
// 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;
}
});

const baseElectronDir = path.resolve(__dirname, '..');
return true;
};

walker.on('end', () => {
testFiles.sort();
testFiles.forEach((file) => {
if (!argv.files || argv.files.includes(path.relative(baseElectronDir, file))) {
mocha.addFile(file);
}
const getFiles = require('../spec/static/get-files');
const testFiles = await getFiles(__dirname, { filter });
testFiles.sort().forEach((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];
}
15 changes: 15 additions & 0 deletions spec/static/get-files.js
Original file line number Diff line number Diff line change
@@ -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;
68 changes: 33 additions & 35 deletions spec/static/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<body>
<script src="jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
(function() {
(async function() {
// Deprecated APIs are still supported and should be tested.
process.throwDeprecation = false

Expand Down Expand Up @@ -49,47 +49,45 @@
if (query.grep) mocha.grep(query.grep)
if (query.invert) mocha.invert()

const files = query.files ? query.files.split(',') : undefined

// Read all test files.
const walker = require('walkdir').walk(path.dirname(__dirname), {
no_recurse: true
})
const filter = (file) => {
if (!/-spec\.js$/.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
// 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 testFiles = []
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, '..', '..')
if (files && !files.includes(path.relative(baseElectronDir, file))) {
return false
}
})

const baseElectronDir = path.resolve(__dirname, '..', '..')
return true
}

walker.on('end', () => {
testFiles.sort()
testFiles.forEach((file) => {
if (!files || files.includes(path.relative(baseElectronDir, file))) {
mocha.addFile(file)
}
})
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
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(() => {
// Ensure the callback is called after runner is defined
setTimeout(() => {
ipcRenderer.send('process.exit', runner.failures)
}, 0)
})
const runner = mocha.run(() => {
// Ensure the callback is called after runner is defined
setTimeout(() => {
ipcRenderer.send('process.exit', runner.failures)
}, 0)
})
})()
</script>
Expand Down