spec-runner.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env node
  2. const childProcess = require('child_process');
  3. const crypto = require('crypto');
  4. const fs = require('fs-extra');
  5. const { hashElement } = require('folder-hash');
  6. const path = require('path');
  7. const unknownFlags = [];
  8. require('colors');
  9. const pass = '✓'.green;
  10. const fail = '✗'.red;
  11. const args = require('minimist')(process.argv, {
  12. string: ['runners', 'target'],
  13. boolean: ['buildNativeTests'],
  14. unknown: arg => unknownFlags.push(arg)
  15. });
  16. const unknownArgs = [];
  17. for (const flag of unknownFlags) {
  18. unknownArgs.push(flag);
  19. const onlyFlag = flag.replace(/^-+/, '');
  20. if (args[onlyFlag]) {
  21. unknownArgs.push(args[onlyFlag]);
  22. }
  23. }
  24. const utils = require('./lib/utils');
  25. const { YARN_VERSION } = require('./yarn');
  26. const BASE = path.resolve(__dirname, '../..');
  27. const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx';
  28. const runners = new Map([
  29. ['main', { description: 'Main process specs', run: runMainProcessElectronTests }],
  30. ['native', { description: 'Native specs', run: runNativeElectronTests }]
  31. ]);
  32. const specHashPath = path.resolve(__dirname, '../spec/.hash');
  33. let runnersToRun = null;
  34. if (args.runners !== undefined) {
  35. runnersToRun = args.runners.split(',').filter(value => value);
  36. if (!runnersToRun.every(r => [...runners.keys()].includes(r))) {
  37. console.log(`${fail} ${runnersToRun} must be a subset of [${[...runners.keys()].join(' | ')}]`);
  38. process.exit(1);
  39. }
  40. console.log('Only running:', runnersToRun);
  41. } else {
  42. console.log(`Triggering runners: ${[...runners.keys()].join(', ')}`);
  43. }
  44. async function main () {
  45. const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash();
  46. const [currentSpecHash, currentSpecInstallHash] = await getSpecHash();
  47. const somethingChanged = (currentSpecHash !== lastSpecHash) ||
  48. (lastSpecInstallHash !== currentSpecInstallHash);
  49. if (somethingChanged) {
  50. await installSpecModules(path.resolve(__dirname, '..', 'spec'));
  51. await getSpecHash().then(saveSpecHash);
  52. }
  53. if (!fs.existsSync(path.resolve(__dirname, '../electron.d.ts'))) {
  54. console.log('Generating electron.d.ts as it is missing');
  55. generateTypeDefinitions();
  56. }
  57. await runElectronTests();
  58. }
  59. function generateTypeDefinitions () {
  60. const { status } = childProcess.spawnSync('npm', ['run', 'create-typescript-definitions'], {
  61. cwd: path.resolve(__dirname, '..'),
  62. stdio: 'inherit',
  63. shell: true
  64. });
  65. if (status !== 0) {
  66. throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`);
  67. }
  68. }
  69. function loadLastSpecHash () {
  70. return fs.existsSync(specHashPath)
  71. ? fs.readFileSync(specHashPath, 'utf8').split('\n')
  72. : [null, null];
  73. }
  74. function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
  75. fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`);
  76. }
  77. async function runElectronTests () {
  78. const errors = [];
  79. const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR;
  80. for (const [runnerId, { description, run }] of runners) {
  81. if (runnersToRun && !runnersToRun.includes(runnerId)) {
  82. console.info('\nSkipping:', description);
  83. continue;
  84. }
  85. try {
  86. console.info('\nRunning:', description);
  87. if (testResultsDir) {
  88. process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`);
  89. }
  90. await run();
  91. } catch (err) {
  92. errors.push([runnerId, err]);
  93. }
  94. }
  95. if (errors.length !== 0) {
  96. for (const err of errors) {
  97. console.error('\n\nRunner Failed:', err[0]);
  98. console.error(err[1]);
  99. }
  100. console.log(`${fail} Electron test runners have failed`);
  101. process.exit(1);
  102. }
  103. }
  104. async function runTestUsingElectron (specDir, testName) {
  105. let exe = path.resolve(BASE, utils.getElectronExec());
  106. const runnerArgs = [`electron/${specDir}`, ...unknownArgs.slice(2)];
  107. if (process.platform === 'linux') {
  108. runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe);
  109. exe = 'python3';
  110. }
  111. const { status, signal } = childProcess.spawnSync(exe, runnerArgs, {
  112. cwd: path.resolve(__dirname, '../..'),
  113. stdio: 'inherit'
  114. });
  115. if (status !== 0) {
  116. if (status) {
  117. const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString();
  118. console.log(`${fail} Electron tests failed with code ${textStatus}.`);
  119. } else {
  120. console.log(`${fail} Electron tests failed with kill signal ${signal}.`);
  121. }
  122. process.exit(1);
  123. }
  124. console.log(`${pass} Electron ${testName} process tests passed.`);
  125. }
  126. const specFilter = (file) => {
  127. if (!/-spec\.[tj]s$/.test(file)) {
  128. return false;
  129. } else {
  130. return true;
  131. }
  132. };
  133. async function runNativeElectronTests () {
  134. let testTargets = require('./native-test-targets.json');
  135. const outDir = `out/${utils.getOutDir()}`;
  136. // If native tests are being run, only one arg would be relevant
  137. if (args.target && !testTargets.includes(args.target)) {
  138. console.log(`${fail} ${args.target} must be a subset of [${[testTargets].join(', ')}]`);
  139. process.exit(1);
  140. }
  141. // Optionally build all native test targets
  142. if (args.buildNativeTests) {
  143. for (const target of testTargets) {
  144. const build = childProcess.spawnSync('ninja', ['-C', outDir, target], {
  145. cwd: path.resolve(__dirname, '../..'),
  146. stdio: 'inherit'
  147. });
  148. // Exit if test target failed to build
  149. if (build.status !== 0) {
  150. console.log(`${fail} ${target} failed to build.`);
  151. process.exit(1);
  152. }
  153. }
  154. }
  155. // If a specific target was passed, only build and run that target
  156. if (args.target) testTargets = [args.target];
  157. // Run test targets
  158. const failures = [];
  159. for (const target of testTargets) {
  160. console.info('\nRunning native test for target:', target);
  161. const testRun = childProcess.spawnSync(`./${outDir}/${target}`, {
  162. cwd: path.resolve(__dirname, '../..'),
  163. stdio: 'inherit'
  164. });
  165. // Collect failures and log at end
  166. if (testRun.status !== 0) failures.push({ target });
  167. }
  168. // Exit if any failures
  169. if (failures.length > 0) {
  170. console.log(`${fail} Electron native tests failed for the following targets: `, failures);
  171. process.exit(1);
  172. }
  173. console.log(`${pass} Electron native tests passed.`);
  174. }
  175. async function runMainProcessElectronTests () {
  176. await runTestUsingElectron('spec', 'main');
  177. }
  178. async function installSpecModules (dir) {
  179. // v8 headers use c++17 so override the gyp default of -std=c++14,
  180. // but don't clobber any other CXXFLAGS that were passed into spec-runner.js
  181. const CXXFLAGS = ['-std=c++17', process.env.CXXFLAGS].filter(x => !!x).join(' ');
  182. const nodeDir = path.resolve(BASE, `out/${utils.getOutDir({ shouldLog: true })}/gen/node_headers`);
  183. const env = {
  184. ...process.env,
  185. CXXFLAGS,
  186. npm_config_nodedir: nodeDir,
  187. npm_config_msvs_version: '2019',
  188. npm_config_yes: 'true'
  189. };
  190. if (fs.existsSync(path.resolve(dir, 'node_modules'))) {
  191. await fs.remove(path.resolve(dir, 'node_modules'));
  192. }
  193. const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
  194. env,
  195. cwd: dir,
  196. stdio: 'inherit'
  197. });
  198. if (status !== 0 && !process.env.IGNORE_YARN_INSTALL_ERROR) {
  199. console.log(`${fail} Failed to yarn install in '${dir}'`);
  200. process.exit(1);
  201. }
  202. }
  203. function getSpecHash () {
  204. return Promise.all([
  205. (async () => {
  206. const hasher = crypto.createHash('SHA256');
  207. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/package.json')));
  208. hasher.update(fs.readFileSync(path.resolve(__dirname, '../spec/yarn.lock')));
  209. hasher.update(fs.readFileSync(path.resolve(__dirname, '../script/spec-runner.js')));
  210. return hasher.digest('hex');
  211. })(),
  212. (async () => {
  213. const specNodeModulesPath = path.resolve(__dirname, '../spec/node_modules');
  214. if (!fs.existsSync(specNodeModulesPath)) {
  215. return null;
  216. }
  217. const { hash } = await hashElement(specNodeModulesPath, {
  218. folders: {
  219. exclude: ['.bin']
  220. }
  221. });
  222. return hash;
  223. })()
  224. ]);
  225. }
  226. main().catch((error) => {
  227. console.error('An error occurred inside the spec runner:', error);
  228. process.exit(1);
  229. });