2018-09-16 17:24:07 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
const childProcess = require('child_process')
|
2018-09-13 15:30:12 +00:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
|
|
|
const { hashElement } = require('folder-hash')
|
|
|
|
const path = require('path')
|
2019-03-21 19:24:07 +00:00
|
|
|
const unknownFlags = []
|
2019-03-14 22:23:21 +00:00
|
|
|
|
2019-08-28 22:19:52 +00:00
|
|
|
require('colors')
|
2019-08-29 14:46:54 +00:00
|
|
|
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)
|
2019-03-13 22:09:28 +00:00
|
|
|
})
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2019-03-21 19:24:07 +00:00
|
|
|
const unknownArgs = []
|
|
|
|
for (const flag of unknownFlags) {
|
|
|
|
unknownArgs.push(flag)
|
|
|
|
const onlyFlag = flag.replace(/^-+/, '')
|
|
|
|
if (args[onlyFlag]) {
|
|
|
|
unknownArgs.push(args[onlyFlag])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
const utils = require('./lib/utils')
|
2019-06-05 23:30:39 +00:00
|
|
|
const { YARN_VERSION } = require('./yarn')
|
2018-09-13 16:57:39 +00:00
|
|
|
|
2018-09-13 15:30:12 +00:00
|
|
|
const BASE = path.resolve(__dirname, '../..')
|
|
|
|
const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
2019-04-30 20:59:47 +00:00
|
|
|
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 }]
|
2019-08-28 22:19:52 +00:00
|
|
|
])
|
|
|
|
|
2018-09-13 15:30:12 +00:00
|
|
|
const specHashPath = path.resolve(__dirname, '../spec/.hash')
|
|
|
|
|
2019-03-15 00:22:42 +00:00
|
|
|
let runnersToRun = null
|
|
|
|
if (args.runners) {
|
2019-03-15 17:57:12 +00:00
|
|
|
runnersToRun = args.runners.split(',')
|
2019-08-28 22:19:52 +00:00
|
|
|
if (!runnersToRun.every(r => [...runners.keys()].includes(r))) {
|
|
|
|
console.log(`${fail} ${runnersToRun} must be a subset of [${[...runners.keys()].join(' | ')}]`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2019-03-15 00:22:42 +00:00
|
|
|
console.log('Only running:', runnersToRun)
|
2019-03-10 22:38:44 +00:00
|
|
|
} else {
|
2019-10-03 20:21:30 +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 () {
|
|
|
|
const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash()
|
|
|
|
const [currentSpecHash, currentSpecInstallHash] = await getSpecHash()
|
|
|
|
const somethingChanged = (currentSpecHash !== lastSpecHash) ||
|
|
|
|
(lastSpecInstallHash !== currentSpecInstallHash)
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
if (somethingChanged) {
|
2019-10-09 23:33:15 +00:00
|
|
|
await installSpecModules(path.resolve(__dirname, '..', 'spec'))
|
|
|
|
await installSpecModules(path.resolve(__dirname, '..', 'spec-main'))
|
2018-10-02 01:53:34 +00:00
|
|
|
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'))) {
|
|
|
|
console.log('Generating electron.d.ts as it is missing')
|
|
|
|
generateTypeDefinitions()
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
await runElectronTests()
|
|
|
|
}
|
|
|
|
|
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
|
2019-03-10 23:24:27 +00:00
|
|
|
})
|
|
|
|
if (status !== 0) {
|
|
|
|
throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 01:53:34 +00:00
|
|
|
function loadLastSpecHash () {
|
|
|
|
return fs.existsSync(specHashPath)
|
|
|
|
? fs.readFileSync(specHashPath, 'utf8').split('\n')
|
|
|
|
: [null, null]
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
|
|
|
|
fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runElectronTests () {
|
2019-03-10 22:38:44 +00:00
|
|
|
const errors = []
|
2019-07-09 20:56:46 +00:00
|
|
|
|
|
|
|
const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR
|
|
|
|
for (const [runnerId, { description, run }] of runners) {
|
2019-08-29 18:11:10 +00:00
|
|
|
if (runnersToRun && !runnersToRun.includes(runnerId)) {
|
2019-07-09 20:56:46 +00:00
|
|
|
console.info('\nSkipping:', description)
|
2019-03-10 22:38:44 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
2019-07-09 20:56:46 +00:00
|
|
|
console.info('\nRunning:', description)
|
|
|
|
if (testResultsDir) {
|
|
|
|
process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`)
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
2019-07-09 20:56:46 +00:00
|
|
|
await run()
|
2019-03-10 22:38:44 +00:00
|
|
|
} catch (err) {
|
2019-07-09 20:56:46 +00:00
|
|
|
errors.push([runnerId, err])
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length !== 0) {
|
|
|
|
for (const err of errors) {
|
|
|
|
console.error('\n\nRunner Failed:', err[0])
|
|
|
|
console.error(err[1])
|
|
|
|
}
|
2019-08-28 22:19:52 +00:00
|
|
|
console.log(`${fail} Electron test runners have failed`)
|
|
|
|
process.exit(1)
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runRemoteBasedElectronTests () {
|
2018-09-13 16:57:39 +00:00
|
|
|
let exe = path.resolve(BASE, utils.getElectronExec())
|
2019-03-14 23:15:23 +00:00
|
|
|
const runnerArgs = ['electron/spec', ...unknownArgs.slice(2)]
|
2018-09-13 15:30:12 +00:00
|
|
|
if (process.platform === 'linux') {
|
2019-03-13 22:09:28 +00:00
|
|
|
runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
|
2018-09-13 15:30:12 +00:00
|
|
|
exe = 'python'
|
|
|
|
}
|
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'
|
|
|
|
})
|
|
|
|
if (status !== 0) {
|
2019-08-02 21:23:11 +00:00
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString()
|
2019-08-28 22:19:52 +00:00
|
|
|
console.log(`${fail} Electron tests failed with code ${textStatus}.`)
|
|
|
|
process.exit(1)
|
2019-03-10 22:38:44 +00:00
|
|
|
}
|
2019-08-28 22:19:52 +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 () {
|
|
|
|
let testTargets = require('./native-test-targets.json')
|
2019-12-17 22:00:42 +00:00
|
|
|
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)) {
|
|
|
|
console.log(`${fail} ${args.target} must be a subset of [${[testTargets].join(', ')}]`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Exit if test target failed to build
|
|
|
|
if (build.status !== 0) {
|
|
|
|
console.log(`${fail} ${target} failed to build.`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a specific target was passed, only build and run that target
|
|
|
|
if (args.target) testTargets = [args.target]
|
|
|
|
|
|
|
|
// Run test targets
|
|
|
|
const failures = []
|
|
|
|
for (const target of testTargets) {
|
|
|
|
console.info('\nRunning native test for target:', target)
|
|
|
|
const testRun = childProcess.spawnSync(`./${outDir}/${target}`, {
|
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Collect failures and log at end
|
|
|
|
if (testRun.status !== 0) failures.push({ target })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit if any failures
|
|
|
|
if (failures.length > 0) {
|
|
|
|
console.log(`${fail} Electron native tests failed for the following targets: `, failures)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`${pass} Electron native tests passed.`)
|
|
|
|
}
|
|
|
|
|
2019-03-10 22:38:44 +00:00
|
|
|
async function runMainProcessElectronTests () {
|
2019-08-28 20:54:50 +00:00
|
|
|
let exe = path.resolve(BASE, utils.getElectronExec())
|
|
|
|
const runnerArgs = ['electron/spec-main', ...unknownArgs.slice(2)]
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
|
|
|
|
exe = 'python'
|
|
|
|
}
|
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'
|
|
|
|
})
|
2018-10-02 01:53:34 +00:00
|
|
|
if (status !== 0) {
|
2020-02-25 02:11:06 +00:00
|
|
|
if (status) {
|
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString()
|
|
|
|
console.log(`${fail} Electron tests failed with code ${textStatus}.`)
|
|
|
|
} else {
|
|
|
|
console.log(`${fail} Electron tests failed with kill signal ${signal}.`)
|
|
|
|
}
|
2019-08-28 22:19:52 +00:00
|
|
|
process.exit(1)
|
2018-10-02 01:53:34 +00:00
|
|
|
}
|
2019-08-28 22:19:52 +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) {
|
2019-12-17 22:00:42 +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'
|
2018-10-02 01:53:34 +00:00
|
|
|
})
|
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'
|
2018-09-13 15:30:12 +00:00
|
|
|
})
|
2019-07-19 23:15:47 +00:00
|
|
|
if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) {
|
2019-10-09 23:33:15 +00:00
|
|
|
console.log(`${fail} Failed to yarn install in '${dir}'`)
|
2019-08-28 22:19:52 +00:00
|
|
|
process.exit(1)
|
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 () => {
|
2018-09-13 15:30:12 +00:00
|
|
|
const hasher = crypto.createHash('SHA256')
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
|
2019-10-09 23:33:15 +00:00
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/package.json')))
|
2019-04-30 20:59:47 +00:00
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock')))
|
2019-10-09 23:33:15 +00:00
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec-main/yarn.lock')))
|
2018-10-02 01:53:34 +00:00
|
|
|
return hasher.digest('hex')
|
|
|
|
})(),
|
|
|
|
(async () => {
|
2018-09-13 15:30:12 +00:00
|
|
|
const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
|
|
|
|
if (!fs.existsSync(specNodeModulesPath)) {
|
2018-10-02 01:53:34 +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']
|
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
})
|
|
|
|
return hash
|
|
|
|
})()
|
2018-09-13 15:30:12 +00:00
|
|
|
])
|
|
|
|
}
|
2018-10-02 01:53:34 +00:00
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
console.error('An error occurred inside the spec runner:', error)
|
|
|
|
process.exit(1)
|
|
|
|
})
|