2018-09-16 20:24:07 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-10-02 04:53:34 +03:00
|
|
|
const childProcess = require('child_process')
|
2018-09-13 18:30:12 +03:00
|
|
|
const crypto = require('crypto')
|
|
|
|
const fs = require('fs')
|
|
|
|
const { hashElement } = require('folder-hash')
|
|
|
|
const path = require('path')
|
2019-03-21 22:24:07 +03:00
|
|
|
const unknownFlags = []
|
2019-03-15 01:23:21 +03:00
|
|
|
|
2019-03-14 01:09:28 +03:00
|
|
|
const args = require('minimist')(process.argv, {
|
2019-03-15 03:22:42 +03:00
|
|
|
string: ['runners'],
|
2019-03-21 22:24:07 +03:00
|
|
|
unknown: arg => unknownFlags.push(arg)
|
2019-03-14 01:09:28 +03:00
|
|
|
})
|
2018-09-13 18:30:12 +03:00
|
|
|
|
2019-03-21 22:24:07 +03: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 19:57:39 +03:00
|
|
|
const utils = require('./lib/utils')
|
2019-06-06 02:30:39 +03:00
|
|
|
const { YARN_VERSION } = require('./yarn')
|
2018-09-13 19:57:39 +03:00
|
|
|
|
2018-09-13 18:30:12 +03:00
|
|
|
const BASE = path.resolve(__dirname, '../..')
|
|
|
|
const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
2019-04-30 23:59:47 +03:00
|
|
|
const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx'
|
2018-09-13 18:30:12 +03:00
|
|
|
|
|
|
|
const specHashPath = path.resolve(__dirname, '../spec/.hash')
|
|
|
|
|
2019-03-15 03:22:42 +03:00
|
|
|
let runnersToRun = null
|
|
|
|
if (args.runners) {
|
2019-03-15 20:57:12 +03:00
|
|
|
runnersToRun = args.runners.split(',')
|
2019-03-15 03:22:42 +03:00
|
|
|
console.log('Only running:', runnersToRun)
|
2019-03-11 01:38:44 +03:00
|
|
|
} else {
|
|
|
|
console.log('Will trigger all spec runners')
|
|
|
|
}
|
|
|
|
|
2018-10-02 04:53:34 +03:00
|
|
|
async function main () {
|
|
|
|
const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash()
|
|
|
|
const [currentSpecHash, currentSpecInstallHash] = await getSpecHash()
|
|
|
|
const somethingChanged = (currentSpecHash !== lastSpecHash) ||
|
|
|
|
(lastSpecInstallHash !== currentSpecInstallHash)
|
2018-09-13 18:30:12 +03:00
|
|
|
|
2018-10-02 04:53:34 +03:00
|
|
|
if (somethingChanged) {
|
|
|
|
await installSpecModules()
|
|
|
|
await getSpecHash().then(saveSpecHash)
|
2018-09-13 18:30:12 +03:00
|
|
|
}
|
2018-10-02 04:53:34 +03:00
|
|
|
|
2019-03-11 02:24:27 +03:00
|
|
|
if (!fs.existsSync(path.resolve(__dirname, '../electron.d.ts'))) {
|
|
|
|
console.log('Generating electron.d.ts as it is missing')
|
|
|
|
generateTypeDefinitions()
|
|
|
|
}
|
|
|
|
|
2018-10-02 04:53:34 +03:00
|
|
|
await runElectronTests()
|
|
|
|
}
|
|
|
|
|
2019-03-11 02:24:27 +03:00
|
|
|
function generateTypeDefinitions () {
|
|
|
|
const { status } = childProcess.spawnSync('npm', ['run', 'create-typescript-definitions'], {
|
|
|
|
cwd: path.resolve(__dirname, '..'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
})
|
|
|
|
if (status !== 0) {
|
|
|
|
throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 04:53:34 +03: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-11 01:38:44 +03:00
|
|
|
const errors = []
|
2019-07-09 23:56:46 +03:00
|
|
|
const runners = new Map([
|
|
|
|
['main', { description: 'Main process specs', run: runMainProcessElectronTests }],
|
|
|
|
['remote', { description: 'Remote based specs', run: runRemoteBasedElectronTests }]
|
|
|
|
])
|
|
|
|
|
|
|
|
const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR
|
|
|
|
for (const [runnerId, { description, run }] of runners) {
|
|
|
|
if (runnersToRun && !runnersToRun.includes(runnerId)) {
|
|
|
|
console.info('\nSkipping:', description)
|
2019-03-11 01:38:44 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
2019-07-09 23:56:46 +03:00
|
|
|
console.info('\nRunning:', description)
|
|
|
|
if (testResultsDir) {
|
|
|
|
process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`)
|
2019-03-11 01:38:44 +03:00
|
|
|
}
|
2019-07-09 23:56:46 +03:00
|
|
|
await run()
|
2019-03-11 01:38:44 +03:00
|
|
|
} catch (err) {
|
2019-07-09 23:56:46 +03:00
|
|
|
errors.push([runnerId, err])
|
2019-03-11 01:38:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errors.length !== 0) {
|
|
|
|
for (const err of errors) {
|
|
|
|
console.error('\n\nRunner Failed:', err[0])
|
|
|
|
console.error(err[1])
|
|
|
|
}
|
|
|
|
throw new Error('Electron test runners have failed')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runRemoteBasedElectronTests () {
|
2018-09-13 19:57:39 +03:00
|
|
|
let exe = path.resolve(BASE, utils.getElectronExec())
|
2019-03-15 02:15:23 +03:00
|
|
|
const runnerArgs = ['electron/spec', ...unknownArgs.slice(2)]
|
2018-09-13 18:30:12 +03:00
|
|
|
if (process.platform === 'linux') {
|
2019-03-14 01:09:28 +03:00
|
|
|
runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
|
2018-09-13 18:30:12 +03:00
|
|
|
exe = 'python'
|
|
|
|
}
|
2018-10-02 04:53:34 +03:00
|
|
|
|
2019-03-14 01:09:28 +03:00
|
|
|
const { status } = childProcess.spawnSync(exe, runnerArgs, {
|
2019-03-11 01:38:44 +03:00
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
})
|
|
|
|
if (status !== 0) {
|
2019-08-03 00:23:11 +03:00
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString()
|
|
|
|
throw new Error(`Electron tests failed with code ${textStatus}.`)
|
2019-03-11 01:38:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runMainProcessElectronTests () {
|
|
|
|
const exe = path.resolve(BASE, utils.getElectronExec())
|
|
|
|
|
2019-03-15 01:23:21 +03:00
|
|
|
const { status } = childProcess.spawnSync(exe, ['electron/spec-main', ...unknownArgs.slice(2)], {
|
2018-09-13 18:30:12 +03:00
|
|
|
cwd: path.resolve(__dirname, '../..'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
})
|
2018-10-02 04:53:34 +03:00
|
|
|
if (status !== 0) {
|
2019-08-03 00:23:11 +03:00
|
|
|
const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString()
|
|
|
|
throw new Error(`Electron tests failed with code ${textStatus}.`)
|
2018-10-02 04:53:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installSpecModules () {
|
2019-08-21 20:41:35 +03:00
|
|
|
const nodeDir = path.resolve(BASE, `out/${utils.getOutDir(true)}/gen/node_headers`)
|
2018-10-02 04:53:34 +03:00
|
|
|
const env = Object.assign({}, process.env, {
|
|
|
|
npm_config_nodedir: nodeDir,
|
|
|
|
npm_config_msvs_version: '2017'
|
|
|
|
})
|
2019-06-06 02:30:39 +03:00
|
|
|
const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
|
2018-10-02 04:53:34 +03:00
|
|
|
env,
|
|
|
|
cwd: path.resolve(__dirname, '../spec'),
|
|
|
|
stdio: 'inherit'
|
2018-09-13 18:30:12 +03:00
|
|
|
})
|
2019-07-20 02:15:47 +03:00
|
|
|
if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) {
|
|
|
|
throw new Error('Failed to yarn install in the spec folder')
|
2018-10-02 04:53:34 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-13 18:30:12 +03:00
|
|
|
|
|
|
|
function getSpecHash () {
|
|
|
|
return Promise.all([
|
2018-10-02 04:53:34 +03:00
|
|
|
(async () => {
|
2018-09-13 18:30:12 +03:00
|
|
|
const hasher = crypto.createHash('SHA256')
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
|
2019-04-30 23:59:47 +03:00
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock')))
|
2018-10-02 04:53:34 +03:00
|
|
|
return hasher.digest('hex')
|
|
|
|
})(),
|
|
|
|
(async () => {
|
2018-09-13 18:30:12 +03:00
|
|
|
const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
|
|
|
|
if (!fs.existsSync(specNodeModulesPath)) {
|
2018-10-02 04:53:34 +03:00
|
|
|
return null
|
2018-09-13 18:30:12 +03:00
|
|
|
}
|
2018-10-02 04:53:34 +03:00
|
|
|
const { hash } = await hashElement(specNodeModulesPath, {
|
2018-10-01 17:00:04 +03:00
|
|
|
folders: {
|
|
|
|
exclude: ['.bin']
|
|
|
|
}
|
2018-10-02 04:53:34 +03:00
|
|
|
})
|
|
|
|
return hash
|
|
|
|
})()
|
2018-09-13 18:30:12 +03:00
|
|
|
])
|
|
|
|
}
|
2018-10-02 04:53:34 +03:00
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
console.error('An error occurred inside the spec runner:', error)
|
|
|
|
process.exit(1)
|
|
|
|
})
|