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-14 22:23:21 +00:00
|
|
|
const unknownArgs = []
|
|
|
|
|
2019-03-13 22:09:28 +00:00
|
|
|
const args = require('minimist')(process.argv, {
|
2019-03-14 22:23:21 +00:00
|
|
|
string: ['only'],
|
|
|
|
unknown: arg => unknownArgs.push(arg)
|
2019-03-13 22:09:28 +00:00
|
|
|
})
|
2018-09-13 15:30:12 +00:00
|
|
|
|
2018-09-13 16:57:39 +00:00
|
|
|
const utils = require('./lib/utils')
|
|
|
|
|
2018-09-13 15:30:12 +00:00
|
|
|
const BASE = path.resolve(__dirname, '../..')
|
|
|
|
const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
|
|
|
|
|
|
|
|
const specHashPath = path.resolve(__dirname, '../spec/.hash')
|
|
|
|
|
2019-03-10 22:38:44 +00:00
|
|
|
let only = null
|
2019-03-13 22:09:28 +00:00
|
|
|
if (args.only) {
|
|
|
|
only = args.only.split(',')
|
2019-03-10 22:38:44 +00:00
|
|
|
console.log('Only running:', only)
|
|
|
|
} else {
|
|
|
|
console.log('Will trigger all spec runners')
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
await installSpecModules()
|
|
|
|
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, '..'),
|
|
|
|
stdio: 'inherit'
|
|
|
|
})
|
|
|
|
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 = []
|
|
|
|
const runners = [
|
|
|
|
['Remote based specs', 'remote', runRemoteBasedElectronTests],
|
|
|
|
['Main process specs', 'main', runMainProcessElectronTests]
|
|
|
|
]
|
|
|
|
|
|
|
|
const mochaFile = process.env.MOCHA_FILE
|
|
|
|
for (const runner of runners) {
|
|
|
|
if (only && !only.includes(runner[1])) {
|
|
|
|
console.info('\nSkipping:', runner[0])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
console.info('\nRunning:', runner[0])
|
|
|
|
if (mochaFile) {
|
|
|
|
process.env.MOCHA_FILE = mochaFile.replace('.xml', `-${runner[1]}.xml`)
|
|
|
|
}
|
|
|
|
await runner[2]()
|
|
|
|
} catch (err) {
|
|
|
|
errors.push([runner[0], err])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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) {
|
|
|
|
throw new Error(`Electron tests failed with code ${status}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runMainProcessElectronTests () {
|
|
|
|
const exe = path.resolve(BASE, utils.getElectronExec())
|
|
|
|
|
2019-03-14 22:23:21 +00:00
|
|
|
const { status } = childProcess.spawnSync(exe, ['electron/spec-main', ...unknownArgs.slice(2)], {
|
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) {
|
|
|
|
throw new Error(`Electron tests failed with code ${status}.`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function installSpecModules () {
|
|
|
|
const nodeDir = path.resolve(BASE, `out/${utils.OUT_DIR}/gen/node_headers`)
|
|
|
|
const env = Object.assign({}, process.env, {
|
|
|
|
npm_config_nodedir: nodeDir,
|
|
|
|
npm_config_msvs_version: '2017'
|
|
|
|
})
|
2019-03-12 00:09:34 +00:00
|
|
|
const { status } = childProcess.spawnSync(NPM_CMD, ['install'], {
|
2018-10-02 01:53:34 +00:00
|
|
|
env,
|
|
|
|
cwd: path.resolve(__dirname, '../spec'),
|
|
|
|
stdio: 'inherit'
|
2018-09-13 15:30:12 +00:00
|
|
|
})
|
2018-10-02 01:53:34 +00:00
|
|
|
if (status !== 0) {
|
|
|
|
throw new Error('Failed to npm install in the spec folder')
|
|
|
|
}
|
|
|
|
}
|
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')))
|
|
|
|
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package-lock.json')))
|
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)
|
|
|
|
})
|