modules-spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const assert = require('assert')
  2. const Module = require('module')
  3. const path = require('path')
  4. const {remote} = require('electron')
  5. const {BrowserWindow} = remote
  6. const {closeWindow} = require('./window-helpers')
  7. const nativeModulesEnabled = remote.getGlobal('nativeModulesEnabled')
  8. describe('modules support', function () {
  9. var fixtures = path.join(__dirname, 'fixtures')
  10. describe('third-party module', function () {
  11. describe('runas', function () {
  12. if (!nativeModulesEnabled) return
  13. it('can be required in renderer', function () {
  14. require('runas')
  15. })
  16. it('can be required in node binary', function (done) {
  17. var runas = path.join(fixtures, 'module', 'runas.js')
  18. var child = require('child_process').fork(runas)
  19. child.on('message', function (msg) {
  20. assert.equal(msg, 'ok')
  21. done()
  22. })
  23. })
  24. })
  25. describe('ffi', function () {
  26. if (!nativeModulesEnabled) return
  27. if (process.platform === 'win32') return
  28. it('does not crash', function () {
  29. var ffi = require('ffi')
  30. var libm = ffi.Library('libm', {
  31. ceil: ['double', ['double']]
  32. })
  33. assert.equal(libm.ceil(1.5), 2)
  34. })
  35. })
  36. describe('q', function () {
  37. var Q = require('q')
  38. describe('Q.when', function () {
  39. it('emits the fullfil callback', function (done) {
  40. Q(true).then(function (val) {
  41. assert.equal(val, true)
  42. done()
  43. })
  44. })
  45. })
  46. })
  47. describe('coffee-script', function () {
  48. it('can be registered and used to require .coffee files', function () {
  49. assert.doesNotThrow(function () {
  50. require('coffee-script').register()
  51. })
  52. assert.strictEqual(require('./fixtures/module/test.coffee'), true)
  53. })
  54. })
  55. })
  56. describe('global variables', function () {
  57. describe('process', function () {
  58. it('can be declared in a module', function () {
  59. assert.strictEqual(require('./fixtures/module/declare-process'), 'declared process')
  60. })
  61. })
  62. describe('global', function () {
  63. it('can be declared in a module', function () {
  64. assert.strictEqual(require('./fixtures/module/declare-global'), 'declared global')
  65. })
  66. })
  67. describe('Buffer', function () {
  68. it('can be declared in a module', function () {
  69. assert.strictEqual(require('./fixtures/module/declare-buffer'), 'declared Buffer')
  70. })
  71. })
  72. })
  73. describe('Module._nodeModulePaths', function () {
  74. describe('when the path is inside the resources path', function () {
  75. it('does not include paths outside of the resources path', function () {
  76. let modulePath = process.resourcesPath
  77. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  78. path.join(process.resourcesPath, 'node_modules')
  79. ])
  80. modulePath = process.resourcesPath + '-foo'
  81. let nodeModulePaths = Module._nodeModulePaths(modulePath)
  82. assert(nodeModulePaths.includes(path.join(modulePath, 'node_modules')))
  83. assert(nodeModulePaths.includes(path.join(modulePath, '..', 'node_modules')))
  84. modulePath = path.join(process.resourcesPath, 'foo')
  85. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  86. path.join(process.resourcesPath, 'foo', 'node_modules'),
  87. path.join(process.resourcesPath, 'node_modules')
  88. ])
  89. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo')
  90. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  91. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  92. path.join(process.resourcesPath, 'node_modules')
  93. ])
  94. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'bar')
  95. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  96. path.join(process.resourcesPath, 'node_modules', 'foo', 'bar', 'node_modules'),
  97. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  98. path.join(process.resourcesPath, 'node_modules')
  99. ])
  100. modulePath = path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar')
  101. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  102. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules', 'bar', 'node_modules'),
  103. path.join(process.resourcesPath, 'node_modules', 'foo', 'node_modules'),
  104. path.join(process.resourcesPath, 'node_modules')
  105. ])
  106. })
  107. })
  108. describe('when the path is outside the resources path', function () {
  109. it('includes paths outside of the resources path', function () {
  110. let modulePath = path.resolve('/foo')
  111. assert.deepEqual(Module._nodeModulePaths(modulePath), [
  112. path.join(modulePath, 'node_modules'),
  113. path.resolve('/node_modules')
  114. ])
  115. })
  116. })
  117. })
  118. describe('require', () => {
  119. describe('when loaded URL is not file: protocol', () => {
  120. let w
  121. beforeEach(() => {
  122. w = new BrowserWindow({
  123. show: false
  124. })
  125. })
  126. afterEach(async () => {
  127. await closeWindow(w)
  128. w = null
  129. })
  130. it('searches for module under app directory', async () => {
  131. w.loadURL('about:blank')
  132. const result = await w.webContents.executeJavaScript('typeof require("q").when')
  133. assert.equal(result, 'function')
  134. })
  135. })
  136. })
  137. })