chore: some async await stuff for the spec runner (#14897)

This commit is contained in:
Alexey Kuzmin 2018-10-02 03:53:34 +02:00 committed by Samuel Attard
parent 6a3402be5f
commit 07161a8452

View file

@ -1,6 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
const cp = require('child_process') const childProcess = require('child_process')
const crypto = require('crypto') const crypto = require('crypto')
const fs = require('fs') const fs = require('fs')
const { hashElement } = require('folder-hash') const { hashElement } = require('folder-hash')
@ -13,68 +13,87 @@ const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
const specHashPath = path.resolve(__dirname, '../spec/.hash') const specHashPath = path.resolve(__dirname, '../spec/.hash')
const [lastSpecHash, lastSpecInstallHash] = fs.existsSync(specHashPath) async function main () {
? fs.readFileSync(specHashPath, 'utf8').split('\n') const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash()
: ['invalid', 'invalid'] const [currentSpecHash, currentSpecInstallHash] = await getSpecHash()
const somethingChanged = (currentSpecHash !== lastSpecHash) ||
(lastSpecInstallHash !== currentSpecInstallHash)
getSpecHash().then(([currentSpecHash, currentSpecInstallHash]) => { if (somethingChanged) {
const specChanged = currentSpecHash !== lastSpecHash await installSpecModules()
const installChanged = lastSpecInstallHash !== currentSpecInstallHash await getSpecHash().then(saveSpecHash)
if (specChanged || installChanged) {
const out = cp.spawnSync(NPM_CMD, ['install'], {
env: Object.assign({}, process.env, {
npm_config_nodedir: path.resolve(BASE, `out/${utils.OUT_DIR}/gen/node_headers`),
npm_config_msvs_version: '2017'
}),
cwd: path.resolve(__dirname, '../spec'),
stdio: 'inherit'
})
if (out.status !== 0) {
console.error('Failed to npm install in the spec folder')
process.exit(1)
}
return getSpecHash()
.then(([newSpecHash, newSpecInstallHash]) => {
fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`)
})
} }
}).then(() => {
await runElectronTests()
}
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 () {
let exe = path.resolve(BASE, utils.getElectronExec()) let exe = path.resolve(BASE, utils.getElectronExec())
const args = process.argv.slice(2) const args = process.argv.slice(2)
if (process.platform === 'linux') { if (process.platform === 'linux') {
args.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe) args.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
exe = 'python' exe = 'python'
} }
const child = cp.spawn(exe, args, {
const { status } = childProcess.spawnSync(exe, args, {
cwd: path.resolve(__dirname, '../..'), cwd: path.resolve(__dirname, '../..'),
stdio: 'inherit' stdio: 'inherit'
}) })
child.on('exit', (code) => { if (status !== 0) {
process.exit(code) 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'
}) })
}).catch((error) => { const { status } = childProcess.spawnSync(NPM_CMD, ['install'], {
console.error('An error occurred inside the spec runner', error) env,
process.exit(1) cwd: path.resolve(__dirname, '../spec'),
}) stdio: 'inherit'
})
if (status !== 0) {
throw new Error('Failed to npm install in the spec folder')
}
}
function getSpecHash () { function getSpecHash () {
return Promise.all([ return Promise.all([
new Promise((resolve) => { (async () => {
const hasher = crypto.createHash('SHA256') const hasher = crypto.createHash('SHA256')
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json'))) hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package-lock.json'))) hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package-lock.json')))
resolve(hasher.digest('hex')) return hasher.digest('hex')
}), })(),
new Promise((resolve, reject) => { (async () => {
const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules') const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
if (!fs.existsSync(specNodeModulesPath)) { if (!fs.existsSync(specNodeModulesPath)) {
return resolve('invalid') return null
} }
hashElement(specNodeModulesPath, { const { hash } = await hashElement(specNodeModulesPath, {
folders: { folders: {
exclude: ['.bin'] exclude: ['.bin']
} }
}).then((result) => resolve(result.hash)).catch(reject) })
}) return hash
})()
]) ])
} }
main().catch((error) => {
console.error('An error occurred inside the spec runner:', error)
process.exit(1)
})