hooks.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict'
  2. const config = require('./config.json')
  3. const hooks = require('../hooks')
  4. const packager = require('..')
  5. const test = require('ava')
  6. const util = require('./_util')
  7. function hookTest (wantHookCalled, hookName, t, opts) {
  8. let hookCalled = false
  9. opts.dir = util.fixtureSubdir('basic')
  10. opts.electronVersion = config.version
  11. opts.arch = 'ia32'
  12. opts.platform = 'all'
  13. opts[hookName] = [(buildPath, electronVersion, platform, arch, callback) => {
  14. hookCalled = true
  15. t.is(electronVersion, opts.electronVersion, `${hookName} electronVersion should be the same as the options object`)
  16. t.is(arch, opts.arch, `${hookName} arch should be the same as the options object`)
  17. callback()
  18. }]
  19. // 2 packages will be built during this test
  20. return packager(opts)
  21. .then(finalPaths => {
  22. t.is(finalPaths.length, 2, 'packager call should resolve with expected number of paths')
  23. t.is(wantHookCalled, hookCalled, `${hookName} methods ${wantHookCalled ? 'should' : 'should not'} have been called`)
  24. return util.verifyPackageExistence(finalPaths)
  25. }).then(exists => t.deepEqual(exists, [true, true], 'Packages should be generated for both 32-bit platforms'))
  26. }
  27. function createHookTest (hookName) {
  28. util.packagerTest(`platform=all test (one arch) (${hookName} hook)`,
  29. (t, opts) => hookTest(true, hookName, t, opts))
  30. }
  31. createHookTest('afterCopy')
  32. createHookTest('afterPrune')
  33. createHookTest('afterExtract')
  34. test('promisifyHooks executes functions in parallel', t => {
  35. let output = '0'
  36. const timeoutFunc = (number, msTimeout) => {
  37. return done => {
  38. setTimeout(() => {
  39. output += ` ${number}`
  40. done()
  41. }, msTimeout)
  42. }
  43. }
  44. const testHooks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(number =>
  45. timeoutFunc(number, number % 2 === 0 ? 1000 : 0)
  46. )
  47. return hooks.promisifyHooks(testHooks)
  48. .then(() => t.not(output, '0 1 2 3 4 5 6 7 8 9 10', 'should not be in sequential order'))
  49. })
  50. test('serialHooks executes functions serially', t => {
  51. let output = '0'
  52. const timeoutFunc = (number, msTimeout) => {
  53. return () => new Promise(resolve => { // eslint-disable-line promise/avoid-new
  54. setTimeout(() => {
  55. output += ` ${number}`
  56. resolve()
  57. }, msTimeout)
  58. })
  59. }
  60. const testHooks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(number =>
  61. timeoutFunc(number, number % 2 === 0 ? 1000 : 0)
  62. )
  63. return hooks.serialHooks(testHooks)(() => output)
  64. .then(result => t.is(result, '0 1 2 3 4 5 6 7 8 9 10', 'should be in sequential order'))
  65. })
  66. util.packagerTest('prune hook does not get called when prune=false', (t, opts) => {
  67. opts.prune = false
  68. return hookTest(false, 'afterPrune', t, opts)
  69. })