modules-spec.js 5.4 KB

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