electron/spec/node-spec.js

221 lines
6.7 KiB
JavaScript
Raw Normal View History

2016-01-19 22:49:40 +00:00
const assert = require('assert');
const child_process = require('child_process');
2016-01-20 16:49:52 +00:00
const fs = require('fs');
2016-01-19 22:49:40 +00:00
const path = require('path');
const os = require('os');
const remote = require('electron').remote;
2016-01-12 02:40:23 +00:00
describe('node feature', function() {
var fixtures = path.join(__dirname, 'fixtures');
2016-01-12 02:40:23 +00:00
describe('child_process', function() {
2016-02-17 01:39:11 +00:00
describe('child_process.fork', function() {
2016-01-12 02:40:23 +00:00
it('works in current process', function(done) {
2016-02-17 17:27:25 +00:00
var child = child_process.fork(path.join(fixtures, 'module', 'ping.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'message');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('preserves args', function(done) {
2016-02-17 01:39:11 +00:00
var args = ['--expose_gc', '-test', '1'];
var child = child_process.fork(path.join(fixtures, 'module', 'process_args.js'), args);
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.deepEqual(args, msg.slice(2));
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('works in forked process', function(done) {
2016-02-17 01:39:11 +00:00
var child = child_process.fork(path.join(fixtures, 'module', 'fork_ping.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'message');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('works in forked process when options.env is specifed', function(done) {
2016-02-17 01:39:11 +00:00
var child = child_process.fork(path.join(fixtures, 'module', 'fork_ping.js'), [], {
2016-01-12 02:40:23 +00:00
path: process.env['PATH']
});
child.on('message', function(msg) {
assert.equal(msg, 'message');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('works in browser process', function(done) {
2016-02-17 17:27:25 +00:00
var fork = remote.require('child_process').fork;
var child = fork(path.join(fixtures, 'module', 'ping.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'message');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-01-12 02:40:23 +00:00
it('has String::localeCompare working in script', function(done) {
2016-02-17 17:27:25 +00:00
var child = child_process.fork(path.join(fixtures, 'module', 'locale-compare.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.deepEqual(msg, [0, -1, 1]);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('has setImmediate working in script', function(done) {
var child = child_process.fork(path.join(fixtures, 'module', 'set-immediate.js'));
2016-01-12 02:40:23 +00:00
child.on('message', function(msg) {
assert.equal(msg, 'ok');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
child.send('message');
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('contexts', function() {
describe('setTimeout in fs callback', function() {
if (process.env.TRAVIS === 'true') {
return;
}
2016-02-17 01:39:11 +00:00
it('does not crash', function(done) {
fs.readFile(__filename, function() {
setTimeout(done, 0);
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('throw error in node context', function() {
2016-02-17 01:39:11 +00:00
it('gets caught', function(done) {
var error = new Error('boo!');
var lsts = process.listeners('uncaughtException');
2016-01-12 02:40:23 +00:00
process.removeAllListeners('uncaughtException');
2016-01-19 22:49:40 +00:00
process.on('uncaughtException', function() {
2016-01-12 02:40:23 +00:00
var i, len, lst;
process.removeAllListeners('uncaughtException');
for (i = 0, len = lsts.length; i < len; i++) {
lst = lsts[i];
process.on('uncaughtException', lst);
}
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
fs.readFile(__filename, function() {
2016-01-12 02:40:23 +00:00
throw error;
});
});
});
2016-01-12 02:40:23 +00:00
describe('setTimeout called under Chromium event loop in browser process', function() {
2016-02-17 01:39:11 +00:00
it('can be scheduled in time', function(done) {
remote.getGlobal('setTimeout')(done, 0);
2016-01-12 02:40:23 +00:00
});
});
2016-02-17 01:39:11 +00:00
describe('setInterval called under Chromium event loop in browser process', function() {
it('can be scheduled in time', function(done) {
2016-01-12 02:40:23 +00:00
var clear, interval;
clear = function() {
remote.getGlobal('clearInterval')(interval);
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
};
2016-02-17 01:39:11 +00:00
interval = remote.getGlobal('setInterval')(clear, 10);
2016-01-12 02:40:23 +00:00
});
});
});
2016-01-12 02:40:23 +00:00
describe('message loop', function() {
describe('process.nextTick', function() {
it('emits the callback', function(done) {
2016-02-17 01:39:11 +00:00
process.nextTick(done);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('works in nested calls', function(done) {
process.nextTick(function() {
process.nextTick(function() {
process.nextTick(done);
2016-01-12 02:40:23 +00:00
});
});
});
});
2016-02-17 01:39:11 +00:00
describe('setImmediate', function() {
2016-01-12 02:40:23 +00:00
it('emits the callback', function(done) {
2016-02-17 01:39:11 +00:00
setImmediate(done);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('works in nested calls', function(done) {
setImmediate(function() {
setImmediate(function() {
setImmediate(done);
2016-01-12 02:40:23 +00:00
});
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('net.connect', function() {
if (process.platform !== 'darwin') {
return;
}
2016-02-17 01:39:11 +00:00
it('emit error when connect to a socket path without listeners', function(done) {
2016-02-17 17:27:25 +00:00
var socketPath = path.join(os.tmpdir(), 'atom-shell-test.sock');
var script = path.join(fixtures, 'module', 'create_socket.js');
var child = child_process.fork(script, [socketPath]);
2016-02-17 01:39:11 +00:00
child.on('exit', function(code) {
2016-01-12 02:40:23 +00:00
assert.equal(code, 0);
2016-02-17 17:27:25 +00:00
var client = require('net').connect(socketPath);
2016-02-17 01:39:11 +00:00
client.on('error', function(error) {
2016-01-12 02:40:23 +00:00
assert.equal(error.code, 'ECONNREFUSED');
2016-02-17 01:39:11 +00:00
done();
2016-01-12 02:40:23 +00:00
});
});
});
});
2016-01-12 02:40:23 +00:00
describe('Buffer', function() {
it('can be created from WebKit external string', function() {
2016-02-17 17:27:25 +00:00
var p = document.createElement('p');
2016-01-12 02:40:23 +00:00
p.innerText = '闲云潭影日悠悠,物换星移几度秋';
2016-02-17 17:27:25 +00:00
var b = new Buffer(p.innerText);
2016-01-12 02:40:23 +00:00
assert.equal(b.toString(), '闲云潭影日悠悠,物换星移几度秋');
2016-02-17 01:39:11 +00:00
assert.equal(Buffer.byteLength(p.innerText), 45);
2016-01-12 02:40:23 +00:00
});
2016-02-17 01:39:11 +00:00
it('correctly parses external one-byte UTF8 string', function() {
2016-02-17 17:27:25 +00:00
var p = document.createElement('p');
2016-01-12 02:40:23 +00:00
p.innerText = 'Jøhänñéß';
2016-02-17 17:27:25 +00:00
var b = new Buffer(p.innerText);
2016-01-12 02:40:23 +00:00
assert.equal(b.toString(), 'Jøhänñéß');
2016-02-17 01:39:11 +00:00
assert.equal(Buffer.byteLength(p.innerText), 13);
2016-01-12 02:40:23 +00:00
});
});
2016-01-26 12:26:42 +00:00
2016-01-12 02:40:23 +00:00
describe('process.stdout', function() {
it('should not throw exception', function() {
2016-01-26 12:26:42 +00:00
process.stdout;
2016-01-12 02:40:23 +00:00
});
2016-01-26 12:26:42 +00:00
it('should not throw exception when calling write()', function() {
process.stdout.write('test');
});
xit('should have isTTY defined', function() {
assert.equal(typeof process.stdout.isTTY, 'boolean');
2016-01-12 02:40:23 +00:00
});
});
2016-01-26 12:26:42 +00:00
describe('vm.createContext', function() {
it('should not crash', function() {
require('vm').runInNewContext('');
2016-01-12 02:40:23 +00:00
});
});
});