2018-09-16 17:24:07 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const childProcess = require('child_process');
|
|
|
|
const crypto = require('crypto');
|
2020-07-17 16:08:44 +00:00
|
|
|
const fs = require('fs-extra');
|
2020-03-20 20:28:31 +00:00
|
|
|
const { hashElement } = require('folder-hash');
|
|
|
|
const path = require('path');
|
|
|
|
const unknownFlags = [];
|
2019-03-14 22:23:21 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
require('colors');
|
|
|
|
const pass = '✓'.green;
|
|
|
|
const fail = '✗'.red;
|
2019-08-28 22:19:52 +00:00
|
|
|
|
2019-03-13 22:09:28 +00:00
|
|
|
const args = require('minimist')(process.argv, {
|
2019-10-03 20:21:30 +00:00
|
|
|
string: ['runners', 'target'],
|
|
|
|
boolean: ['buildNativeTests'],
|
2019-03-21 19:24:07 +00:00
|
|
|
unknown: arg => unknownFlags.push(arg)
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const unknownArgs = [];
|
2019-03-21 19:24:07 +00:00
|
|
|
for (const flag of unknownFlags) {
|
2020-03-20 20:28:31 +00:00
|
|
|
unknownArgs.push(flag);
|
|
|
|
const onlyFlag = flag.replace(/^-+/, '');
|
2019-03-21 19:24:07 +00:00
|
|
|
if (args[onlyFlag]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
unknownArgs.push(args[onlyFlag]);
|
2019-03-21 19:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const utils = require('./lib/utils');
|
|
|
|
const { YARN_VERSION } = require('./yarn');
|
2018-09-13 16:57:39 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const BASE = path.resolve(__dirname, '../..');
|
|
|
|
const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx';
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2019-08-28 22:19:52 +00:00
|
|
|
const runners = new Map([
|
|
|
|
['main', { description: 'Main process specs', run: runMainProcessElectronTests }],
|
2020-02-07 12:56:54 +00:00
|
|
|
['remote', { description: 'Remote based specs', run: runRemoteBasedElectronTests }],
|
|
|
|
['native', { description: 'Native specs', run: runNativeElectronTests }]
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2019-08-28 22:19:52 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const specHashPath = path.resolve(__dirname, '../spec/.hash');
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let runnersToRun = null;
|
2019-03-15 00:22:42 +00:00
|
|
|
if (args.runners) {
|
2020-03-20 20:28:31 +00:00
|
|
|
runnersToRun = args.runners.split(',');
|
2019-08-28 22:19:52 +00:00
|
|
|
if (!runnersToRun.every(r => [...runners.keys()].includes(r))) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} ${runnersToRun} must be a subset of [${[...runners.keys()].join(' | ')}]`);
|
|
|
|
process.exit(1);
|
2019-08-28 22:19:52 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log('Only running:', runnersToRun);
|
2019-03-10 22:38:44 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`Triggering runners: ${[...runners.keys()].join(', ')}`);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
async function main () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash();
|
|
|
|
const [currentSpecHash, currentSpecInstallHash] = await getSpecHash();
|
2018-10-02 01:53:34 +00:00
|
|
|
const somethingChanged = (currentSpecHash !== lastSpecHash) ||
|
2020-03-20 20:28:31 +00:00
|
|
|
(lastSpecInstallHash !== currentSpecInstallHash);
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
if (somethingChanged) {
|
2020-03-20 20:28:31 +00:00
|
|
|
await installSpecModules(path.resolve(__dirname, '..', 'spec'));
|
|
|
|
await installSpecModules(path.resolve(__dirname, '..', 'spec-main'));
|
|
|
|
await getSpecHash().then(saveSpecHash);
|
2018-09-13 15:30:12 +00:00
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
|
2019-03-10 23:24:27 +00:00
|
|
|
if (!fs.existsSync(path.resolve(__dirname, '../electron.d.ts'))) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log('Generating electron.d.ts as it is missing');
|
|
|
|
generateTypeDefinitions();
|
2019-03-10 23:24:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
await runElectronTests();
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 23:24:27 +00:00
|
|
|
function generateTypeDefinitions () {
|
|
|
|
const { status } = childProcess.spawnSync('npm', ['run', 'create-typescript-definitions'], {
|
|
|
|
cwd: path.resolve(__dirname, '..'),
|
2019-09-03 17:13:06 +00:00
|
|
|
stdio: 'inherit',
|
|
|
|
shell: true
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-03-10 23:24:27 +00:00
|
|
|
if (status !== 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`);
|
2019-03-10 23:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
function loadLastSpecHash () {
|
|
|
|
return fs.existsSync(specHashPath)
|
|
|
|
? fs.readFileSync(specHashPath, 'utf8').split('\n')
|
2020-03-20 20:28:31 +00:00
|
|
|
: [null, null];
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
|
2020-03-20 20:28:31 +00:00
|
|
|
fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`);
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function runElectronTests () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const errors = [];
|
2019-07-09 20:56:46 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR;
|
2019-07-09 20:56:46 +00:00
|
|
|
for (const [runnerId, { description, run }] of runners) {
|
2019-08-29 18:11:10 +00:00
|
|
|
if (runnersToRun && !runnersToRun.includes(runnerId)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.info('\nSkipping:', description);
|
|
|
|
continue;
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
try {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.info('\nRunning:', description);
|
2019-07-09 20:56:46 +00:00
|
|
|
if (testResultsDir) {
|
2020-03-20 20:28:31 +00:00
|
|
|
process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
await run();
|
2019-03-10 22:38:44 +00:00
|
|
|
} catch (err) {
|
2020-03-20 20:28:31 +00:00
|
|
|
errors.push([runnerId, err]);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length !== 0) {
|
|
|
|
for (const err of errors) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('\n\nRunner Failed:', err[0]);
|
|
|
|
console.error(err[1]);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} Electron test runners have failed`);
|
|
|
|
process.exit(1);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runRemoteBasedElectronTests () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let exe = path.resolve(BASE, utils.getElectronExec());
|
|
|
|
const runnerArgs = ['electron/spec', ...unknownArgs.slice(2)];
|
2018-09-13 15:30:12 +00:00
|
|
|
if (process.platform === 'linux') {
|
2020-03-20 20:28:31 +00:00
|
|
|
runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe);
|
|
|
|
exe = 'python';
|
2018-09-13 15:30:12 +00:00
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
|
2019-03-13 22:09:28 +00:00
|
|
|
const { status } = childProcess.spawnSync(exe, runnerArgs, {
|
2019-03-10 22:38:44 +00:00
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-03-10 22:38:44 +00:00
|
|
|
if (status !== 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString();
|
|
|
|
console.log(`${fail} Electron tests failed with code ${textStatus}.`);
|
|
|
|
process.exit(1);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${pass} Electron remote process tests passed.`);
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 20:21:30 +00:00
|
|
|
async function runNativeElectronTests () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let testTargets = require('./native-test-targets.json');
|
|
|
|
const outDir = `out/${utils.getOutDir()}`;
|
2019-10-03 20:21:30 +00:00
|
|
|
|
|
|
|
// If native tests are being run, only one arg would be relevant
|
|
|
|
if (args.target && !testTargets.includes(args.target)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} ${args.target} must be a subset of [${[testTargets].join(', ')}]`);
|
|
|
|
process.exit(1);
|
2019-10-03 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally build all native test targets
|
|
|
|
if (args.buildNativeTests) {
|
|
|
|
for (const target of testTargets) {
|
|
|
|
const build = childProcess.spawnSync('ninja', ['-C', outDir, target], {
|
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-03 20:21:30 +00:00
|
|
|
|
|
|
|
// Exit if test target failed to build
|
|
|
|
if (build.status !== 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} ${target} failed to build.`);
|
|
|
|
process.exit(1);
|
2019-10-03 20:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a specific target was passed, only build and run that target
|
2020-03-20 20:28:31 +00:00
|
|
|
if (args.target) testTargets = [args.target];
|
2019-10-03 20:21:30 +00:00
|
|
|
|
|
|
|
// Run test targets
|
2020-03-20 20:28:31 +00:00
|
|
|
const failures = [];
|
2019-10-03 20:21:30 +00:00
|
|
|
for (const target of testTargets) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.info('\nRunning native test for target:', target);
|
2019-10-03 20:21:30 +00:00
|
|
|
const testRun = childProcess.spawnSync(`./${outDir}/${target}`, {
|
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-10-03 20:21:30 +00:00
|
|
|
|
|
|
|
// Collect failures and log at end
|
2020-03-20 20:28:31 +00:00
|
|
|
if (testRun.status !== 0) failures.push({ target });
|
2019-10-03 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exit if any failures
|
|
|
|
if (failures.length > 0) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} Electron native tests failed for the following targets: `, failures);
|
|
|
|
process.exit(1);
|
2019-10-03 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${pass} Electron native tests passed.`);
|
2019-10-03 20:21:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 22:38:44 +00:00
|
|
|
async function runMainProcessElectronTests () {
|
2020-03-20 20:28:31 +00:00
|
|
|
let exe = path.resolve(BASE, utils.getElectronExec());
|
|
|
|
const runnerArgs = ['electron/spec-main', ...unknownArgs.slice(2)];
|
2019-08-28 20:54:50 +00:00
|
|
|
if (process.platform === 'linux') {
|
2020-03-20 20:28:31 +00:00
|
|
|
runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe);
|
|
|
|
exe = 'python';
|
2019-08-28 20:54:50 +00:00
|
|
|
}
|
2019-03-10 22:38:44 +00:00
|
|
|
|
2020-02-25 02:11:06 +00:00
|
|
|
const { status, signal } = childProcess.spawnSync(exe, runnerArgs, {
|
2018-09-13 15:30:12 +00:00
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2018-10-02 01:53:34 +00:00
|
|
|
if (status !== 0) {
|
2020-02-25 02:11:06 +00:00
|
|
|
if (status) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString();
|
|
|
|
console.log(`${fail} Electron tests failed with code ${textStatus}.`);
|
2020-02-25 02:11:06 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} Electron tests failed with kill signal ${signal}.`);
|
2020-02-25 02:11:06 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
process.exit(1);
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${pass} Electron main process tests passed.`);
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 23:33:15 +00:00
|
|
|
async function installSpecModules (dir) {
|
2020-03-20 20:28:31 +00:00
|
|
|
const nodeDir = path.resolve(BASE, `out/${utils.getOutDir({ shouldLog: true })}/gen/node_headers`);
|
2018-10-02 01:53:34 +00:00
|
|
|
const env = Object.assign({}, process.env, {
|
|
|
|
npm_config_nodedir: nodeDir,
|
2019-10-28 22:12:35 +00:00
|
|
|
npm_config_msvs_version: '2019'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-07-17 16:08:44 +00:00
|
|
|
if (fs.existsSync(path.resolve(dir, 'node_modules'))) {
|
|
|
|
await fs.remove(path.resolve(dir, 'node_modules'));
|
|
|
|
}
|
2019-06-05 23:30:39 +00:00
|
|
|
const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
|
2018-10-02 01:53:34 +00:00
|
|
|
env,
|
2019-10-09 23:33:15 +00:00
|
|
|
cwd: dir,
|
2018-10-02 01:53:34 +00:00
|
|
|
stdio: 'inherit'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-07-19 23:15:47 +00:00
|
|
|
if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.log(`${fail} Failed to yarn install in '${dir}'`);
|
|
|
|
process.exit(1);
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
2020-07-17 16:08:44 +00:00
|
|
|
|
|
|
|
// TODO(MarshallOfSound): Remove once node-gyp supports arm64
|
|
|
|
if (process.platform === 'darwin' && process.env.npm_config_arch === 'arm64') {
|
|
|
|
for (const nodeModule of fs.readdirSync(path.resolve(dir, 'node_modules'))) {
|
|
|
|
if (fs.existsSync(path.resolve(dir, 'node_modules', nodeModule, 'binding.gyp'))) {
|
|
|
|
childProcess.spawnSync(NPX_CMD, ['https://github.com/MarshallOfSound/node-gyp/archive/apple-silicon.tar.gz', 'clean', 'configure', 'build', '--arch=arm64'], {
|
|
|
|
env,
|
|
|
|
cwd: path.resolve(dir, 'node_modules', nodeModule),
|
|
|
|
stdio: 'inherit'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
2018-09-13 15:30:12 +00:00
|
|
|
|
|
|
|
function getSpecHash () {
|
|
|
|
return Promise.all([
|
2018-10-02 01:53:34 +00:00
|
|
|
(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const hasher = crypto.createHash('SHA256');
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')));
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/package.json')));
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock')));
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/yarn.lock')));
|
2020-07-17 16:08:44 +00:00
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../script/spec-runner.js')));
|
2020-03-20 20:28:31 +00:00
|
|
|
return hasher.digest('hex');
|
2018-10-02 01:53:34 +00:00
|
|
|
})(),
|
|
|
|
(async () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules');
|
2018-09-13 15:30:12 +00:00
|
|
|
if (!fs.existsSync(specNodeModulesPath)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
return null;
|
2018-09-13 15:30:12 +00:00
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
const { hash } = await hashElement(specNodeModulesPath, {
|
2018-10-01 14:00:04 +00:00
|
|
|
folders: {
|
|
|
|
exclude: ['.bin']
|
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
return hash;
|
2018-10-02 01:53:34 +00:00
|
|
|
})()
|
2020-03-20 20:28:31 +00:00
|
|
|
]);
|
2018-09-13 15:30:12 +00:00
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
|
|
|
|
main().catch((error) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('An error occurred inside the spec runner:', error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|