node-spec.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 {ipcRenderer, 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('close', (code) => {
  79. assert.equal(code, 0)
  80. assert.equal(data, 'pipes stdio')
  81. done()
  82. })
  83. })
  84. it('works when sending a message to a process forked with the --eval argument', function (done) {
  85. const source = "process.on('message', (message) => { process.send(message) })"
  86. const forked = ChildProcess.fork('--eval', [source])
  87. forked.once('message', (message) => {
  88. assert.equal(message, 'hello')
  89. done()
  90. })
  91. forked.send('hello')
  92. })
  93. })
  94. describe('child_process.spawn', function () {
  95. it('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', function (done) {
  96. const child = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
  97. env: {
  98. ELECTRON_RUN_AS_NODE: true
  99. }
  100. })
  101. let output = ''
  102. child.stdout.on('data', function (data) {
  103. output += data
  104. })
  105. child.stdout.on('close', function () {
  106. assert.deepEqual(JSON.parse(output), {
  107. processLog: process.platform === 'win32' ? 'function' : 'undefined',
  108. processType: 'undefined',
  109. window: 'undefined'
  110. })
  111. done()
  112. })
  113. })
  114. })
  115. })
  116. describe('contexts', function () {
  117. describe('setTimeout in fs callback', function () {
  118. if (process.env.TRAVIS === 'true') {
  119. return
  120. }
  121. it('does not crash', function (done) {
  122. fs.readFile(__filename, function () {
  123. setTimeout(done, 0)
  124. })
  125. })
  126. })
  127. describe('error thrown in renderer process node context', function () {
  128. it('gets emitted as a process uncaughtException event', function (done) {
  129. const error = new Error('boo!')
  130. const listeners = process.listeners('uncaughtException')
  131. process.removeAllListeners('uncaughtException')
  132. process.on('uncaughtException', (thrown) => {
  133. assert.strictEqual(thrown, error)
  134. process.removeAllListeners('uncaughtException')
  135. listeners.forEach((listener) => {
  136. process.on('uncaughtException', listener)
  137. })
  138. done()
  139. })
  140. fs.readFile(__filename, () => {
  141. throw error
  142. })
  143. })
  144. })
  145. describe('error thrown in main process node context', function () {
  146. it('gets emitted as a process uncaughtException event', function () {
  147. const error = ipcRenderer.sendSync('handle-uncaught-exception', 'hello')
  148. assert.equal(error, 'hello')
  149. })
  150. })
  151. describe('promise rejection in main process node context', function () {
  152. it('gets emitted as a process unhandledRejection event', function () {
  153. const error = ipcRenderer.sendSync('handle-unhandled-rejection', 'hello')
  154. assert.equal(error, 'hello')
  155. })
  156. })
  157. describe('setTimeout called under Chromium event loop in browser process', function () {
  158. it('can be scheduled in time', function (done) {
  159. remote.getGlobal('setTimeout')(done, 0)
  160. })
  161. })
  162. describe('setInterval called under Chromium event loop in browser process', function () {
  163. it('can be scheduled in time', function (done) {
  164. var clear, interval
  165. clear = function () {
  166. remote.getGlobal('clearInterval')(interval)
  167. done()
  168. }
  169. interval = remote.getGlobal('setInterval')(clear, 10)
  170. })
  171. })
  172. })
  173. describe('message loop', function () {
  174. describe('process.nextTick', function () {
  175. it('emits the callback', function (done) {
  176. process.nextTick(done)
  177. })
  178. it('works in nested calls', function (done) {
  179. process.nextTick(function () {
  180. process.nextTick(function () {
  181. process.nextTick(done)
  182. })
  183. })
  184. })
  185. })
  186. describe('setImmediate', function () {
  187. it('emits the callback', function (done) {
  188. setImmediate(done)
  189. })
  190. it('works in nested calls', function (done) {
  191. setImmediate(function () {
  192. setImmediate(function () {
  193. setImmediate(done)
  194. })
  195. })
  196. })
  197. })
  198. })
  199. describe('net.connect', function () {
  200. if (process.platform !== 'darwin') {
  201. return
  202. }
  203. it('emit error when connect to a socket path without listeners', function (done) {
  204. var socketPath = path.join(os.tmpdir(), 'atom-shell-test.sock')
  205. var script = path.join(fixtures, 'module', 'create_socket.js')
  206. var child = ChildProcess.fork(script, [socketPath])
  207. child.on('exit', function (code) {
  208. assert.equal(code, 0)
  209. var client = require('net').connect(socketPath)
  210. client.on('error', function (error) {
  211. assert.equal(error.code, 'ECONNREFUSED')
  212. done()
  213. })
  214. })
  215. })
  216. })
  217. describe('Buffer', function () {
  218. it('can be created from WebKit external string', function () {
  219. var p = document.createElement('p')
  220. p.innerText = '闲云潭影日悠悠,物换星移几度秋'
  221. var b = new Buffer(p.innerText)
  222. assert.equal(b.toString(), '闲云潭影日悠悠,物换星移几度秋')
  223. assert.equal(Buffer.byteLength(p.innerText), 45)
  224. })
  225. it('correctly parses external one-byte UTF8 string', function () {
  226. var p = document.createElement('p')
  227. p.innerText = 'Jøhänñéß'
  228. var b = new Buffer(p.innerText)
  229. assert.equal(b.toString(), 'Jøhänñéß')
  230. assert.equal(Buffer.byteLength(p.innerText), 13)
  231. })
  232. it('does not crash when creating large Buffers', function () {
  233. var buffer = new Buffer(new Array(4096).join(' '))
  234. assert.equal(buffer.length, 4095)
  235. buffer = new Buffer(new Array(4097).join(' '))
  236. assert.equal(buffer.length, 4096)
  237. })
  238. })
  239. describe('process.stdout', function () {
  240. it('does not throw an exception when accessed', function () {
  241. assert.doesNotThrow(function () {
  242. process.stdout
  243. })
  244. })
  245. it('does not throw an exception when calling write()', function () {
  246. assert.doesNotThrow(function () {
  247. process.stdout.write('test')
  248. })
  249. })
  250. it('should have isTTY defined on Mac and Linux', function () {
  251. if (isCI) return
  252. if (process.platform === 'win32') {
  253. assert.equal(process.stdout.isTTY, undefined)
  254. } else {
  255. assert.equal(typeof process.stdout.isTTY, 'boolean')
  256. }
  257. })
  258. })
  259. describe('process.stdin', function () {
  260. it('does not throw an exception when accessed', function () {
  261. assert.doesNotThrow(function () {
  262. process.stdin
  263. })
  264. })
  265. it('returns null when read from', function () {
  266. assert.equal(process.stdin.read(), null)
  267. })
  268. })
  269. describe('process.version', function () {
  270. it('should not have -pre', function () {
  271. assert(!process.version.endsWith('-pre'))
  272. })
  273. })
  274. describe('vm.createContext', function () {
  275. it('should not crash', function () {
  276. require('vm').runInNewContext('')
  277. })
  278. })
  279. it('includes the electron version in process.versions', () => {
  280. assert(/^\d+\.\d+\.\d+$/.test(process.versions.electron))
  281. })
  282. it('includes the chrome version in process.versions', () => {
  283. assert(/^\d+\.\d+\.\d+\.\d+$/.test(process.versions.chrome))
  284. })
  285. })