_util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict'
  2. // Keeping this module because it handles non-buffers gracefully
  3. const bufferEqual = require('buffer-equal')
  4. const common = require('../common')
  5. const config = require('./config.json')
  6. const fs = require('fs-extra')
  7. const packager = require('../index')
  8. const path = require('path')
  9. const setup = require('./_setup')
  10. const tempy = require('tempy')
  11. const test = require('ava')
  12. const ORIGINAL_CWD = process.cwd()
  13. test.before(t => {
  14. if (!process.env.CI) {
  15. return setup.setupTestsuite()
  16. .then(() => process.chdir(setup.WORK_CWD))
  17. }
  18. return Promise.resolve(process.chdir(setup.WORK_CWD))
  19. })
  20. test.after.always(t => {
  21. process.chdir(ORIGINAL_CWD)
  22. return fs.remove(setup.WORK_CWD)
  23. })
  24. test.beforeEach(t => {
  25. t.context.workDir = tempy.directory()
  26. t.context.tempDir = tempy.directory()
  27. })
  28. test.afterEach.always(t => {
  29. return fs.remove(t.context.workDir)
  30. .then(() => fs.remove(t.context.tempDir))
  31. })
  32. function testSinglePlatform (name, testFunction, testFunctionArgs, parallel) {
  33. module.exports.packagerTest(name, (t, opts) => {
  34. Object.assign(opts, module.exports.singlePlatformOptions())
  35. return testFunction.apply(null, [t, opts].concat(testFunctionArgs))
  36. }, parallel)
  37. }
  38. module.exports = {
  39. allPlatformArchCombosCount: 9,
  40. areFilesEqual: function areFilesEqual (file1, file2) {
  41. let buffer1, buffer2
  42. return fs.readFile(file1)
  43. .then((data) => {
  44. buffer1 = data
  45. return fs.readFile(file2)
  46. }).then((data) => {
  47. buffer2 = data
  48. return bufferEqual(buffer1, buffer2)
  49. })
  50. },
  51. fixtureSubdir: setup.fixtureSubdir,
  52. generateResourcesPath: function generateResourcesPath (opts) {
  53. return common.isPlatformMac(opts.platform)
  54. ? path.join(opts.name + '.app', 'Contents', 'Resources')
  55. : 'resources'
  56. },
  57. invalidOptionTest: function invalidOptionTest (opts) {
  58. return t => t.throws(packager(opts))
  59. },
  60. packageAndEnsureResourcesPath: function packageAndEnsureResourcesPath (t, opts) {
  61. let resourcesPath
  62. return packager(opts)
  63. .then(paths => {
  64. resourcesPath = path.join(paths[0], module.exports.generateResourcesPath(opts))
  65. return fs.stat(resourcesPath)
  66. }).then(stats => {
  67. t.true(stats.isDirectory(), 'The output directory should contain the expected resources subdirectory')
  68. return resourcesPath
  69. })
  70. },
  71. packagerTest: function packagerTest (name, testFunction, parallel) {
  72. const testDefinition = parallel ? test : test.serial
  73. testDefinition(name, t => {
  74. return testFunction(t, {
  75. name: 'packagerTest',
  76. out: t.context.workDir,
  77. tmpdir: t.context.tempDir
  78. })
  79. })
  80. },
  81. singlePlatformOptions: function singlePlatformOptions () {
  82. return {
  83. platform: 'linux',
  84. arch: 'x64',
  85. electronVersion: config.version
  86. }
  87. },
  88. // Rest parameters are added (not behind a feature flag) in Node 6
  89. testSinglePlatform: function (name, testFunction /*, ...testFunctionArgs */) {
  90. const testFunctionArgs = Array.prototype.slice.call(arguments, 2)
  91. return testSinglePlatform(name, testFunction, testFunctionArgs, false)
  92. },
  93. // Rest parameters are added (not behind a feature flag) in Node 6
  94. testSinglePlatformParallel: function (name, testFunction /*, ...testFunctionArgs */) {
  95. const testFunctionArgs = Array.prototype.slice.call(arguments, 2)
  96. return testSinglePlatform(name, testFunction, testFunctionArgs, true)
  97. },
  98. verifyPackageExistence: function verifyPackageExistence (finalPaths) {
  99. return Promise.all(finalPaths.map((finalPath) => {
  100. return fs.stat(finalPath)
  101. .then(
  102. stats => stats.isDirectory(),
  103. () => false
  104. )
  105. }))
  106. }
  107. }