spec-runner.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env node
  2. const childProcess = require('child_process')
  3. const crypto = require('crypto')
  4. const fs = require('fs')
  5. const { hashElement } = require('folder-hash')
  6. const path = require('path')
  7. const utils = require('./lib/utils')
  8. const BASE = path.resolve(__dirname, '../..')
  9. const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
  10. const specHashPath = path.resolve(__dirname, '../spec/.hash')
  11. async function main () {
  12. const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash()
  13. const [currentSpecHash, currentSpecInstallHash] = await getSpecHash()
  14. const somethingChanged = (currentSpecHash !== lastSpecHash) ||
  15. (lastSpecInstallHash !== currentSpecInstallHash)
  16. if (somethingChanged) {
  17. await installSpecModules()
  18. await getSpecHash().then(saveSpecHash)
  19. }
  20. await runElectronTests()
  21. }
  22. function loadLastSpecHash () {
  23. return fs.existsSync(specHashPath)
  24. ? fs.readFileSync(specHashPath, 'utf8').split('\n')
  25. : [null, null]
  26. }
  27. function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
  28. fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`)
  29. }
  30. async function runElectronTests () {
  31. let exe = path.resolve(BASE, utils.getElectronExec())
  32. const args = process.argv.slice(2)
  33. if (process.platform === 'linux') {
  34. args.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
  35. exe = 'python'
  36. }
  37. const { status } = childProcess.spawnSync(exe, args, {
  38. cwd: path.resolve(__dirname, '../..'),
  39. stdio: 'inherit'
  40. })
  41. if (status !== 0) {
  42. throw new Error(`Electron tests failed with code ${status}.`)
  43. }
  44. }
  45. async function installSpecModules () {
  46. const nodeDir = path.resolve(BASE, `out/${utils.OUT_DIR}/gen/node_headers`)
  47. const env = Object.assign({}, process.env, {
  48. npm_config_nodedir: nodeDir,
  49. npm_config_msvs_version: '2017'
  50. })
  51. const { status } = childProcess.spawnSync(NPM_CMD, ['install'], {
  52. env,
  53. cwd: path.resolve(__dirname, '../spec'),
  54. stdio: 'inherit'
  55. })
  56. if (status !== 0) {
  57. throw new Error('Failed to npm install in the spec folder')
  58. }
  59. }
  60. function getSpecHash () {
  61. return Promise.all([
  62. (async () => {
  63. const hasher = crypto.createHash('SHA256')
  64. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
  65. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package-lock.json')))
  66. return hasher.digest('hex')
  67. })(),
  68. (async () => {
  69. const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
  70. if (!fs.existsSync(specNodeModulesPath)) {
  71. return null
  72. }
  73. const { hash } = await hashElement(specNodeModulesPath, {
  74. folders: {
  75. exclude: ['.bin']
  76. }
  77. })
  78. return hash
  79. })()
  80. ])
  81. }
  82. main().catch((error) => {
  83. console.error('An error occurred inside the spec runner:', error)
  84. process.exit(1)
  85. })