2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2016-06-29 16:37:10 +00:00
|
|
|
const ChildProcess = require('child_process')
|
2016-03-25 20:03:49 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const os = require('os')
|
2017-01-25 16:55:38 +00:00
|
|
|
const {ipcRenderer, remote} = require('electron')
|
2016-03-25 20:03:49 +00:00
|
|
|
|
2016-06-20 02:16:17 +00:00
|
|
|
const isCI = remote.getGlobal('isCi')
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
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) {
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'ping.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
child.on('message', function (msg) {
|
|
|
|
assert.equal(msg, 'message')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('preserves args', function (done) {
|
|
|
|
var args = ['--expose_gc', '-test', '1']
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'process_args.js'), args)
|
2016-03-25 20:03:49 +00:00
|
|
|
child.on('message', function (msg) {
|
|
|
|
assert.deepEqual(args, msg.slice(2))
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('works in forked process', function (done) {
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
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) {
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'fork_ping.js'), [], {
|
2016-01-12 02:40:23 +00:00
|
|
|
path: process.env['PATH']
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
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) {
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'locale-compare.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
child.on('message', function (msg) {
|
|
|
|
assert.deepEqual(msg, [0, -1, 1])
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('has setImmediate working in script', function (done) {
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(path.join(fixtures, 'module', 'set-immediate.js'))
|
2016-03-25 20:03:49 +00:00
|
|
|
child.on('message', function (msg) {
|
|
|
|
assert.equal(msg, 'ok')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
child.send('message')
|
|
|
|
})
|
2016-05-31 01:26:19 +00:00
|
|
|
|
|
|
|
it('pipes stdio', function (done) {
|
2016-06-29 16:37:10 +00:00
|
|
|
let child = ChildProcess.fork(path.join(fixtures, 'module', 'process-stdout.js'), {silent: true})
|
2016-05-31 01:26:19 +00:00
|
|
|
let data = ''
|
|
|
|
child.stdout.on('data', (chunk) => {
|
|
|
|
data += String(chunk)
|
|
|
|
})
|
2017-04-20 18:40:04 +00:00
|
|
|
child.on('close', (code) => {
|
2016-05-31 01:26:19 +00:00
|
|
|
assert.equal(code, 0)
|
|
|
|
assert.equal(data, 'pipes stdio')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-04-06 16:47:33 +00:00
|
|
|
|
2017-04-06 16:52:52 +00:00
|
|
|
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) })"
|
2017-04-06 16:47:33 +00:00
|
|
|
const forked = ChildProcess.fork('--eval', [source])
|
2017-04-06 16:52:52 +00:00
|
|
|
forked.once('message', (message) => {
|
2017-04-06 16:47:33 +00:00
|
|
|
assert.equal(message, 'hello')
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
forked.send('hello')
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-10-12 20:01:43 +00:00
|
|
|
|
|
|
|
describe('child_process.spawn', function () {
|
2017-06-12 21:04:57 +00:00
|
|
|
let child
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
if (child != null) {
|
|
|
|
child.kill()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-10-12 20:01:43 +00:00
|
|
|
it('supports spawning Electron as a node process via the ELECTRON_RUN_AS_NODE env var', function (done) {
|
2017-06-12 21:04:57 +00:00
|
|
|
child = ChildProcess.spawn(process.execPath, [path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
|
2016-10-12 20:01:43 +00:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
2017-06-12 21:04:57 +00:00
|
|
|
|
|
|
|
it('supports starting the v8 inspector with --inspect/--inspect-brk', function (done) {
|
|
|
|
child = ChildProcess.spawn(process.execPath, ['--inspect-brk', path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
|
|
|
|
env: {
|
|
|
|
ELECTRON_RUN_AS_NODE: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
let output = ''
|
|
|
|
child.stderr.on('data', function (data) {
|
|
|
|
output += data
|
|
|
|
|
2017-07-12 23:59:53 +00:00
|
|
|
if (output.trim().startsWith('Debugger listening on ws://')) {
|
2017-06-12 21:04:57 +00:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
child.stdout.on('data', function (data) {
|
|
|
|
done(new Error(`Unexpected output: ${data.toString()}`))
|
|
|
|
})
|
|
|
|
})
|
2016-10-12 20:01:43 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('contexts', function () {
|
|
|
|
describe('setTimeout in fs callback', function () {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.env.TRAVIS === 'true') {
|
2016-03-25 20:03:49 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
it('does not crash', function (done) {
|
|
|
|
fs.readFile(__filename, function () {
|
|
|
|
setTimeout(done, 0)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-25 16:55:38 +00:00
|
|
|
describe('error thrown in renderer process node context', function () {
|
2017-01-25 17:04:25 +00:00
|
|
|
it('gets emitted as a process uncaughtException event', function (done) {
|
2017-01-25 16:55:38 +00:00
|
|
|
const error = new Error('boo!')
|
|
|
|
const listeners = process.listeners('uncaughtException')
|
2016-03-25 20:03:49 +00:00
|
|
|
process.removeAllListeners('uncaughtException')
|
2017-01-25 16:55:38 +00:00
|
|
|
process.on('uncaughtException', (thrown) => {
|
|
|
|
assert.strictEqual(thrown, error)
|
2016-03-25 20:03:49 +00:00
|
|
|
process.removeAllListeners('uncaughtException')
|
2017-01-25 16:55:38 +00:00
|
|
|
listeners.forEach((listener) => {
|
|
|
|
process.on('uncaughtException', listener)
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
done()
|
|
|
|
})
|
2017-01-25 16:55:38 +00:00
|
|
|
fs.readFile(__filename, () => {
|
2016-03-25 20:03:49 +00:00
|
|
|
throw error
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-25 16:55:38 +00:00
|
|
|
describe('error thrown in main process node context', function () {
|
2017-01-25 18:19:38 +00:00
|
|
|
it('gets emitted as a process uncaughtException event', function () {
|
2017-01-25 16:55:38 +00:00
|
|
|
const error = ipcRenderer.sendSync('handle-uncaught-exception', 'hello')
|
|
|
|
assert.equal(error, 'hello')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-25 17:04:25 +00:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
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 () {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (process.platform !== 'darwin') {
|
2016-03-25 20:03:49 +00:00
|
|
|
return
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
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')
|
2016-06-29 16:37:10 +00:00
|
|
|
var child = ChildProcess.fork(script, [socketPath])
|
2016-03-25 20:03:49 +00:00
|
|
|
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)
|
|
|
|
})
|
2016-04-05 08:08:27 +00:00
|
|
|
|
|
|
|
it('does not crash when creating large Buffers', function () {
|
2016-06-29 16:37:10 +00:00
|
|
|
var buffer = new Buffer(new Array(4096).join(' '))
|
2016-06-29 16:44:38 +00:00
|
|
|
assert.equal(buffer.length, 4095)
|
2016-06-29 16:37:10 +00:00
|
|
|
buffer = new Buffer(new Array(4097).join(' '))
|
2016-06-29 16:44:38 +00:00
|
|
|
assert.equal(buffer.length, 4096)
|
2016-04-05 08:08:27 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('process.stdout', function () {
|
2016-09-07 21:40:18 +00:00
|
|
|
it('does not throw an exception when accessed', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
process.stdout
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2016-09-07 21:40:18 +00:00
|
|
|
it('does not throw an exception when calling write()', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
process.stdout.write('test')
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
|
2016-09-08 18:01:58 +00:00
|
|
|
it('should have isTTY defined on Mac and Linux', function () {
|
2016-06-20 02:16:17 +00:00
|
|
|
if (isCI) return
|
|
|
|
|
2016-09-08 20:12:00 +00:00
|
|
|
if (process.platform === 'win32') {
|
2016-09-08 18:01:58 +00:00
|
|
|
assert.equal(process.stdout.isTTY, undefined)
|
2016-09-08 20:12:00 +00:00
|
|
|
} else {
|
2016-09-08 18:01:58 +00:00
|
|
|
assert.equal(typeof process.stdout.isTTY, 'boolean')
|
2016-09-08 20:12:00 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-07 21:40:18 +00:00
|
|
|
describe('process.stdin', function () {
|
|
|
|
it('does not throw an exception when accessed', function () {
|
|
|
|
assert.doesNotThrow(function () {
|
|
|
|
process.stdin
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-09-07 21:51:05 +00:00
|
|
|
it('returns null when read from', function () {
|
|
|
|
assert.equal(process.stdin.read(), null)
|
2016-09-07 21:40:18 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-04-05 10:53:07 +00:00
|
|
|
describe('process.version', function () {
|
|
|
|
it('should not have -pre', function () {
|
|
|
|
assert(!process.version.endsWith('-pre'))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
describe('vm.createContext', function () {
|
|
|
|
it('should not crash', function () {
|
|
|
|
require('vm').runInNewContext('')
|
|
|
|
})
|
|
|
|
})
|
2017-01-12 01:51:46 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|