forked from adamlaska/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-spec.js
More file actions
324 lines (285 loc) · 10.1 KB
/
node-spec.js
File metadata and controls
324 lines (285 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const assert = require('assert')
const ChildProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
const {ipcRenderer, remote} = require('electron')
const isCI = remote.getGlobal('isCi')
describe('node feature', function () {
var fixtures = path.join(__dirname, 'fixtures')
describe('child_process', function () {
describe('child_process.fork', function () {
it('works in current process', function (done) {
var child = ChildProcess.fork(path.join(fixtures, 'module', 'ping.js'))
child.on('message', function (msg) {
assert.equal(msg, 'message')
done()
})
child.send('message')
})
it('preserves args', function (done) {
var args = ['--expose_gc', '-test', '1']
var child = ChildProcess.fork(path.join(fixtures, 'module', 'process_args.js'), args)
child.on('message', function (msg) {
assert.deepEqual(args, msg.slice(2))
done()
})
child.send('message')
})
it('works in forked process', function (done) {
var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'))
child.on('message', function (msg) {
assert.equal(msg, 'message')
done()
})
child.send('message')
})
it('works in forked process when options.env is specifed', function (done) {
var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'), [], {
path: process.env['PATH']
})
child.on('message', function (msg) {
assert.equal(msg, 'message')
done()
})
child.send('message')
})
it('works in browser process', function (done) {
var fork = remote.require('child_process').fork
var child = fork(path.join(fixtures, 'module', 'ping.js'))
child.on('message', function (msg) {
assert.equal(msg, 'message')
done()
})
child.send('message')
})
it('has String::localeCompare working in script', function (done) {
var child = ChildProcess.fork(path.join(fixtures, 'module', 'locale-compare.js'))
child.on('message', function (msg) {
assert.deepEqual(msg, [0, -1, 1])
done()
})
child.send('message')
})
it('has setImmediate working in script', function (done) {
var child = ChildProcess.fork(path.join(fixtures, 'module', 'set-immediate.js'))
child.on('message', function (msg) {
assert.equal(msg, 'ok')
done()
})
child.send('message')
})
it('pipes stdio', function (done) {
let child = ChildProcess.fork(path.join(fixtures, 'module', 'process-stdout.js'), {silent: true})
let data = ''
child.stdout.on('data', (chunk) => {
data += String(chunk)
})
child.on('close', (code) => {
assert.equal(code, 0)
assert.equal(data, 'pipes stdio')
done()
})
})
it('works when sending a message to a process forked with the --eval argument', function (done) {
const source = "process.on('message', (message) => { process.send(message) })"
const forked = ChildProcess.fork('--eval', [source])
forked.once('message', (message) => {
assert.equal(message, 'hello')
done()
})
forked.send('hello')
})
})
describe('child_process.spawn', function () {
it('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', function (done) {
const child = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
env: {
ELECTRON_RUN_AS_NODE: true
}
})
let output = ''
child.stdout.on('data', function (data) {
output += data
})
child.stdout.on('close', function () {
assert.deepEqual(JSON.parse(output), {
processLog: process.platform === 'win32' ? 'function' : 'undefined',
processType: 'undefined',
window: 'undefined'
})
done()
})
})
})
})
describe('contexts', function () {
describe('setTimeout in fs callback', function () {
if (process.env.TRAVIS === 'true') {
return
}
it('does not crash', function (done) {
fs.readFile(__filename, function () {
setTimeout(done, 0)
})
})
})
describe('error thrown in renderer process node context', function () {
it('gets emitted as a process uncaughtException event', function (done) {
const error = new Error('boo!')
const listeners = process.listeners('uncaughtException')
process.removeAllListeners('uncaughtException')
process.on('uncaughtException', (thrown) => {
assert.strictEqual(thrown, error)
process.removeAllListeners('uncaughtException')
listeners.forEach((listener) => {
process.on('uncaughtException', listener)
})
done()
})
fs.readFile(__filename, () => {
throw error
})
})
})
describe('error thrown in main process node context', function () {
it('gets emitted as a process uncaughtException event', function () {
const error = ipcRenderer.sendSync('handle-uncaught-exception', 'hello')
assert.equal(error, 'hello')
})
})
describe('promise rejection in main process node context', function () {
it('gets emitted as a process unhandledRejection event', function () {
const error = ipcRenderer.sendSync('handle-unhandled-rejection', 'hello')
assert.equal(error, 'hello')
})
})
describe('setTimeout called under Chromium event loop in browser process', function () {
it('can be scheduled in time', function (done) {
remote.getGlobal('setTimeout')(done, 0)
})
})
describe('setInterval called under Chromium event loop in browser process', function () {
it('can be scheduled in time', function (done) {
var clear, interval
clear = function () {
remote.getGlobal('clearInterval')(interval)
done()
}
interval = remote.getGlobal('setInterval')(clear, 10)
})
})
})
describe('message loop', function () {
describe('process.nextTick', function () {
it('emits the callback', function (done) {
process.nextTick(done)
})
it('works in nested calls', function (done) {
process.nextTick(function () {
process.nextTick(function () {
process.nextTick(done)
})
})
})
})
describe('setImmediate', function () {
it('emits the callback', function (done) {
setImmediate(done)
})
it('works in nested calls', function (done) {
setImmediate(function () {
setImmediate(function () {
setImmediate(done)
})
})
})
})
})
describe('net.connect', function () {
if (process.platform !== 'darwin') {
return
}
it('emit error when connect to a socket path without listeners', function (done) {
var socketPath = path.join(os.tmpdir(), 'atom-shell-test.sock')
var script = path.join(fixtures, 'module', 'create_socket.js')
var child = ChildProcess.fork(script, [socketPath])
child.on('exit', function (code) {
assert.equal(code, 0)
var client = require('net').connect(socketPath)
client.on('error', function (error) {
assert.equal(error.code, 'ECONNREFUSED')
done()
})
})
})
})
describe('Buffer', function () {
it('can be created from WebKit external string', function () {
var p = document.createElement('p')
p.innerText = '闲云潭影日悠悠,物换星移几度秋'
var b = new Buffer(p.innerText)
assert.equal(b.toString(), '闲云潭影日悠悠,物换星移几度秋')
assert.equal(Buffer.byteLength(p.innerText), 45)
})
it('correctly parses external one-byte UTF8 string', function () {
var p = document.createElement('p')
p.innerText = 'Jøhänñéß'
var b = new Buffer(p.innerText)
assert.equal(b.toString(), 'Jøhänñéß')
assert.equal(Buffer.byteLength(p.innerText), 13)
})
it('does not crash when creating large Buffers', function () {
var buffer = new Buffer(new Array(4096).join(' '))
assert.equal(buffer.length, 4095)
buffer = new Buffer(new Array(4097).join(' '))
assert.equal(buffer.length, 4096)
})
})
describe('process.stdout', function () {
it('does not throw an exception when accessed', function () {
assert.doesNotThrow(function () {
process.stdout
})
})
it('does not throw an exception when calling write()', function () {
assert.doesNotThrow(function () {
process.stdout.write('test')
})
})
it('should have isTTY defined on Mac and Linux', function () {
if (isCI) return
if (process.platform === 'win32') {
assert.equal(process.stdout.isTTY, undefined)
} else {
assert.equal(typeof process.stdout.isTTY, 'boolean')
}
})
})
describe('process.stdin', function () {
it('does not throw an exception when accessed', function () {
assert.doesNotThrow(function () {
process.stdin
})
})
it('returns null when read from', function () {
assert.equal(process.stdin.read(), null)
})
})
describe('process.version', function () {
it('should not have -pre', function () {
assert(!process.version.endsWith('-pre'))
})
})
describe('vm.createContext', function () {
it('should not crash', function () {
require('vm').runInNewContext('')
})
})
it('includes the electron version in process.versions', () => {
assert(/^\d+\.\d+\.\d+$/.test(process.versions.electron))
})
it('includes the chrome version in process.versions', () => {
assert(/^\d+\.\d+\.\d+\.\d+$/.test(process.versions.chrome))
})
})