modules-spec.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const assert = require('assert')
  2. const Module = require('module')
  3. const path = require('path')
  4. const fs = require('fs')
  5. const { remote } = require('electron')
  6. const { BrowserWindow } = remote
  7. const { closeWindow } = require('./window-helpers')
  8. const features = process.atomBinding('features')
  9. const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
  10. describe('modules support', () => {
  11. const fixtures = path.join(__dirname, 'fixtures')
  12. describe('third-party module', () => {
  13. (nativeModulesEnabled ? describe : describe.skip)('runas', () => {
  14. it('can be required in renderer', () => {
  15. require('runas')
  16. })
  17. it('can be required in node binary', function (done) {
  18. if (!features.isRunAsNodeEnabled()) {
  19. this.skip()
  20. done()
  21. }
  22. const runas = path.join(fixtures, 'module', 'runas.js')
  23. const child = require('child_process').fork(runas)
  24. child.on('message', (msg) => {
  25. assert.strictEqual(msg, 'ok')
  26. done()
  27. })
  28. })
  29. if (process.platform === 'win32') {
  30. it('can be required if electron.exe is renamed', () => {
  31. const { execPath } = remote.process
  32. const testExecPath = path.join(path.dirname(execPath), 'test.exe')
  33. fs.copyFileSync(execPath, testExecPath)
  34. try {
  35. const runasFixture = path.join(fixtures, 'module', 'runas-renamed.js')
  36. assert.ok(fs.existsSync(runasFixture))
  37. const child = require('child_process').spawnSync(testExecPath, [runasFixture])
  38. assert.strictEqual(child.status, 0)
  39. } finally {
  40. fs.unlinkSync(testExecPath)
  41. }
  42. })
  43. }
  44. })
  45. // TODO(alexeykuzmin): Disabled during the Chromium 62 (Node.js 9) upgrade.
  46. // Enable it back when "ffi" module supports Node.js 9.
  47. // https://github.com/electron/electron/issues/11274
  48. xdescribe('ffi', () => {
  49. before(function () {
  50. if (!nativeModulesEnabled || process.platform === 'win32' ||
  51. process.arch === 'arm64') {
  52. this.skip()
  53. }
  54. })
  55. it('does not crash', () => {
  56. const ffi = require('ffi')
  57. const libm = ffi.Library('libm', {
  58. ceil: ['double', ['double']]
  59. })
  60. assert.strictEqual(libm.ceil(1.5), 2)
  61. })
  62. })
  63. describe('q', () => {
  64. const Q = require('q')
  65. describe('Q.when', () => {
  66. it('emits the fullfil callback', (done) => {
  67. Q(true).then((val) => {
  68. assert.strictEqual(val, true)
  69. done()
  70. })
  71. })
  72. })
  73. })
  74. describe('coffee-script', () => {
  75. it('can be registered and used to require .coffee files', () => {
  76. assert.doesNotThrow(() => {
  77. require('coffee-script').register()
  78. })
  79. assert.strictEqual(require('./fixtures/module/test.coffee'), true)
  80. })
  81. })
  82. })
  83. describe('global variables', () => {
  84. describe('process', () => {
  85. it('can be declared in a module', () => {
  86. assert.strictEqual(require('./fixtures/module/declare-process'), 'declared process')
  87. })
  88. })
  89. describe('global', () => {
  90. it('can be declared in a module', () => {
  91. assert.strictEqual(require('./fixtures/module/declare-global'), 'declared global')
  92. })
  93. })
  94. describe('Buffer', () => {
  95. it('can be declared in a module', () => {
  96. assert.strictEqual(require('./fixtures/module/declare-buffer'), 'declared Buffer')
  97. })
  98. })
  99. })
  100. describe('Module._nodeModulePaths', () => {
  101. describe('when the path is inside the resources path', () => {
  102. it('does not include paths outside of the resources path', () => {
  103. let modulePath = process.resourcesPath
  104. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  105. path.join(process.resourcesPath, 'node_modules')
  106. ])
  107. modulePath = process.resourcesPath + '-foo'
  108. const nodeModulePaths = Module._nodeModulePaths(modulePath)
  109. assert(nodeModulePaths.includes(path.join(modulePath, 'node_modules')))
  110. assert(nodeModulePaths.includes(path.join(modulePath, '..', 'node_modules')))
  111. modulePath = path.join(process.resourcesPath, 'foo')
  112. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  113. path.join(process.resourcesPath, 'foo', 'node_modules'),
  114. path.join(process.resourcesPath, 'node_modules')
  115. ])
  116. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo')
  117. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  118. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  119. path.join(process.resourcesPath, 'node_modules')
  120. ])
  121. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar')
  122. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  123. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  124. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  125. path.join(process.resourcesPath, 'node_modules')
  126. ])
  127. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar')
  128. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  129. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  130. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  131. path.join(process.resourcesPath, 'node_modules')
  132. ])
  133. })
  134. })
  135. describe('when the path is outside the resources path', () => {
  136. it('includes paths outside of the resources path', () => {
  137. const modulePath = path.resolve('/foo')
  138. assert.deepStrictEqual(Module._nodeModulePaths(modulePath), [
  139. path.join(modulePath, 'node_modules'),
  140. path.resolve('/node_modules')
  141. ])
  142. })
  143. })
  144. })
  145. describe('require', () => {
  146. describe('when loaded URL is not file: protocol', () => {
  147. let w
  148. beforeEach(() => {
  149. w = new BrowserWindow({ show: false })
  150. })
  151. afterEach(async () => {
  152. await closeWindow(w)
  153. w = null
  154. })
  155. it('searches for module under app directory', async () => {
  156. w.loadURL('about:blank')
  157. const result = await w.webContents.executeJavaScript('typeof require("q").when')
  158. assert.strictEqual(result, 'function')
  159. })
  160. })
  161. })
  162. })