spec-runner.js 5.7 KB

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