test: remove usage of remote module from node tests (#20103)
* test: remove usage of remote module from node tests * isTTY is undefined in the renderer process on all platforms * Update spec/node-spec.js * Update node-spec.js
This commit is contained in:
parent
ae6f08d56e
commit
9abdfc11a9
2 changed files with 210 additions and 204 deletions
206
spec-main/node-spec.ts
Normal file
206
spec-main/node-spec.ts
Normal file
|
@ -0,0 +1,206 @@
|
|||
import { expect } from 'chai'
|
||||
import * as childProcess from 'child_process'
|
||||
import * as path from 'path'
|
||||
import * as util from 'util'
|
||||
import { emittedOnce } from './events-helpers';
|
||||
import { ifdescribe, ifit } from './spec-helpers';
|
||||
import { webContents, WebContents } from 'electron';
|
||||
|
||||
const features = process.electronBinding('features')
|
||||
|
||||
describe('node feature', () => {
|
||||
const fixtures = path.join(__dirname, '..', 'spec', 'fixtures')
|
||||
describe('child_process', () => {
|
||||
describe('child_process.fork', () => {
|
||||
it('works in browser process', (done) => {
|
||||
const child = childProcess.fork(path.join(fixtures, 'module', 'ping.js'))
|
||||
child.on('message', (msg) => {
|
||||
expect(msg).to.equal('message')
|
||||
done()
|
||||
})
|
||||
child.send('message')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('contexts', () => {
|
||||
describe('setTimeout called under Chromium event loop in browser process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
setTimeout(done, 0)
|
||||
})
|
||||
|
||||
it('can be promisified', (done) => {
|
||||
util.promisify(setTimeout)(0).then(done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setInterval called under Chromium event loop in browser process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
let interval: any = null
|
||||
let clearing = false
|
||||
const clear = () => {
|
||||
if (interval === null || clearing) return
|
||||
|
||||
// interval might trigger while clearing (remote is slow sometimes)
|
||||
clearing = true
|
||||
clearInterval(interval)
|
||||
clearing = false
|
||||
interval = null
|
||||
done()
|
||||
}
|
||||
interval = setInterval(clear, 10)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
ifdescribe(features.isRunAsNodeEnabled())('inspector', () => {
|
||||
let child: childProcess.ChildProcessWithoutNullStreams
|
||||
let exitPromise: Promise<any[]>
|
||||
|
||||
afterEach(async () => {
|
||||
if (child && exitPromise) {
|
||||
const [code, signal] = await exitPromise
|
||||
expect(signal).to.equal(null)
|
||||
expect(code).to.equal(0)
|
||||
} else if (child) {
|
||||
child.kill()
|
||||
}
|
||||
})
|
||||
|
||||
it('supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
|
||||
child = childProcess.spawn(process.execPath, ['--inspect-brk', path.join(fixtures, 'module', 'run-as-node.js')], {
|
||||
env: {
|
||||
ELECTRON_RUN_AS_NODE: 'true'
|
||||
}
|
||||
})
|
||||
|
||||
let output = ''
|
||||
function cleanup () {
|
||||
child.stderr.removeListener('data', errorDataListener)
|
||||
child.stdout.removeListener('data', outDataHandler)
|
||||
}
|
||||
function errorDataListener (data: Buffer) {
|
||||
output += data
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
cleanup()
|
||||
done()
|
||||
}
|
||||
}
|
||||
function outDataHandler (data: Buffer) {
|
||||
cleanup()
|
||||
done(new Error(`Unexpected output: ${data.toString()}`))
|
||||
}
|
||||
child.stderr.on('data', errorDataListener)
|
||||
child.stdout.on('data', outDataHandler)
|
||||
})
|
||||
|
||||
it('supports starting the v8 inspector with --inspect and a provided port', (done) => {
|
||||
child = childProcess.spawn(process.execPath, ['--inspect=17364', path.join(fixtures, 'module', 'run-as-node.js')], {
|
||||
env: {
|
||||
ELECTRON_RUN_AS_NODE: 'true'
|
||||
}
|
||||
})
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function cleanup () {
|
||||
child.stderr.removeListener('data', errorDataListener)
|
||||
child.stdout.removeListener('data', outDataHandler)
|
||||
}
|
||||
function errorDataListener (data: Buffer) {
|
||||
output += data
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364')
|
||||
cleanup()
|
||||
done()
|
||||
}
|
||||
}
|
||||
function outDataHandler (data: Buffer) {
|
||||
cleanup()
|
||||
done(new Error(`Unexpected output: ${data.toString()}`))
|
||||
}
|
||||
child.stderr.on('data', errorDataListener)
|
||||
child.stdout.on('data', outDataHandler)
|
||||
})
|
||||
|
||||
it('does not start the v8 inspector when --inspect is after a -- argument', (done) => {
|
||||
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'noop.js'), '--', '--inspect'])
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function dataListener (data: Buffer) {
|
||||
output += data
|
||||
}
|
||||
child.stderr.on('data', dataListener)
|
||||
child.stdout.on('data', dataListener)
|
||||
child.on('exit', () => {
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
done(new Error('Inspector was started when it should not have been'))
|
||||
} else {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// IPC Electron child process not supported on Windows
|
||||
ifit(process.platform !== 'win32')('does does not crash when quitting with the inspector connected', function (done) {
|
||||
child = childProcess.spawn(process.execPath, [path.join(fixtures, 'module', 'delay-exit'), '--inspect=0'], {
|
||||
stdio: ['ipc']
|
||||
}) as childProcess.ChildProcessWithoutNullStreams
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function dataListener (data: Buffer) {
|
||||
output += data
|
||||
|
||||
if (output.trim().indexOf('Debugger listening on ws://') > -1 && output.indexOf('\n') > -1) {
|
||||
const socketMatch = output.trim().match(/(ws:\/\/.+:[0-9]+\/.+?)\n/gm)
|
||||
if (socketMatch && socketMatch[0]) {
|
||||
child.stderr.removeListener('data', dataListener)
|
||||
child.stdout.removeListener('data', dataListener)
|
||||
|
||||
const w = (webContents as any).create({}) as WebContents
|
||||
w.loadURL('about:blank')
|
||||
.then(() => w.executeJavaScript(`new Promise(resolve => {
|
||||
const connection = new WebSocket(${JSON.stringify(socketMatch[0])})
|
||||
connection.onopen = () => {
|
||||
resolve()
|
||||
connection.close()
|
||||
}
|
||||
})`))
|
||||
.then(() => {
|
||||
(w as any).destroy()
|
||||
child.send('plz-quit')
|
||||
done()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
child.stderr.on('data', dataListener)
|
||||
child.stdout.on('data', dataListener)
|
||||
})
|
||||
|
||||
it('supports js binding', (done) => {
|
||||
child = childProcess.spawn(process.execPath, ['--inspect', path.join(fixtures, 'module', 'inspector-binding.js')], {
|
||||
env: {
|
||||
ELECTRON_RUN_AS_NODE: 'true'
|
||||
},
|
||||
stdio: ['ipc']
|
||||
}) as childProcess.ChildProcessWithoutNullStreams
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
child.on('message', ({ cmd, debuggerEnabled, success }) => {
|
||||
if (cmd === 'assert') {
|
||||
expect(debuggerEnabled).to.be.true()
|
||||
expect(success).to.be.true()
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('can find a module using a package.json main field', () => {
|
||||
const result = childProcess.spawnSync(process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')])
|
||||
expect(result.status).to.equal(0)
|
||||
})
|
||||
})
|
|
@ -5,12 +5,12 @@ const dirtyChai = require('dirty-chai')
|
|||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const { ipcRenderer, remote } = require('electron')
|
||||
const { ipcRenderer } = require('electron')
|
||||
const features = process.electronBinding('features')
|
||||
|
||||
const { emittedOnce } = require('./events-helpers')
|
||||
const { ifit } = require('./spec-helpers')
|
||||
|
||||
const isCI = remote.getGlobal('isCi')
|
||||
chai.use(dirtyChai)
|
||||
|
||||
describe('node feature', () => {
|
||||
|
@ -63,16 +63,6 @@ describe('node feature', () => {
|
|||
child.send('message')
|
||||
})
|
||||
|
||||
it('works in browser process', (done) => {
|
||||
const fork = remote.require('child_process').fork
|
||||
const child = fork(path.join(fixtures, 'module', 'ping.js'))
|
||||
child.on('message', (msg) => {
|
||||
expect(msg).to.equal('message')
|
||||
done()
|
||||
})
|
||||
child.send('message')
|
||||
})
|
||||
|
||||
it('has String::localeCompare working in script', (done) => {
|
||||
const child = ChildProcess.fork(path.join(fixtures, 'module', 'locale-compare.js'))
|
||||
child.on('message', (msg) => {
|
||||
|
@ -215,16 +205,6 @@ describe('node feature', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('setTimeout called under Chromium event loop in browser process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
remote.getGlobal('setTimeout')(done, 0)
|
||||
})
|
||||
|
||||
it('can be promisified', (done) => {
|
||||
remote.getGlobal('setTimeoutPromisified')(0).then(done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setTimeout called under blink env in renderer process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
setTimeout(done, 10)
|
||||
|
@ -235,24 +215,6 @@ describe('node feature', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('setInterval called under Chromium event loop in browser process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
let interval = null
|
||||
let clearing = false
|
||||
const clear = () => {
|
||||
if (interval === null || clearing) return
|
||||
|
||||
// interval might trigger while clearing (remote is slow sometimes)
|
||||
clearing = true
|
||||
remote.getGlobal('clearInterval')(interval)
|
||||
clearing = false
|
||||
interval = null
|
||||
done()
|
||||
}
|
||||
interval = remote.getGlobal('setInterval')(clear, 10)
|
||||
})
|
||||
})
|
||||
|
||||
describe('setInterval called under blink env in renderer process', () => {
|
||||
it('can be scheduled in time', (done) => {
|
||||
let interval = null
|
||||
|
@ -288,150 +250,6 @@ describe('node feature', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('inspector', () => {
|
||||
let child = null
|
||||
let exitPromise = null
|
||||
|
||||
beforeEach(function () {
|
||||
if (!features.isRunAsNodeEnabled()) {
|
||||
this.skip()
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
if (child && exitPromise) {
|
||||
const [code, signal] = await exitPromise
|
||||
expect(signal).to.equal(null)
|
||||
expect(code).to.equal(0)
|
||||
} else if (child) {
|
||||
child.kill()
|
||||
}
|
||||
})
|
||||
|
||||
it('supports starting the v8 inspector with --inspect/--inspect-brk', (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 = ''
|
||||
function cleanup () {
|
||||
child.stderr.removeListener('data', errorDataListener)
|
||||
child.stdout.removeListener('data', outDataHandler)
|
||||
}
|
||||
function errorDataListener (data) {
|
||||
output += data
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
cleanup()
|
||||
done()
|
||||
}
|
||||
}
|
||||
function outDataHandler (data) {
|
||||
cleanup()
|
||||
done(new Error(`Unexpected output: ${data.toString()}`))
|
||||
}
|
||||
child.stderr.on('data', errorDataListener)
|
||||
child.stdout.on('data', outDataHandler)
|
||||
})
|
||||
|
||||
it('supports starting the v8 inspector with --inspect and a provided port', (done) => {
|
||||
child = ChildProcess.spawn(process.execPath, ['--inspect=17364', path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
|
||||
env: {
|
||||
ELECTRON_RUN_AS_NODE: true
|
||||
}
|
||||
})
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function cleanup () {
|
||||
child.stderr.removeListener('data', errorDataListener)
|
||||
child.stdout.removeListener('data', outDataHandler)
|
||||
}
|
||||
function errorDataListener (data) {
|
||||
output += data
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
expect(output.trim()).to.contain(':17364', 'should be listening on port 17364')
|
||||
cleanup()
|
||||
done()
|
||||
}
|
||||
}
|
||||
function outDataHandler (data) {
|
||||
cleanup()
|
||||
done(new Error(`Unexpected output: ${data.toString()}`))
|
||||
}
|
||||
child.stderr.on('data', errorDataListener)
|
||||
child.stdout.on('data', outDataHandler)
|
||||
})
|
||||
|
||||
it('does not start the v8 inspector when --inspect is after a -- argument', (done) => {
|
||||
child = ChildProcess.spawn(remote.process.execPath, [path.join(__dirname, 'fixtures', 'module', 'noop.js'), '--', '--inspect'])
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function dataListener (data) {
|
||||
output += data
|
||||
}
|
||||
child.stderr.on('data', dataListener)
|
||||
child.stdout.on('data', dataListener)
|
||||
child.on('exit', () => {
|
||||
if (output.trim().startsWith('Debugger listening on ws://')) {
|
||||
done(new Error('Inspector was started when it should not have been'))
|
||||
} else {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('does does not crash when quitting with the inspector connected', function (done) {
|
||||
// IPC Electron child process not supported on Windows
|
||||
if (process.platform === 'win32') return this.skip()
|
||||
child = ChildProcess.spawn(remote.process.execPath, [path.join(__dirname, 'fixtures', 'module', 'delay-exit'), '--inspect=0'], {
|
||||
stdio: ['ipc']
|
||||
})
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
let output = ''
|
||||
function dataListener (data) {
|
||||
output += data
|
||||
|
||||
if (output.trim().indexOf('Debugger listening on ws://') > -1 && output.indexOf('\n') > -1) {
|
||||
const socketMatch = output.trim().match(/(ws:\/\/.+:[0-9]+\/.+?)\n/gm)
|
||||
if (socketMatch && socketMatch[0]) {
|
||||
child.stderr.removeListener('data', dataListener)
|
||||
child.stdout.removeListener('data', dataListener)
|
||||
const connection = new WebSocket(socketMatch[0])
|
||||
connection.onopen = () => {
|
||||
child.send('plz-quit')
|
||||
connection.close()
|
||||
done()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
child.stderr.on('data', dataListener)
|
||||
child.stdout.on('data', dataListener)
|
||||
})
|
||||
|
||||
it('supports js binding', (done) => {
|
||||
child = ChildProcess.spawn(process.execPath, ['--inspect', path.join(__dirname, 'fixtures', 'module', 'inspector-binding.js')], {
|
||||
env: {
|
||||
ELECTRON_RUN_AS_NODE: true
|
||||
},
|
||||
stdio: ['ipc']
|
||||
})
|
||||
exitPromise = emittedOnce(child, 'exit')
|
||||
|
||||
child.on('message', ({ cmd, debuggerEnabled, success }) => {
|
||||
if (cmd === 'assert') {
|
||||
expect(debuggerEnabled).to.be.true()
|
||||
expect(success).to.be.true()
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('message loop', () => {
|
||||
describe('process.nextTick', () => {
|
||||
it('emits the callback', (done) => process.nextTick(done))
|
||||
|
@ -526,21 +344,8 @@ describe('node feature', () => {
|
|||
}).to.not.throw()
|
||||
})
|
||||
|
||||
it('should have isTTY defined on Mac and Linux', function () {
|
||||
if (isCI || process.platform === 'win32') {
|
||||
this.skip()
|
||||
return
|
||||
}
|
||||
|
||||
expect(process.stdout.isTTY).to.be.a('boolean')
|
||||
})
|
||||
|
||||
it('should have isTTY undefined on Windows', function () {
|
||||
if (isCI || process.platform !== 'win32') {
|
||||
this.skip()
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: figure out why process.stdout.isTTY is true on Darwin but not Linux/Win.
|
||||
ifit(process.platform !== 'darwin')('isTTY should be undefined in the renderer process', function () {
|
||||
expect(process.stdout.isTTY).to.be.undefined()
|
||||
})
|
||||
})
|
||||
|
@ -636,9 +441,4 @@ describe('node feature', () => {
|
|||
.that.is.a('string')
|
||||
.and.matches(/^\d+\.\d+\.\d+\.\d+$/)
|
||||
})
|
||||
|
||||
it('can find a module using a package.json main field', () => {
|
||||
const result = ChildProcess.spawnSync(remote.process.execPath, [path.resolve(fixtures, 'api', 'electron-main-module', 'app.asar')])
|
||||
expect(result.status).to.equal(0)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue