spec-runner.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env node
  2. const { ElectronVersions, Installer } = require('@electron/fiddle-core');
  3. const chalk = require('chalk');
  4. const { hashElement } = require('folder-hash');
  5. const minimist = require('minimist');
  6. const childProcess = require('node:child_process');
  7. const crypto = require('node:crypto');
  8. const fs = require('node:fs');
  9. const os = require('node:os');
  10. const path = require('node:path');
  11. const unknownFlags = [];
  12. const pass = chalk.green('✓');
  13. const fail = chalk.red('✗');
  14. const args = minimist(process.argv, {
  15. string: ['runners', 'target', 'electronVersion'],
  16. unknown: arg => unknownFlags.push(arg)
  17. });
  18. const unknownArgs = [];
  19. for (const flag of unknownFlags) {
  20. unknownArgs.push(flag);
  21. const onlyFlag = flag.replace(/^-+/, '');
  22. if (args[onlyFlag]) {
  23. unknownArgs.push(args[onlyFlag]);
  24. }
  25. }
  26. const utils = require('./lib/utils');
  27. const { YARN_VERSION } = require('./yarn');
  28. const BASE = path.resolve(__dirname, '../..');
  29. const NPX_CMD = process.platform === 'win32' ? 'npx.cmd' : 'npx';
  30. const runners = new Map([
  31. ['main', { description: 'Main process specs', run: runMainProcessElectronTests }]
  32. ]);
  33. const specHashPath = path.resolve(__dirname, '../spec/.hash');
  34. if (args.electronVersion) {
  35. if (args.runners && args.runners !== 'main') {
  36. console.log(`${fail} only 'main' runner can be used with --electronVersion`);
  37. process.exit(1);
  38. }
  39. args.runners = 'main';
  40. }
  41. let runnersToRun = null;
  42. if (args.runners !== undefined) {
  43. runnersToRun = args.runners.split(',').filter(value => value);
  44. if (!runnersToRun.every(r => [...runners.keys()].includes(r))) {
  45. console.log(`${fail} ${runnersToRun} must be a subset of [${[...runners.keys()].join(' | ')}]`);
  46. process.exit(1);
  47. }
  48. console.log('Only running:', runnersToRun);
  49. } else {
  50. console.log(`Triggering runners: ${[...runners.keys()].join(', ')}`);
  51. }
  52. async function main () {
  53. if (args.electronVersion) {
  54. const versions = await ElectronVersions.create();
  55. if (args.electronVersion === 'latest') {
  56. args.electronVersion = versions.latest.version;
  57. } else if (args.electronVersion.startsWith('latest@')) {
  58. const majorVersion = parseInt(args.electronVersion.slice('latest@'.length));
  59. const ver = versions.inMajor(majorVersion).slice(-1)[0];
  60. if (ver) {
  61. args.electronVersion = ver.version;
  62. } else {
  63. console.log(`${fail} '${majorVersion}' is not a recognized Electron major version`);
  64. process.exit(1);
  65. }
  66. } else if (!versions.isVersion(args.electronVersion)) {
  67. console.log(`${fail} '${args.electronVersion}' is not a recognized Electron version`);
  68. process.exit(1);
  69. }
  70. const versionString = `v${args.electronVersion}`;
  71. console.log(`Running against Electron ${versionString.green}`);
  72. }
  73. const [lastSpecHash, lastSpecInstallHash] = loadLastSpecHash();
  74. const [currentSpecHash, currentSpecInstallHash] = await getSpecHash();
  75. const somethingChanged = (currentSpecHash !== lastSpecHash) ||
  76. (lastSpecInstallHash !== currentSpecInstallHash);
  77. if (somethingChanged) {
  78. await installSpecModules(path.resolve(__dirname, '..', 'spec'));
  79. await getSpecHash().then(saveSpecHash);
  80. }
  81. if (!fs.existsSync(path.resolve(__dirname, '../electron.d.ts'))) {
  82. console.log('Generating electron.d.ts as it is missing');
  83. generateTypeDefinitions();
  84. }
  85. await runElectronTests();
  86. }
  87. function generateTypeDefinitions () {
  88. const { status } = childProcess.spawnSync('npm', ['run', 'create-typescript-definitions'], {
  89. cwd: path.resolve(__dirname, '..'),
  90. stdio: 'inherit',
  91. shell: true
  92. });
  93. if (status !== 0) {
  94. throw new Error(`Electron typescript definition generation failed with exit code: ${status}.`);
  95. }
  96. }
  97. function loadLastSpecHash () {
  98. return fs.existsSync(specHashPath)
  99. ? fs.readFileSync(specHashPath, 'utf8').split('\n')
  100. : [null, null];
  101. }
  102. function saveSpecHash ([newSpecHash, newSpecInstallHash]) {
  103. fs.writeFileSync(specHashPath, `${newSpecHash}\n${newSpecInstallHash}`);
  104. }
  105. async function runElectronTests () {
  106. const errors = [];
  107. const testResultsDir = process.env.ELECTRON_TEST_RESULTS_DIR;
  108. for (const [runnerId, { description, run }] of runners) {
  109. if (runnersToRun && !runnersToRun.includes(runnerId)) {
  110. console.info('\nSkipping:', description);
  111. continue;
  112. }
  113. try {
  114. console.info('\nRunning:', description);
  115. if (testResultsDir) {
  116. process.env.MOCHA_FILE = path.join(testResultsDir, `test-results-${runnerId}.xml`);
  117. }
  118. await run();
  119. } catch (err) {
  120. errors.push([runnerId, err]);
  121. }
  122. }
  123. if (errors.length !== 0) {
  124. for (const err of errors) {
  125. console.error('\n\nRunner Failed:', err[0]);
  126. console.error(err[1]);
  127. }
  128. console.log(`${fail} Electron test runners have failed`);
  129. process.exit(1);
  130. }
  131. }
  132. async function runTestUsingElectron (specDir, testName) {
  133. let exe;
  134. if (args.electronVersion) {
  135. const installer = new Installer();
  136. exe = await installer.install(args.electronVersion);
  137. } else {
  138. exe = path.resolve(BASE, utils.getElectronExec());
  139. }
  140. const runnerArgs = [`electron/${specDir}`, ...unknownArgs.slice(2)];
  141. if (process.platform === 'linux') {
  142. runnerArgs.unshift(path.resolve(__dirname, 'dbus_mock.py'), exe);
  143. exe = 'python3';
  144. }
  145. const { status, signal } = childProcess.spawnSync(exe, runnerArgs, {
  146. cwd: path.resolve(__dirname, '../..'),
  147. stdio: 'inherit'
  148. });
  149. if (status !== 0) {
  150. if (status) {
  151. const textStatus = process.platform === 'win32' ? `0x${status.toString(16)}` : status.toString();
  152. console.log(`${fail} Electron tests failed with code ${textStatus}.`);
  153. } else {
  154. console.log(`${fail} Electron tests failed with kill signal ${signal}.`);
  155. }
  156. process.exit(1);
  157. }
  158. console.log(`${pass} Electron ${testName} process tests passed.`);
  159. }
  160. async function runMainProcessElectronTests () {
  161. await runTestUsingElectron('spec', 'main');
  162. }
  163. async function installSpecModules (dir) {
  164. const env = {
  165. ...process.env,
  166. CXXFLAGS: process.env.CXXFLAGS,
  167. npm_config_msvs_version: '2022',
  168. npm_config_yes: 'true'
  169. };
  170. if (args.electronVersion) {
  171. env.npm_config_target = args.electronVersion;
  172. env.npm_config_disturl = 'https://electronjs.org/headers';
  173. env.npm_config_runtime = 'electron';
  174. env.npm_config_devdir = path.join(os.homedir(), '.electron-gyp');
  175. env.npm_config_build_from_source = 'true';
  176. const { status } = childProcess.spawnSync('npm', ['run', 'node-gyp-install', '--ensure'], {
  177. env,
  178. cwd: dir,
  179. stdio: 'inherit',
  180. shell: true
  181. });
  182. if (status !== 0) {
  183. console.log(`${fail} Failed to "npm run node-gyp-install" install in '${dir}'`);
  184. process.exit(1);
  185. }
  186. } else {
  187. env.npm_config_nodedir = path.resolve(BASE, `out/${utils.getOutDir({ shouldLog: true })}/gen/node_headers`);
  188. }
  189. if (fs.existsSync(path.resolve(dir, 'node_modules'))) {
  190. await fs.promises.rm(path.resolve(dir, 'node_modules'), { force: true, recursive: true });
  191. }
  192. const { status } = childProcess.spawnSync(NPX_CMD, [`yarn@${YARN_VERSION}`, 'install', '--frozen-lockfile'], {
  193. env,
  194. cwd: dir,
  195. stdio: 'inherit',
  196. shell: process.platform === 'win32'
  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. });