spec-runner.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 unknownFlags = []
  8. const args = require('minimist')(process.argv, {
  9. string: ['runners'],
  10. unknown: arg => unknownFlags.push(arg)
  11. })
  12. const unknownArgs = []
  13. for (const flag of unknownFlags) {
  14. unknownArgs.push(flag)
  15. const onlyFlag = flag.replace(/^-+/, '')
  16. if (args[onlyFlag]) {
  17. unknownArgs.push(args[onlyFlag])
  18. }
  19. }
  20. const utils = require('./lib/utils')
  21. const { YARN_VERSION } = require('./yarn')
  22. const BASE = path.resolve(__dirname, '../..')
  23. const NPM_CMD = process.platform === 'win32' ? 'npm.cmd' : 'npm'
  24. const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx'
  25. const specHashPath = path.resolve(__dirname, '../spec/.hash')
  26. let runnersToRun = null
  27. if (args.runners) {
  28. runnersToRun = args.runners.split(',')
  29. console.log('Only running:', runnersToRun)
  30. } else {
  31. console.log('Will trigger all spec runners')
  32. }
  33. async function main () {
  34. const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash()
  35. const [currentSpecHash, currentSpecInstallHash] = await getSpecHash()
  36. const somethingChanged = (currentSpecHash !== lastSpecHash) ||
  37. (lastSpecInstallHash !== currentSpecInstallHash)
  38. if (somethingChanged) {
  39. await installSpecModules()
  40. await getSpecHash().then(saveSpecHash)
  41. }
  42. if (!fs.existsSync(path.resolve(__dirname, '../electron.d.ts'))) {
  43. console.log('Generating electron.d.ts as it is missing')
  44. generateTypeDefinitions()
  45. }
  46. await runElectronTests()
  47. }
  48. function generateTypeDefinitions () {
  49. const { status } = childProcess.spawnSync('npm', ['run', 'create-typescript-definitions'], {
  50. cwd: path.resolve(__dirname, '..'),
  51. stdio: 'inherit',
  52. shell: true
  53. })
  54. if (status !== 0) {
  55. throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`)
  56. }
  57. }
  58. function loadLastSpecHash () {
  59. return fs.existsSync(specHashPath)
  60. ? fs.readFileSync(specHashPath, 'utf8').split('\n')
  61. : [null, null]
  62. }
  63. function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
  64. fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`)
  65. }
  66. async function runElectronTests () {
  67. const errors = []
  68. const runners = new Map([
  69. ['main', { description: 'Main process specs', run: runMainProcessElectronTests }],
  70. ['remote', { description: 'Remote based specs', run: runRemoteBasedElectronTests }]
  71. ])
  72. const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR
  73. for (const [runnerId, { description, run }] of runners) {
  74. if (runnersToRun && !runnersToRun.includes(runnerId)) {
  75. console.info('\nSkipping:', description)
  76. continue
  77. }
  78. try {
  79. console.info('\nRunning:', description)
  80. if (testResultsDir) {
  81. process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`)
  82. }
  83. await run()
  84. } catch (err) {
  85. errors.push([runnerId, err])
  86. }
  87. }
  88. if (errors.length !== 0) {
  89. for (const err of errors) {
  90. console.error('\n\nRunner Failed:', err[0])
  91. console.error(err[1])
  92. }
  93. throw new Error('Electron test runners have failed')
  94. }
  95. }
  96. async function runRemoteBasedElectronTests () {
  97. let exe = path.resolve(BASE, utils.getElectronExec())
  98. const runnerArgs = ['electron/spec', ...unknownArgs.slice(2)]
  99. if (process.platform === 'linux') {
  100. runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe)
  101. exe = 'python'
  102. }
  103. const { status } = childProcess.spawnSync(exe, runnerArgs, {
  104. cwd: path.resolve(__dirname, '../..'),
  105. stdio: 'inherit'
  106. })
  107. if (status !== 0) {
  108. throw new Error(`Electron tests failed with code ${status}.`)
  109. }
  110. }
  111. async function runMainProcessElectronTests () {
  112. const exe = path.resolve(BASE, utils.getElectronExec())
  113. const { status } = childProcess.spawnSync(exe, ['electron/spec-main', ...unknownArgs.slice(2)], {
  114. cwd: path.resolve(__dirname, '../..'),
  115. stdio: 'inherit'
  116. })
  117. if (status !== 0) {
  118. throw new Error(`Electron tests failed with code ${status}.`)
  119. }
  120. }
  121. async function installSpecModules () {
  122. const nodeDir = path.resolve(BASE, `out/${utils.OUT_DIR}/gen/node_headers`)
  123. const env = Object.assign({}, process.env, {
  124. npm_config_nodedir: nodeDir,
  125. npm_config_msvs_version: '2017'
  126. })
  127. const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
  128. env,
  129. cwd: path.resolve(__dirname, '../spec'),
  130. stdio: 'inherit'
  131. })
  132. if (status !== 0) {
  133. throw new Error('Failed to npm install in the spec folder')
  134. }
  135. }
  136. function getSpecHash () {
  137. return Promise.all([
  138. (async () => {
  139. const hasher = crypto.createHash('SHA256')
  140. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')))
  141. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock')))
  142. return hasher.digest('hex')
  143. })(),
  144. (async () => {
  145. const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules')
  146. if (!fs.existsSync(specNodeModulesPath)) {
  147. return null
  148. }
  149. const { hash } = await hashElement(specNodeModulesPath, {
  150. folders: {
  151. exclude: ['.bin']
  152. }
  153. })
  154. return hash
  155. })()
  156. ])
  157. }
  158. main().catch((error) => {
  159. console.error('An error occurred inside the spec runner:', error)
  160. process.exit(1)
  161. })