win32.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. 'use strict'
  2. const config = require('./config.json')
  3. const fs = require('fs-extra')
  4. const packager = require('..')
  5. const path = require('path')
  6. const test = require('ava')
  7. const util = require('./_util')
  8. const win32 = require('../win32')
  9. const win32Opts = {
  10. name: 'basicTest',
  11. dir: util.fixtureSubdir('basic'),
  12. electronVersion: config.version,
  13. arch: 'x64',
  14. platform: 'win32'
  15. }
  16. function generateRceditOptionsSansIcon (opts) {
  17. return new win32.App(opts).generateRceditOptionsSansIcon()
  18. }
  19. function generateVersionStringTest (metadataProperties, extraOpts, expectedValues, assertionMsgs) {
  20. return t => {
  21. const opts = Object.assign({}, win32Opts, extraOpts)
  22. const rcOpts = generateRceditOptionsSansIcon(opts)
  23. metadataProperties = [].concat(metadataProperties)
  24. expectedValues = [].concat(expectedValues)
  25. assertionMsgs = [].concat(assertionMsgs)
  26. metadataProperties.forEach((property, i) => {
  27. const value = expectedValues[i]
  28. const msg = assertionMsgs[i]
  29. if (property === 'version-string') {
  30. for (const subkey in value) {
  31. t.is(rcOpts[property][subkey], value[subkey], `${msg} (${subkey})`)
  32. }
  33. } else {
  34. t.is(rcOpts[property], value, msg)
  35. }
  36. })
  37. }
  38. }
  39. function setFileVersionTest (buildVersion) {
  40. const appVersion = '4.99.101.0'
  41. const opts = {
  42. appVersion: appVersion,
  43. buildVersion: buildVersion
  44. }
  45. return generateVersionStringTest(
  46. ['product-version', 'file-version'],
  47. opts,
  48. [appVersion, buildVersion],
  49. ['Product version should match app version',
  50. 'File version should match build version']
  51. )
  52. }
  53. function setProductVersionTest (appVersion) {
  54. return generateVersionStringTest(
  55. ['product-version', 'file-version'],
  56. { appVersion: appVersion },
  57. [appVersion, appVersion],
  58. ['Product version should match app version',
  59. 'File version should match app version']
  60. )
  61. }
  62. function setCopyrightTest (appCopyright) {
  63. const opts = {
  64. appCopyright: appCopyright
  65. }
  66. return generateVersionStringTest('version-string', opts, {LegalCopyright: appCopyright}, 'Legal copyright should match app copyright')
  67. }
  68. function setCopyrightAndCompanyNameTest (appCopyright, companyName) {
  69. const opts = {
  70. appCopyright: appCopyright,
  71. win32metadata: {
  72. CompanyName: companyName
  73. }
  74. }
  75. return generateVersionStringTest(
  76. 'version-string',
  77. opts,
  78. {LegalCopyright: appCopyright, CompanyName: companyName},
  79. 'Legal copyright should match app copyright and Company name should match win32metadata value'
  80. )
  81. }
  82. function setRequestedExecutionLevelTest (requestedExecutionLevel) {
  83. const opts = {
  84. win32metadata: {
  85. 'requested-execution-level': requestedExecutionLevel
  86. }
  87. }
  88. return generateVersionStringTest(
  89. 'requested-execution-level',
  90. opts,
  91. requestedExecutionLevel,
  92. 'requested-execution-level in win32metadata should match rcOpts value'
  93. )
  94. }
  95. function setApplicationManifestTest (applicationManifest) {
  96. const opts = {
  97. win32metadata: {
  98. 'application-manifest': applicationManifest
  99. }
  100. }
  101. return generateVersionStringTest(
  102. 'application-manifest',
  103. opts,
  104. applicationManifest,
  105. 'application-manifest in win32metadata should match rcOpts value'
  106. )
  107. }
  108. function setCompanyNameTest (companyName) {
  109. const opts = {
  110. win32metadata: {
  111. CompanyName: companyName
  112. }
  113. }
  114. return generateVersionStringTest('version-string',
  115. opts,
  116. {CompanyName: companyName},
  117. `Company name should match win32metadata value`)
  118. }
  119. test('better error message when wine is not found', (t) => {
  120. let err = Error('spawn wine ENOENT')
  121. err.code = 'ENOENT'
  122. err.syscall = 'spawn wine'
  123. t.is(err.message, 'spawn wine ENOENT')
  124. err = win32.updateWineMissingException(err)
  125. t.not(err.message, 'spawn wine ENOENT')
  126. })
  127. test('error message unchanged when error not about wine', t => {
  128. let errNotEnoent = Error('unchanged')
  129. errNotEnoent.code = 'ESOMETHINGELSE'
  130. errNotEnoent.syscall = 'spawn wine'
  131. t.is(errNotEnoent.message, 'unchanged')
  132. errNotEnoent = win32.updateWineMissingException(errNotEnoent)
  133. t.is(errNotEnoent.message, 'unchanged')
  134. let errNotSpawnWine = Error('unchanged')
  135. errNotSpawnWine.code = 'ENOENT'
  136. errNotSpawnWine.syscall = 'spawn foo'
  137. t.is(errNotSpawnWine.message, 'unchanged')
  138. errNotSpawnWine = win32.updateWineMissingException(errNotSpawnWine)
  139. t.is(errNotSpawnWine.message, 'unchanged')
  140. })
  141. test('win32metadata defaults', t => {
  142. const opts = { name: 'Win32 App' }
  143. const rcOpts = generateRceditOptionsSansIcon(opts)
  144. t.is(rcOpts['version-string'].FileDescription, opts.name, 'default FileDescription')
  145. t.is(rcOpts['version-string'].InternalName, opts.name, 'default InternalName')
  146. t.is(rcOpts['version-string'].OriginalFilename, 'Win32 App.exe', 'default OriginalFilename')
  147. t.is(rcOpts['version-string'].ProductName, opts.name, 'default ProductName')
  148. })
  149. util.packagerTest('win32 executable name is based on sanitized app name', (t, opts) => {
  150. Object.assign(opts, win32Opts, { name: '@username/package-name' })
  151. return packager(opts)
  152. .then(paths => {
  153. t.is(1, paths.length, '1 bundle created')
  154. const appExePath = path.join(paths[0], '@username-package-name.exe')
  155. return fs.pathExists(appExePath)
  156. }).then(exists => t.true(exists, 'The sanitized EXE filename should exist'))
  157. })
  158. util.packagerTest('win32 executable name uses executableName when available', (t, opts) => {
  159. Object.assign(opts, win32Opts, { name: 'PackageName', executableName: 'my-package' })
  160. return packager(opts)
  161. .then(paths => {
  162. t.is(1, paths.length, '1 bundle created')
  163. const appExePath = path.join(paths[0], 'my-package.exe')
  164. return fs.pathExists(appExePath)
  165. }).then(exists => t.true(exists, 'the executableName-based filename should exist'))
  166. })
  167. util.packagerTest('win32 icon set', (t, opts) => {
  168. Object.assign(opts, win32Opts, { executableName: 'iconTest', arch: 'ia32', icon: path.join(__dirname, 'fixtures', 'monochrome') })
  169. return packager(opts)
  170. .then(paths => {
  171. t.is(1, paths.length, '1 bundle created')
  172. const appExePath = path.join(paths[0], 'iconTest.exe')
  173. return fs.pathExists(appExePath)
  174. }).then(exists => t.true(exists, 'the Electron executable should exist'))
  175. })
  176. test('win32 build version sets FileVersion test', setFileVersionTest('2.3.4.5'))
  177. test('win32 app version sets ProductVersion test', setProductVersionTest('5.4.3.2'))
  178. test('win32 app copyright sets LegalCopyright test', setCopyrightTest('Copyright Bar'))
  179. test('win32 set LegalCopyright and CompanyName test', setCopyrightAndCompanyNameTest('Copyright Bar', 'MyCompany LLC'))
  180. test('win32 set CompanyName test', setCompanyNameTest('MyCompany LLC'))
  181. test('win32 set requested-execution-level test', setRequestedExecutionLevelTest('asInvoker'))
  182. test('win32 set application-manifest test', setApplicationManifestTest('/path/to/manifest.xml'))