node-spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const assert = require('assert')
  2. const ChildProcess = require('child_process')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const os = require('os')
  6. const {remote} = require('electron')
  7. const isCI = remote.getGlobal('isCi')
  8. describe('node feature', function () {
  9. var fixtures = path.join(__dirname, 'fixtures')
  10. describe('child_process', function () {
  11. describe('child_process.fork', function () {
  12. it('works in current process', function (done) {
  13. var child = ChildProcess.fork(path.join(fixtures, 'module', 'ping.js'))
  14. child.on('message', function (msg) {
  15. assert.equal(msg, 'message')
  16. done()
  17. })
  18. child.send('message')
  19. })
  20. it('preserves args', function (done) {
  21. var args = ['--expose_gc', '-test', '1']
  22. var child = ChildProcess.fork(path.join(fixtures, 'module', 'process_args.js'), args)
  23. child.on('message', function (msg) {
  24. assert.deepEqual(args, msg.slice(2))
  25. done()
  26. })
  27. child.send('message')
  28. })
  29. it('works in forked process', function (done) {
  30. var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'))
  31. child.on('message', function (msg) {
  32. assert.equal(msg, 'message')
  33. done()
  34. })
  35. child.send('message')
  36. })
  37. it('works in forked process when options.env is specifed', function (done) {
  38. var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'), [], {
  39. path: process.env['PATH']
  40. })
  41. child.on('message', function (msg) {
  42. assert.equal(msg, 'message')
  43. done()
  44. })
  45. child.send('message')
  46. })
  47. it('works in browser process', function (done) {
  48. var fork = remote.require('child_process').fork
  49. var child = fork(path.join(fixtures, 'module', 'ping.js'))
  50. child.on('message', function (msg) {
  51. assert.equal(msg, 'message')
  52. done()
  53. })
  54. child.send('message')
  55. })
  56. it('has String::localeCompare working in script', function (done) {
  57. var child = ChildProcess.fork(path.join(fixtures, 'module', 'locale-compare.js'))
  58. child.on('message', function (msg) {
  59. assert.deepEqual(msg, [0, -1, 1])
  60. done()
  61. })
  62. child.send('message')
  63. })
  64. it('has setImmediate working in script', function (done) {
  65. var child = ChildProcess.fork(path.join(fixtures, 'module', 'set-immediate.js'))
  66. child.on('message', function (msg) {
  67. assert.equal(msg, 'ok')
  68. done()
  69. })
  70. child.send('message')
  71. })
  72. it('pipes stdio', function (done) {
  73. let child = ChildProcess.fork(path.join(fixtures, 'module', 'process-stdout.js'), {silent: true})
  74. let data = ''
  75. child.stdout.on('data', (chunk) => {
  76. data += String(chunk)
  77. })
  78. child.on('exit', (code) => {
  79. assert.equal(code, 0)
  80. assert.equal(data, 'pipes stdio')
  81. done()
  82. })
  83. })
  84. })
  85. })
  86. describe('contexts', function () {
  87. describe('setTimeout in fs callback', function () {
  88. if (process.env.TRAVIS === 'true') {
  89. return
  90. }
  91. it('does not crash', function (done) {
  92. fs.readFile(__filename, function () {
  93. setTimeout(done, 0)
  94. })
  95. })
  96. })
  97. describe('throw error in node context', function () {
  98. it('gets caught', function (done) {
  99. var error = new Error('boo!')
  100. var lsts = process.listeners('uncaughtException')
  101. process.removeAllListeners('uncaughtException')
  102. process.on('uncaughtException', function () {
  103. var i, len, lst
  104. process.removeAllListeners('uncaughtException')
  105. for (i = 0, len = lsts.length; i < len; i++) {
  106. lst = lsts[i]
  107. process.on('uncaughtException', lst)
  108. }
  109. done()
  110. })
  111. fs.readFile(__filename, function () {
  112. throw error
  113. })
  114. })
  115. })
  116. describe('setTimeout called under Chromium event loop in browser process', function () {
  117. it('can be scheduled in time', function (done) {
  118. remote.getGlobal('setTimeout')(done, 0)
  119. })
  120. })
  121. describe('setInterval called under Chromium event loop in browser process', function () {
  122. it('can be scheduled in time', function (done) {
  123. var clear, interval
  124. clear = function () {
  125. remote.getGlobal('clearInterval')(interval)
  126. done()
  127. }
  128. interval = remote.getGlobal('setInterval')(clear, 10)
  129. })
  130. })
  131. })
  132. describe('message loop', function () {
  133. describe('process.nextTick', function () {
  134. it('emits the callback', function (done) {
  135. process.nextTick(done)
  136. })
  137. it('works in nested calls', function (done) {
  138. process.nextTick(function () {
  139. process.nextTick(function () {
  140. process.nextTick(done)
  141. })
  142. })
  143. })
  144. })
  145. describe('setImmediate', function () {
  146. it('emits the callback', function (done) {
  147. setImmediate(done)
  148. })
  149. it('works in nested calls', function (done) {
  150. setImmediate(function () {
  151. setImmediate(function () {
  152. setImmediate(done)
  153. })
  154. })
  155. })
  156. })
  157. })
  158. describe('net.connect', function () {
  159. if (process.platform !== 'darwin') {
  160. return
  161. }
  162. it('emit error when connect to a socket path without listeners', function (done) {
  163. var socketPath = path.join(os.tmpdir(), 'atom-shell-test.sock')
  164. var script = path.join(fixtures, 'module', 'create_socket.js')
  165. var child = ChildProcess.fork(script, [socketPath])
  166. child.on('exit', function (code) {
  167. assert.equal(code, 0)
  168. var client = require('net').connect(socketPath)
  169. client.on('error', function (error) {
  170. assert.equal(error.code, 'ECONNREFUSED')
  171. done()
  172. })
  173. })
  174. })
  175. })
  176. describe('Buffer', function () {
  177. it('can be created from WebKit external string', function () {
  178. var p = document.createElement('p')
  179. p.innerText = '闲云潭影日悠悠,物换星移几度秋'
  180. var b = new Buffer(p.innerText)
  181. assert.equal(b.toString(), '闲云潭影日悠悠,物换星移几度秋')
  182. assert.equal(Buffer.byteLength(p.innerText), 45)
  183. })
  184. it('correctly parses external one-byte UTF8 string', function () {
  185. var p = document.createElement('p')
  186. p.innerText = 'Jøhänñéß'
  187. var b = new Buffer(p.innerText)
  188. assert.equal(b.toString(), 'Jøhänñéß')
  189. assert.equal(Buffer.byteLength(p.innerText), 13)
  190. })
  191. it('does not crash when creating large Buffers', function () {
  192. var buffer = new Buffer(new Array(4096).join(' '))
  193. assert.equal(buffer.length, 4095)
  194. buffer = new Buffer(new Array(4097).join(' '))
  195. assert.equal(buffer.length, 4096)
  196. })
  197. })
  198. describe('process.stdout', function () {
  199. it('does not throw an exception when accessed', function () {
  200. assert.doesNotThrow(function () {
  201. process.stdout
  202. })
  203. })
  204. it('does not throw an exception when calling write()', function () {
  205. assert.doesNotThrow(function () {
  206. process.stdout.write('test')
  207. })
  208. })
  209. it('should have isTTY defined on Mac and Linux', function () {
  210. if (isCI) return
  211. if (process.platform === 'win32') {
  212. assert.equal(process.stdout.isTTY, undefined)
  213. } else {
  214. assert.equal(typeof process.stdout.isTTY, 'boolean')
  215. }
  216. })
  217. })
  218. describe('process.stdin', function () {
  219. it('does not throw an exception when accessed', function () {
  220. assert.doesNotThrow(function () {
  221. process.stdin
  222. })
  223. })
  224. it('returns null when read from', function () {
  225. assert.equal(process.stdin.read(), null)
  226. })
  227. })
  228. describe('process.version', function () {
  229. it('should not have -pre', function () {
  230. assert(!process.version.endsWith('-pre'))
  231. })
  232. })
  233. describe('vm.createContext', function () {
  234. it('should not crash', function () {
  235. require('vm').runInNewContext('')
  236. })
  237. })
  238. })