_setup.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict'
  2. const common = require('../common')
  3. const download = require('../download')
  4. const config = require('./config.json')
  5. const exec = require('mz/child_process').exec
  6. const fs = require('fs-extra')
  7. const os = require('os')
  8. const path = require('path')
  9. const targets = require('../targets')
  10. function fixtureSubdir (subdir) {
  11. return path.join(__dirname, 'fixtures', subdir)
  12. }
  13. /**
  14. * Skip testing darwin/mas target on Windows since Electron Packager itself skips it
  15. * (see https://github.com/electron-userland/electron-packager/issues/71)
  16. */
  17. function skipDownloadingMacZips (platform, arch) {
  18. return common.isPlatformMac(platform) && process.platform === 'win32'
  19. }
  20. function downloadAll (version) {
  21. console.log(`Calling electron-download for ${version} before running tests...`)
  22. const combinations = download.createDownloadCombos({electronVersion: config.version, all: true}, targets.officialPlatforms, targets.officialArchs, skipDownloadingMacZips)
  23. return Promise.all(combinations.map(combination => downloadElectronZip(version, combination)))
  24. }
  25. function downloadElectronZip (version, options) {
  26. return download.downloadElectronZip(Object.assign({}, options, {
  27. cache: path.join(os.homedir(), '.electron'),
  28. quiet: !!process.env.CI,
  29. version: version
  30. }))
  31. }
  32. function downloadMASLoginHelperElectronZip () {
  33. if (process.platform !== 'win32') {
  34. const version = '2.0.0-beta.1'
  35. console.log(`Calling electron-download for ${version} (MAS only) before running tests...`)
  36. return downloadElectronZip(version, { platform: 'mas', arch: 'x64' })
  37. }
  38. }
  39. /**
  40. * Download all Electron distributions before running tests to avoid timing out due to network
  41. * speed. Most tests run with the config.json version, but we have some tests using 0.37.4, an
  42. * `electron` module specific test using 1.3.1., and an MAS-specific test using 2.0.0-beta.1.
  43. */
  44. function preDownloadElectron () {
  45. const versions = [
  46. config.version,
  47. '0.37.4',
  48. '1.3.1'
  49. ]
  50. return Promise.all(versions.map(downloadAll))
  51. .then(downloadMASLoginHelperElectronZip)
  52. }
  53. function npmInstallForFixture (fixture) {
  54. const fixtureDir = fixtureSubdir(fixture)
  55. return fs.exists(path.join(fixtureDir, 'node_modules'))
  56. .then(exists => {
  57. if (exists) {
  58. return true
  59. } else {
  60. console.log(`Running npm install in fixtures/${fixture}...`)
  61. return exec('npm install --no-bin-links', {cwd: fixtureDir})
  62. }
  63. })
  64. }
  65. function npmInstallForFixtures () {
  66. const fixtures = [
  67. 'basic',
  68. 'basic-renamed-to-electron',
  69. 'electron-in-dependencies',
  70. 'infer-missing-version-only',
  71. 'el-0374'
  72. ]
  73. return Promise.all(fixtures.map(npmInstallForFixture))
  74. }
  75. const WORK_CWD = path.join(__dirname, 'work')
  76. function ensureEmptyWorkDirExists () {
  77. return fs.remove(WORK_CWD)
  78. .then(() => fs.mkdirs(WORK_CWD))
  79. }
  80. module.exports = {
  81. fixtureSubdir: fixtureSubdir,
  82. setupTestsuite: function setupTestsuite () {
  83. return preDownloadElectron()
  84. .then(npmInstallForFixtures)
  85. .catch(error => {
  86. console.error(error.stack || error)
  87. return process.exit(1)
  88. })
  89. .then(ensureEmptyWorkDirExists)
  90. },
  91. WORK_CWD: WORK_CWD
  92. }