infer.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict'
  2. const fs = require('fs-extra')
  3. const getMetadataFromPackageJSON = require('../infer')
  4. const packager = require('..')
  5. const path = require('path')
  6. const pkgUp = require('pkg-up')
  7. const util = require('./_util')
  8. function inferElectronVersionTest (t, opts, fixture, packageName) {
  9. delete opts.electronVersion
  10. opts.dir = util.fixtureSubdir(fixture)
  11. return getMetadataFromPackageJSON([], opts, opts.dir)
  12. .then(() => {
  13. const packageJSON = require(path.join(opts.dir, 'package.json'))
  14. return t.is(opts.electronVersion, packageJSON.devDependencies[packageName], `The version should be inferred from installed ${packageName} version`)
  15. })
  16. }
  17. function copyFixtureToTempDir (t, fixtureSubdir) {
  18. const tmpdir = path.join(t.context.tempDir, fixtureSubdir)
  19. const fixtureDir = util.fixtureSubdir(fixtureSubdir)
  20. const tmpdirPkg = pkgUp.sync(path.join(tmpdir, '..'))
  21. if (tmpdirPkg) {
  22. throw new Error(`Found package.json in parent of temp directory, which will interfere with test results. Please remove package.json at ${tmpdirPkg}`)
  23. }
  24. return fs.emptyDir(tmpdir)
  25. .then(() => fs.copy(fixtureDir, tmpdir))
  26. .then(() => tmpdir)
  27. }
  28. function inferFailureTest (t, opts, fixtureSubdir) {
  29. return copyFixtureToTempDir(t, fixtureSubdir)
  30. .then(dir => {
  31. delete opts.name
  32. delete opts.electronVersion
  33. opts.dir = dir
  34. return t.throws(packager(opts))
  35. })
  36. }
  37. function inferMissingVersionTest (t, opts) {
  38. return copyFixtureToTempDir(t, 'infer-missing-version-only')
  39. .then(dir => {
  40. delete opts.electronVersion
  41. opts.dir = dir
  42. return getMetadataFromPackageJSON([], opts, dir)
  43. }).then(() => {
  44. const packageJSON = require(path.join(opts.dir, 'package.json'))
  45. return t.is(opts.electronVersion, packageJSON.devDependencies['electron'], 'The version should be inferred from installed electron module version')
  46. })
  47. }
  48. function testInferWin32metadata (t, opts, expected, assertionMessage) {
  49. return copyFixtureToTempDir(t, 'infer-win32metadata')
  50. .then(dir => {
  51. opts.dir = dir
  52. return getMetadataFromPackageJSON(['win32'], opts, dir)
  53. }).then(() => t.deepEqual(opts.win32metadata, expected, assertionMessage))
  54. }
  55. function testInferWin32metadataAuthorObject (t, opts, author, expected, assertionMessage) {
  56. let packageJSONFilename
  57. delete opts.name
  58. return copyFixtureToTempDir(t, 'infer-win32metadata')
  59. .then(dir => {
  60. opts.dir = dir
  61. packageJSONFilename = path.join(dir, 'package.json')
  62. return fs.readJson(packageJSONFilename)
  63. }).then(packageJSON => {
  64. packageJSON.author = author
  65. return fs.writeJson(packageJSONFilename, packageJSON)
  66. }).then(() => getMetadataFromPackageJSON(['win32'], opts, opts.dir))
  67. .then(() => t.deepEqual(opts.win32metadata, expected, assertionMessage))
  68. }
  69. util.testSinglePlatformParallel('infer using `electron-prebuilt` package', inferElectronVersionTest, 'basic', 'electron-prebuilt')
  70. util.testSinglePlatformParallel('infer using `electron-prebuilt-compile` package', inferElectronVersionTest, 'infer-electron-prebuilt-compile', 'electron-prebuilt-compile')
  71. util.testSinglePlatformParallel('infer using `electron` package only', inferMissingVersionTest)
  72. util.testSinglePlatformParallel('infer where `electron` version is preferred over `electron-prebuilt`', inferElectronVersionTest, 'basic-renamed-to-electron', 'electron')
  73. util.testSinglePlatformParallel('infer win32metadata', (t, opts) => {
  74. const expected = {CompanyName: 'Foo Bar'}
  75. return testInferWin32metadata(t, opts, expected, 'win32metadata matches package.json values')
  76. })
  77. util.testSinglePlatformParallel('do not infer win32metadata if it already exists', (t, opts) => {
  78. opts.win32metadata = {CompanyName: 'Existing'}
  79. const expected = Object.assign({}, opts.win32metadata)
  80. return testInferWin32metadata(t, opts, expected, 'win32metadata did not update with package.json values')
  81. })
  82. util.testSinglePlatformParallel('infer win32metadata when author is an object', (t, opts) => {
  83. const author = {
  84. name: 'Foo Bar Object',
  85. email: 'foobar@example.com'
  86. }
  87. const expected = {CompanyName: 'Foo Bar Object'}
  88. return testInferWin32metadataAuthorObject(t, opts, author, expected, 'win32metadata did not update with package.json values')
  89. })
  90. util.testSinglePlatformParallel('do not infer win32metadata.CompanyName when author is an object without a name', (t, opts) => {
  91. const author = {
  92. email: 'foobar@example.com'
  93. }
  94. const expected = {}
  95. return testInferWin32metadataAuthorObject(t, opts, author, expected, 'win32metadata.CompanyName should not have been inferred')
  96. })
  97. util.testSinglePlatformParallel('infer missing fields test', inferFailureTest, 'infer-missing-fields')
  98. util.testSinglePlatformParallel('infer with bad fields test', inferFailureTest, 'infer-bad-fields')
  99. util.testSinglePlatformParallel('infer with malformed JSON test', inferFailureTest, 'infer-malformed-json')
  100. util.testSinglePlatformParallel('infer using a non-specific `electron-prebuilt-compile` package version', inferFailureTest, 'infer-non-specific-electron-prebuilt-compile')